Skip to content

Instantly share code, notes, and snippets.

View satishgunjal's full-sized avatar
💭
Analytics | CX Architect | Conversational AI | Chatbots | Machine Learning

Satish Gunjal satishgunjal

💭
Analytics | CX Architect | Conversational AI | Chatbots | Machine Learning
View GitHub Profile
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
predictions = probability_model.predict(test_images)
# 'predictions' will contain the prediction for each image in the training set. Lets check the first prediction
predictions[0]
test_loss, test_acc = model.evaluate(test_images, test_labels, verbose= 2)
print(f'\nTest accuracy: {test_acc}')
%%timeit -n1 -r1 # time required toexecute this cell once
# To view in TensorBoard
logdir = os.path.join("logs/adam", datetime.datetime.now().strftime("%Y%m%d-%H%M%S"))
tensorboard_callback = tf.keras.callbacks.TensorBoard(logdir, histogram_freq=1)
model.fit(train_images, train_labels, epochs= 10, callbacks = [tensorboard_callback])
# The from_logits=True attribute inform the loss function that the output values generated by the model are not normalized, a.k.a. logits.
# Since softmax layer is not being added at the last layer, hence we need to have the from_logits=True to indicate the probabilities are not normalized.
model.compile(optimizer= 'adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics = ['accuracy'])
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28,28)),
tf.keras.layers.Dense(128, activation= 'relu'),
tf.keras.layers.Dense(10) # linear activation function
])
train_images = train_images / 255.0
test_images = test_images / 255.0
@satishgunjal
satishgunjal / gist:230206da034e987299c1e879656d378c
Created October 17, 2020 07:40
Display the first 25 images from the training set and display the class name below each image.
# Display the first 25 images from the training set and display the class name below each image.
plt.figure(figsize=(10,10))
for i in range(25):
plt.subplot(5,5,i+1)
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(train_images[i], cmap=plt.cm.binary)
plt.xlabel(class_names[train_labels[i]])
plt.show()
@satishgunjal
satishgunjal / gist:5e37c446c9ab6f6f35ee75bc10615618
Created October 17, 2020 07:39
Images labels(classes) possible values from 0 to 9
# Images labels(classes) possible values from 0 to 9
train_labels
plt.figure()
plt.imshow(train_images[0])
plt.colorbar()
plt.grid(False)
plt.show()
@satishgunjal
satishgunjal / gist:c00ed1100b90c089e54b0cbb3482bbc1
Created October 17, 2020 07:37
The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255
# The images are 28x28 NumPy arrays, with pixel values ranging from 0 to 255
train_images