Skip to content

Instantly share code, notes, and snippets.

@XinyueZ
Last active March 5, 2022 14:22
Show Gist options
  • Select an option

  • Save XinyueZ/7eadbd96458f27be432647e8f975f06d to your computer and use it in GitHub Desktop.

Select an option

Save XinyueZ/7eadbd96458f27be432647e8f975f06d to your computer and use it in GitHub Desktop.
TF MNIST, a skeleton for deep learning based on classical MNIST.
import tensorflow as tf
from tensorflow.keras.layers import Flatten
from IPython.display import Markdown, display
import matplotlib.pyplot as plt
%matplotlib inline
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
y_train = tf.one_hot(y_train, 10)
y_test = tf.one_hot(y_test, 10)
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(50)
test_ds = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(50)
flatten = Flatten(dtype='float32')
def activate(x):
return tf.nn.softmax(forward(x))
def model(x):
x = flatten(x)
return activate(x)
def cross_entropy(y_label, y_pred):
return (-tf.reduce_sum(y_label * tf.math.log(y_pred + 1.e-10))) # addition of 1e-10 to prevent errors in zero calculations
optimizer = tf.keras.optimizers.SGD(learning_rate=0.25)
def train_step(x, y ):
with tf.GradientTape() as tape:
#compute loss function
current_loss = cross_entropy( y, model(x))
# compute gradient of loss
#(This is automatic! Even with specialized funcctions!)
grads = tape.gradient( current_loss , [W,b] )
# Apply SGD step to our Variables W and b
optimizer.apply_gradients( zip( grads , [W,b] ) )
return current_loss.numpy()
# zeroing out weights in case you want to run this cell multiple times
# Weight tensor
W = tf.Variable(tf.zeros([784, 10], tf.float32))
# Bias tensor
b = tf.Variable(tf.zeros([10], tf.float32))
loss_values = []
accuracies = []
epochs = 10
for i in range(epochs):
j = 0
# each batch has 50 examples
for x_train_batch, y_train_batch in train_ds:
j += 1
current_loss = train_step(x_train_batch, y_train_batch)
if j % 500 == 0: # reporting intermittent batch statistics
print("epoch ", str(i), "batch", str(
j), "loss:", str(current_loss))
# collecting statistics at each epoch...loss function and accuracy
# loss function
current_loss = cross_entropy(y_train, model(x_train)).numpy()
loss_values.append(current_loss)
correct_prediction = tf.equal(tf.argmax(model(x_train), axis=1),
tf.argmax(y_train, axis=1))
print(correct_prediction)
# accuracy
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)).numpy()
accuracies.append(accuracy)
print("end of epoch ", str(i), "loss", str(
current_loss), "accuracy", str(accuracy))
correct_prediction_train = tf.equal(tf.argmax(model(x_train), axis=1),tf.argmax(y_train,axis=1))
accuracy_train = tf.reduce_mean(tf.cast(correct_prediction_train, tf.float32)).numpy()
correct_prediction_test = tf.equal(tf.argmax(model(x_test), axis=1),tf.argmax(y_test, axis=1))
accuracy_test = tf.reduce_mean(tf.cast(correct_prediction_test, tf.float32)).numpy()
print("training accuracy", accuracy_train)
print("test accuracy", accuracy_test)
plt.rcParams['figure.figsize'] = (10, 6)
print(loss_values)
plt.plot(loss_values,'-ro')
plt.title("loss per epoch")
plt.xlabel("epoch")
plt.ylabel("loss")
plt.plot(accuracies,'-ro')
plt.title("accuracy per epoch")
plt.xlabel("epoch")
plt.ylabel("accuracy")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment