ML model building

Jyothi
5 min readApr 30, 2024

Following are the basic steps in building an ML model using colab notes:

Site: colab.research.google.com

1. Using tensorflow in Python:

import tensorflow as tf

tf.config.list_physical_devices(‘GPU’)

TensorFlow provides a comprehensive ecosystem of tools, libraries, and resources for building and deploying machine learning and deep learning models. TensorFlow provides a comprehensive ecosystem of tools, libraries, and resources for building and deploying machine learning and deep learning models. TensorFlow is designed to scale from individual devices like smartphones and IoT devices to large-scale distributed systems with hundreds of GPUs. TensorFlow provides high-level APIs like Keras, tf.estimator, and tf.data, which make it easy to build, train, and deploy machine learning models.

2. Loading existing dataset:

fashion_mnist = tf.keras.datasets.fashion_mnist

(train_images, train_labels), (valid_images, valid_labels) = fashion_mnist.load_data()

It is a dataset of grayscale images of various clothing items, each belonging to one of 10 categories. It is commonly used for practicing image classification tasks. The dataset contains 60,000 training images (train_images) and 10,000 test images (valid_images), each of size 28x28 pixels. The images are low-resolution and depict individual clothing items such as shirts, dresses, sneakers, and more. This dataset is often used as a benchmark for training and evaluating machine learning models.

3. Using plotting library

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()

matplotlib.pyplot is a module within the matplotlib library that provides a MATLAB-like plotting framework in Python. It is commonly used for creating visualizations such as line plots, scatter plots, histograms, and more.

plt.figure(): Creates a new figure for plotting.

plt.imshow(valid_images[data_idx], cmap=’gray’): Displays the image at index data_idx from the valid_images dataset. The cmap=’gray’ argument specifies that the colormap used should be grayscale, suitable for displaying grayscale images.

plt.colorbar(): Adds a colorbar to the plot, providing a scale for the colors used in the image.

plt.grid(False): Turns off the grid lines in the plot.

plt.show(): Displays the plot.

4. Building a model

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)])

model.summary()

tf.keras.utils.plot_model(model, show_shapes=True)

Keras, a deep learning framework that has been integrated into TensorFlow, makes such a model easy to build. This code segment uses Sequential API, which allows us to stack layers, the list of operations we will be applying to our data as it is fed through the network.

· tf.keras.Sequential: This function creates a sequential model, which is a linear stack of layers. In this case, the layers are defined sequentially, and the data flows through them from input to output. In this case we have 2 layers.

· tf.keras.layers.Flatten: This layer flattens the input, which is typically a multi-dimensional array (such as an image), into a one-dimensional array. It’s often used as the input layer in neural networks for image classification tasks.

· tf.keras.layers.Dense: This layer represents a fully connected (or dense) layer in the neural network. Each neuron in this layer is connected to every neuron in the previous layer.

· model.summary(): This will display a summary of the model architecture, including the number of parameters in each layer and the total number of parameters in the model. It’s a useful tool for understanding the structure of your neural network and ensuring that it’s configured as intended.

· tf.keras.utils.plot_model: This will generate a visual representation of the model architecture, including the input shape, the layers, and the connections between them. Setting show_shapes=True will display the shapes of the tensors at each layer in the plot. This can help you verify that the model is constructed correctly.

5. Initiate training

model.compile(optimizer=’adam’,

loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),

metrics=[‘accuracy’])

The compile method in TensorFlow’s Keras API configures the model for training. The compile method prepares the model for training by specifying the optimizer, loss function, and evaluation metrics to be used.

· optimizer=’adam’: This specifies the optimizer to be used during training. In this case, the Adam optimizer is chosen, which is a popular optimization algorithm known for its efficiency and effectiveness in training deep neural networks.

· loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True): This specifies the loss function to be used during training. The Sparse Categorical Crossentropy loss function is commonly used for classification problems where the target labels are integers (e.g., 0, 1, 2). Setting from_logits=True indicates that the model’s output is not normalized (i.e., logits) and needs to be converted to probabilities internally.

· metrics=[‘accuracy’]: This specifies the evaluation metric(s) to be used during training and testing. In this case, accuracy is chosen as the metric, which measures the proportion of correctly classified samples.

6. Training the model

history = model.fit( train_images, train_labels, epochs=5, verbose=True,

validation_data=(valid_images, valid_labels))

The fit method in TensorFlow’s Keras API is used to train the model on a given dataset. The fit method trains the model on the provided training data, using the specified number of epochs and validation data, and returns a history object containing training metrics.

· train_images and train_labels: These are the input training data (images) and corresponding target labels. The model will be trained to learn a mapping between the input images and their respective labels.

· epochs=5: This specifies the number of epochs (iterations over the entire dataset) for which the model will be trained. In this case, the model will be trained for 5 epochs.

· verbose=True: This controls the verbosity of the training process. Setting verbose=True means that training progress will be displayed during training.

· validation_data=(valid_images, valid_labels): This specifies the validation dataset to be used during training. The model’s performance on this dataset will be evaluated after each epoch to monitor its generalization ability and prevent overfitting.

After training completes, the fit method returns a history object, which contains information about the training process such as training/validation loss and metrics over each epoch. This history object can be used for further analysis and visualization of the training results.

7. Prediction

model.predict(train_images[0:10])

The predict method in TensorFlow’s Keras API is used to generate predictions from a trained model. This code segment generates predictions for the selected images using the trained model. The output predictions will contain the model’s predicted outputs for the input images. Depending on the problem you’re solving, these predictions might represent class probabilities, regression values, or any other type of output generated by your model. You can then use these predictions for further analysis or evaluation of the model’s performance.

This predict method gives the raw results and need some formatting to be interpreted by the average human, so below code segment is used to display an image to be classified as well as graph the results of each of our output neurons.

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])

--

--