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

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Activation
from tensorflow.keras.datasets import fashion_mnist
from matplotlib import pyplot as plt
import numpy as np

# Geron implementation
import tensorflow as tf
from tensorflow import keras
print(tf.__version__, keras.__version__)

(Xtrain_full, ytrain_full), (Xtest, ytest) = fashion_mnist.load_data()

class_names = ["top", "trousers", "pullover", "dress", "coat", "sandals", "shirt", "trainers", "bag", "boot"]

# Geron implementation
Xvalid, Xtrain = Xtrain_full[:5000] / 255.0, Xtrain_full[5000:] / 255.0
yvalid, ytrain = ytrain_full[:5000], ytrain_full[5000:]
Xtest = Xtest / 255.0

ytrain[0]

class_names[ytrain[0]]

xtrain[0].shape

elem1 = xtrain[0].flatten()

elem1.shape

# +
from matplotlib import pyplot as plt

for i in range(25):
    plt.subplot(5,5,i+1)
    plt.imshow(Xtrain[i], cmap=plt.cm.Greys)
    plt.axis('off')
# -

# Geron implementation P301
model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28,28]))
model.add(keras.layers.Dense(300, activation='relu'))
model.add(keras.layers.Dense(100, activation='relu'))
model.add(keras.layers.Dense(10, activation='softmax'))

model.summary()

# Geron implementation P302
model.compile(loss='sparse_categorical_crossentropy',
             optimizer='sgd',
             metrics=['accuracy'])

# Geron implementation P303
history = model.fit(Xtrain, ytrain, epochs=30,
                   validation_data=(Xvalid, yvalid))

# Geron implementation P305
import pandas as pd
pd.DataFrame(history.history).plot(figsize=(8,5))
plt.grid(True)
plt.gca().set_ylim(0,1)
plt.show()

# +
m = Sequential([
    Dense(units = 4, activation = 'relu', input_shape = (2,)),
    Dense(units = 1, activation = 'sigmoid')
])

#build computation graph
m.compile(optimizer='rmsprop',
         loss='binary_crossentropy',
         metrics=['accuracy'])

#print all layers
print(m.summary())
# -

#Train the model
h = m.fit(X, y, epochs=100, batch_size=50, validation_split=0.2)

print(len(h.history['loss']))
print(len(h.history['val_loss']))

#plot the learning curve
plt.plot(h.history['loss'], c='green')
plt.plot(h.history['val_loss'], c='red')
plt.plot(h.history['val_accuracy'], c='orange')

score = m.evaluate(X, y, batch_size=50)
print(score)

#predict on data points - I think these have been used to train the model though
print(m.predict(X[:10]))

X[:10]

m.summary()

acc = h.history['accuracy']
plt.plot(range(len(acc)),acc)

# !pip install h5py

# !pip install --upgrade pip

# +
#examining model weights

#save a list of np.arrays with the weights
w = m.
# -

# !pip install pydot

# !pip install pydot

# +
from IPython.display import SVG
from tensorflow.keras.utils import plot_model

plot_model(m,
          to_file='model.png',
          show_shapes=False,
          show_layer_names=True,
          rankdir='TB', expand_nested=False, dpi=96
          )
# -

y[:10]

ytrain[:10]



StarcoderdataJupyterScriptsDedupFiltered