<!DOCTYPE html>
In [2]:
import tensorflow as tf
print(tf.__version__)
The Sequential model API¶
In [3]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Softmax
Build a feedforward neural network model¶
In [4]:
# Build the Sequential feedforward neural network model
model = Sequential([
Flatten(input_shape=(28, 28)),
Dense(16, activation='relu',name='laye_1'),
Dense(16, activation='relu'),
Dense(10),
Softmax()
])
In [5]:
# Print the model summary
model.summary()
In [6]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
Build a convolutional neural network model¶
In [7]:
# Build the Sequential convolutional neural network model
model = Sequential([
Conv2D(16, kernel_size =3, padding='same',activation='relu', strides=1,input_shape=(1, 28,28), data_format='channels_first'),
MaxPooling2D(pool_size=3, data_format='channels_first'),
Flatten(),
Dense(10, activation='softmax')
])
In [8]:
# Print the model summary
model.summary()
Compile the model¶
In [9]:
# Define the model optimizer, loss function and metrics
opt = tf.keras.optimizers.Adam(learning_rate=0.005)
acc = tf.keras.metrics.SparseCategoricalAccuracy()
mae = tf.keras.metrics.MeanAbsoluteError()
model.compile(optimizer=opt,
loss='sparse_categorical_crossentropy',
metrics=[acc, mae]
)
In [10]:
# Print the resulting model attributes
print(model.loss)
print(model.optimizer)
print(model.metrics)
print(model.optimizer.lr)
In [11]:
from tensorflow.keras.preprocessing import image
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Load the data¶
In [12]:
# Load the Fashion-MNIST dataset
fashion_mnist_data = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist_data.load_data()
In [13]:
# Print the shape of the training data
train_images.shape
Out[13]:
In [14]:
# Define the labels
labels = [
'T-shirt/top',
'Trouser',
'Pullover',
'Dress',
'Coat',
'Sandal',
'Shirt',
'Sneaker',
'Bag',
'Ankle boot'
]
In [15]:
train_labels[0]
Out[15]:
In [16]:
# Rescale the image values so that they lie in between 0 and 1.
train_images = train_images / 255.
test_images = test_images / 255.
In [17]:
# Display one of the images
i = 0
img = train_images[i]
plt.imshow(img)
plt.show()
print(f"label: {labels[train_labels[i]]}")
In [18]:
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D,Dense,Flatten
In [19]:
# create model
model = Sequential([
Conv2D(16, (3,3), input_shape=(28, 28, 1), activation="relu"),
MaxPool2D((3,3)),
Flatten(),
Dense(10, activation="softmax")
])
In [20]:
# complie model
opt = tf.keras.optimizers.Adam(learning_rate=0.005)
acc = tf.keras.metrics.SparseCategoricalAccuracy()
mae = tf.keras.metrics.MeanAbsoluteError()
model.compile(optimizer=opt,
loss='sparse_categorical_crossentropy',
metrics=[acc, mae]
)
Fit the model¶
In [21]:
# Fit the model
history = model.fit(train_images[...,np.newaxis], train_labels, epochs=8, batch_size=256, verbose=2)
Plot training history¶
In [22]:
# Load the history into a pandas Dataframe
df = pd.DataFrame(history.history)
df.head()
Out[22]:
In [23]:
# Make a plot for the loss
loss_plot = df.plot(y="loss", title= "Loss vs. Epochs", legend=False)
loss_plot.set(xlabel="Epochs", ylabel="Loss")
Out[23]:
In [27]:
df = pd.DataFrame(history.history)
df
Out[27]:
In [30]:
# Make a plot for the accuracy
acc_plot = df.plot(y="sparse_categorical_accuracy", title="acc vs. Epochs",legend=False)
acc_plot.set(xlabel="Epochs", ylabel="Acc")
Out[30]:
In [32]:
# Make a plot for the additional metric
mae_plot = df.plot(y="mean_absolute_error", title="mae vs. Epochs")
mae_plot.set(xlabel="Epochs", ylabel="mae")
Out[32]:
In [33]:
import matplotlib.pyplot as plt
import numpy as np
Evaluate the model on the test set¶
In [37]:
# Evaluate the model
model.evaluate(test_images[...,np.newaxis], test_labels, verbose=2)
Out[37]:
Make predictions from the model¶
In [59]:
# Choose a random test image
random_inx = np.random.choice(test_images.shape[0])
print(random_inx)
test_image = test_images[random_inx]
plt.imshow(test_image)
plt.show()
print(f"Label: {labels[test_labels[random_inx]]}")
In [60]:
# Get the model predictions
prediction = model.predict(test_image[np.newaxis,...,np.newaxis])
print(f"Prediction Label: {labels[np.argmax(prediction)]}")