Many decades ago, artificial neural networks were developed to mimic the learning capabilities of humans and animals. Below is an excerpt from The Machine that Changed the World, a 1992 documentary about Artificial Intelligence.
from IPython.display import YouTubeVideo
YouTubeVideo('cNxadbrN_aI')
Since then, computers and machine learning libraries have evolved dramatically. This notebook will demonstrate how neural networks have improved and the biological inspiration behind them.
We'll use TensorFlow, a popular open-source library that automatically detects GPUs for computation.
import tensorflow as tf
tf.config.list_physical_devices('GPU')
Data
We'll tackle image classification using the Fashion MNIST dataset:
fashion_mnist = tf.keras.datasets.fashion_mnist
(train_images, train_labels), (valid_images, valid_labels) = fashion_mnist.load_data()
Visualizing an image:
import matplotlib.pyplot as plt
data_idx = 42
plt.figure()
plt.imshow(train_images[data_idx], cmap='gray')
plt.colorbar()
plt.grid(False)
plt.show()
Categories:
Label | Description |
---|---|
0 | T-shirt/top |
1 | Trouser |
2 | Pullover |
3 | Dress |
4 | Coat |
5 | Sandal |
6 | Shirt |
7 | Sneaker |
8 | Bag |
9 | Ankle boot |
Check your guess:
train_labels[data_idx]
Building a Neuron
Neurons are basic neural network components inspired by biological neurons.
Architecture
Biological neurons transmit electrical impulses, while artificial neurons use simplified mathematics. The basic equation used is:
y = w0x0 + w1x1 + ... + b
Each image (28x28 pixels) results in 784 weights.
Model creation using Keras:
number_of_classes = train_labels.max() + 1
model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(number_of_classes)
])
Verify the model
model.summary()
Parameter calculation:
image_height, image_width = 28, 28
number_of_weights = image_height * image_width * number_of_classes
Initiating Training
Loss function and metrics setup:
model.compile(
optimizer='adam',
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=['accuracy']
)
Evaluating the Model
Training and validation:
history = model.fit(
train_images,
train_labels,
epochs=5,
verbose=True,
validation_data=(valid_images, valid_labels)
)
Prediction
Make predictions on new data:
predictions = model.predict(train_images[0:10])
Visualize predictions:
data_idx = 8675
plt.figure()
plt.imshow(train_images[data_idx], cmap='gray')
plt.colorbar()
plt.grid(False)
plt.show()
x_values = range(number_of_classes)
plt.figure()
plt.bar(x_values, model.predict(train_images[data_idx:data_idx+1]).flatten())
plt.xticks(range(10))
plt.show()
print("Correct answer:", train_labels[data_idx])
Congratulations! You've built a neural network capable of basic image classification.