Last active
February 18, 2021 02:06
-
-
Save ororox/f688408980ef0ac82e2d98b432267d4f to your computer and use it in GitHub Desktop.
MNIST, FASION_MNIST, myCallback
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
print(tf.__version__) | |
class myCallback(tf.keras.callbacks.Callback): | |
def on_epoch_end(self, epoch, logs={}): | |
if(logs.get('acc')>=0.90): | |
print("\nReached 90% accuracy so cancelling training!") | |
self.model.stop_training = True | |
#mnist = tf.keras.datasets.fashion_mnist | |
mnist = tf.keras.datasets.mnist | |
(training_images, training_labels), (test_images, test_labels) = mnist.load_data() | |
''' | |
import matplotlib.pyplot as plt | |
plt.imshow(test_images[0]) | |
plt.show() | |
plt.clf() | |
import numpy as np | |
np.set_printoptions(linewidth=200) | |
print(test_images[0]) | |
''' | |
#training_images=training_images.reshape(60000, 28, 28, 1) | |
#test_images = test_images.reshape(10000, 28, 28, 1) | |
training_images=training_images/255.0 | |
test_images=test_images/255.0 | |
model = tf.keras.models.Sequential([ | |
#tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu, input_shape=(28, 28, 1)), | |
#tf.keras.layers.MaxPooling2D(2, 2), | |
#tf.keras.layers.Conv2D(64, (3, 3), activation=tf.nn.relu), | |
#tf.keras.layers.MaxPooling2D(2, 2), | |
tf.keras.layers.Flatten(), | |
#tf.keras.layers.Dense(512, activation=tf.nn.relu), | |
tf.keras.layers.Dense(128, activation=tf.nn.relu), | |
tf.keras.layers.Dense(10, activation=tf.nn.softmax) | |
]) | |
model.compile(optimizer=tf.optimizers.Adam(), | |
loss=tf.losses.sparse_categorical_crossentropy, | |
metrics=['acc']) | |
model.fit(training_images, training_labels, epochs=5, callbacks=[myCallback()]) | |
model.evaluate(test_images, test_labels) | |
classifications = model.predict(test_images) | |
import matplotlib.pyplot as plt | |
#plt.plot(classifications[0]) | |
#plt.axis([0, 9, 0, 1]) | |
plt.bar([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], classifications[0]) | |
plt.show() | |
plt.clf() | |
#plt.imshow(test_images[0]) | |
#plt.show() | |
#plt.clf() | |
print(test_labels[0]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment