-
Imports:
import tensorflow as tf import numpy as np from tensorflow import keras print(tf.__version__)
-
Define and Compile the Neural Network:
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])]) model.compile(optimizer='sgd', loss='mean_squared_error')
-
Providing the Data:
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float) ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)
-
Training the Neural Network:
model.fit(xs, ys, epochs=500)
-
Making Predictions:
print(model.predict([10.0]))
Computer Vision Lab:
This code explores a computer vision example using the Fashion MNIST dataset, focusing on code components:
-
Importing TensorFlow:
import tensorflow as tf print(tf.__version__)
-
Loading and Exploring the Dataset:
(training_images, training_labels), (test_images, test_labels) = fmnist.load_data()
-
Data Preprocessing:
training_images = training_images / 255.0 test_images = test_images / 255.0
-
Building the Neural Network Model:
model = tf.keras.models.Sequential([tf.keras.layers.Flatten(), tf.keras.layers.Dense(128, activation=tf.nn.relu), tf.keras.layers.Dense(10, activation=tf.nn.softmax)])
-
Compiling and Training the Model:
model.compile(optimizer=tf.optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(training_images, training_labels, epochs=5)
-
Model Evaluation:
model.evaluate(test_images, test_labels)
Callbacks API Implementation:
This ungraded lab demonstrates the Callbacks API in TensorFlow, focusing on key code components:
-
Load and Normalize the Fashion MNIST Dataset:
import tensorflow as tf fmnist = tf.keras.datasets.fashion_mnist (x_train, y_train), (x_test, y_test) = fmnist.load_data() x_train, x_test = x_train / 255.0, x_test / 255.0
-
Creating a Callback Class:
class myCallback(tf.keras.callbacks.Callback): def on_epoch_end(self, epoch, logs={}): if(logs.get('loss') < 0.4): print("\nLoss is lower than 0.4 so cancelling training!") self.model.stop_training = True callbacks = myCallback()
-
Define and Compile the Model:
model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(input_shape=(28, 28)), tf.keras.layers.Dense(512, activation=tf.nn.relu), tf.keras.layers.Dense(10, activation=tf.nn.softmax) ]) model.compile(optimizer=tf.optimizers.Adam(), loss='sparse_categorical_crossentropy', metrics=['accuracy'])
-
Train the Model with Callback:
model.fit(x_train, y_train, epochs=10, callbacks=[callbacks])
-
Optional Challenge:
if(logs.get('accuracy') > 0.6): print("\nAccuracy exceeds 60%, stopping training!") self.model.stop_training = True
Improving Computer Vision Accuracy:
This lab explores the improvement of computer vision accuracy using shallow and convolutional neural networks:
Shallow Neural Network:
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation=tf.nn.relu),
tf.keras.layers.Dense(10, activation=tf.nn.softmax)
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=5)
test_loss = model.evaluate(test_images, test_labels)
Convolutional Neural Network:
model = tf.keras.models.Sequential([
tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(28, 28, 1)),
tf.keras.layers.MaxPooling2D(2, 2),
tf.keras.layers.Conv2D(32, (3,3), activation='relu'),
tf.keras.layers.MaxPooling2D(2,2),
tf.keras.layers.Flatten(),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dense(10, activation='softmax')
])
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
model.fit(training_images, training_labels, epochs=5)
test_loss = model.evaluate(test_images, test_labels)
Exploration of Convolutions:
This lab explores convolutions by creating a basic convolution on a 2D grayscale image:
Image Loading and Visualization:
from scipy.datasets import ascent
ascent_image = ascent()
import matplotlib.pyplot as plt
plt.grid(False)
plt.gray()
plt.axis('off')
plt.imshow(ascent_image)
plt.show()
Convolution and Max Pooling:
filter = [[0, 1, 0], [1, -4, 1], [0, 1, 0]]
weight = 1
# Convolution code...
# Visualization code...
# Max pooling code...