# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # Imports

# +
# %matplotlib inline
import matplotlib.pyplot as plt
import tensorflow as tf



fashion_mnist = tf.keras.datasets.fashion_mnist
from sklearn.model_selection import train_test_split

(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

# -

# # Load the data

# +
num_classes = 10

x_train = x_train.reshape(60000, 784)
x_test = x_test.reshape(10000, 784)
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
y_train = tf.keras.utils.to_categorical(y_train, num_classes)
y_test = tf.keras.utils.to_categorical(y_test, num_classes)

print ('Train size:', x_train.shape[0])
print ('Test size:', x_test.shape[0])
# -

# # Build the network

# +
model = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(28, 28)),
    tf.keras.layers.experimental.preprocessing.Rescaling(scale=1./255, offset=0.0), # a layer to rescale an input in the [0, 255] range to be in the [0, 1] range
    tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(64, activation='relu', input_shape=(784,)),
    tf.keras.layers.Dense(10, activation='softmax')
])

for l in model.layers:
    print (l.name, l.input_shape,'==>',l.output_shape)

print (model.summary())
# -

# # Train the network

# + tags=[]
batch_size = 128
epochs = 30

model.compile(tf.keras.optimizers.Adam(learning_rate=0.001),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

    
history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))

score = model.evaluate(x_test, y_test, verbose=100)


print('Test loss:', round(score[0], 3))
print('Test accuracy:', round(score[1], 3))
plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['train', 'test'], loc='upper left')
# -

# # Build networks using all activations

# +
epochs = 30

for activation in [None, 'sigmoid', 'tanh', 'relu']:
    model = Sequential()
    model.add(tf.keras.layers.experimental.preprocessing.Rescaling(scale=1./255, offset=0.0) # a layer to rescale an input in the [0, 255] range to be in the [0, 1] range
    model.add(Dense(64, activation=activation, input_shape=(784,)))
    model.add(Dense(64, activation=activation, input_shape=(784,)))
    model.add(Dense(10, activation='softmax'))
    model.compile(tf.keras.optimizers.Adam(learning_rate=0.001),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])
    history = model.fit(x_train, y_train,
                    batch_size=batch_size,
                    epochs=epochs,
                    verbose=1,
                    validation_data=(x_test, y_test))
    
    plt.plot(history.history['val_acc'])
    
plt.title('model loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend(['None', 'sigmoid', 'tanh', 'relu'], loc='upper left')
# -



StarcoderdataJupyterScriptsDedupFiltered