tensorflow and keras colab notebook

An Introduction to TensorFlow and Keras

Keras is a deep learning library that can run on TensorFlow among other platforms. TensorFlow is an end-to-end machine learning platform with Keras as one of its libraries. However, Keras can run on other ML platforms.

In a browser, opening a Colab notebook and writing the following commands imports some essential libraries to create and use a deep learning model.

import tensorflow as tf
from tensorflow import keras
from matplotlib import pyplot
import numpy as np

Matplotlib is useful in creating static, animated, and interactive visualizations in Python while NumPy; numerical Python is a linear algebra library for python

  1. Loading the data. TensorFlow keras has some common datasets that can be used for an introduction into deep learning. These include fashion MNIST which is a data set comprised of fashion items that include: shoes, coats, dresses etc in gray scale.
#loading the data
fashion_mnist  = keras.datasets.fashion_mnist
(X_train_full, y_train_full), (X_test, y_test) = fashion_mnist.load_data()
#creation of validation data
X_valid, X_train = X_train_full[:5000] / 255.0, X_train_full[5000:] / 255.0
y_valid, y_train = y_train_full[:5000], y_train_full[5000:]
#modifying test data to match the train and validation data
X_test, y_test = X_test/255.0, y_test

2. Plotting the data

Displaying a few images to get a feel of the data set. Plotting some of the data set in Pyplot. This requires defining the plot size as well as the number of items to be displayed.

for i in range(1,20):
	# define subplot
	
	pyplot.subplot(5,5,i) 
 
	
	# plot raw pixel data
	pyplot.imshow(X_train[i], cmap=pyplot.get_cmap('gray'))
# show the figure
pyplot.show()
Some contents of the fashion MNIST

3. Model creation

A sequential model has various layers defined in a logic manner. The flatten layer changes the 2D image input into a 1D array. The pixels are thus rearranged and lined up. The dense layer has each of the inputs i.e. the flattened values connected to the layers of the next layer. The last layer, softmax gives a probability output i.e. the probability of an input image belonging to one of the label class

model = keras.models.Sequential()
model.add(keras.layers.Flatten(input_shape=[28, 28]))
model.add(keras.layers.Dense(300, activation="relu"))
model.add(keras.layers.Dense(100, activation="relu"))
model.add(keras.layers.Dense(10, activation="softmax"))
the model

4. Model compilation and use

#Model compilation & fit
model.compile(loss="sparse_categorical_crossentropy",
 optimizer="sgd",
 metrics=["accuracy"])
#training the model
model.fit(X_train, y_train, epochs=10,validation_data=(X_valid, y_valid))
#making a prediction
predictions = model.predict(X_test)

The full Colab notebook is in the link below and should take one through creating a simple neural network and using it to make a prediction

https://colab.research.google.com/drive/1eLaX8c5c20XTu59LfvCSlXUi8wX0QM-W?usp=sharing

Leave a Reply

Your email address will not be published. Required fields are marked *

More Reading

Post navigation