DEV Community

Wesley Kambale
Wesley Kambale

Posted on • Originally published at kambale.dev on

A Guide to Machine Learning with TensorFlow

Prerequisites

Basic understanding of the Python programming language and knowledge of artificial intelligence concepts.

What is Machine Learning?

Machine learning (ML) is a subfield of artificial intelligence (AI) that enables computers to learn from data to make predictions and identify patterns. Computers traditionally rely on explicit programming. Machine learning algorithms can be divided into two main categories: supervised and unsupervised learning.

Supervised learning

Used when the training data includes labeled examples. The algorithm attempts to find the relationship between the input features (independent variables) and the output (dependent variable), which is known as the "ground truth". Once the relationship has been learned, the algorithm can use this knowledge to make predictions on new, unseen data. Common examples of supervised learning include classification (determining the class of an object based on its features) and regression (predicting a continuous value).

Unsupervised learning

Used when the training data is unlabeled. The algorithm must identify patterns and structure in the data on its own. Common examples of unsupervised learning include clustering (grouping similar data points) and dimensionality reduction (reducing the number of features in the data).

Why TensorFlow in Machine Learning?

TensorFlow (TF) is an open-source software library for machine learning and deep learning developed by the Google Brain team. It is used for implementing and deploying machine learning models, and it provides a comprehensive and flexible platform for developing deep learning applications by combining the computational algebra of optimization techniques for easy calculation of commonly used mathematical expressions.

Some of the important features of TensorFlow are:

  • The definition, optimization, and calculation of mathematical expressions easily with the help of multi-dimensional arrays called tensors.

  • A wide range of programming support (Python, C/C++, Java, R) of deep neural networks and machine learning techniques.

  • Highly scalable features for computation with various data sets both raw and pre-trained.

  • TensorFlow, as a cloud-based framework, comes with the power of GPU computing and automation management.

Installing TensorFlow

TensorFlow can be installed in Python easily, just like any other module, with a terminal command using pip, the package manager for Python. Open a terminal or command prompt and enter the following command:

pip install tensorflow

Enter fullscreen mode Exit fullscreen mode

Note: This general installation command may not work for all operating systems.

macOS

To install TF that is optimized for Apple's macOS processors (especially M1 and M2) without going through the troubles of using the general installation, the following command is used:

pip install tensorflow-macos

Enter fullscreen mode Exit fullscreen mode

Windows & Ubuntu without GPU

You can install the CPU version of TF on Windows & Ubuntu if you do not have an external GPU installed or wish to use the CPU:

pip install tensorflow-cpu

Enter fullscreen mode Exit fullscreen mode

Note: Training neural networks with CPUs has performance issues in comparison to powerful GPUs.

TensorFlow also provides a high-level API, called Keras, which can simplify the creation and training of machine learning models. You can install Keras using the following command:

pip install tensorflow-keras

Enter fullscreen mode Exit fullscreen mode

Training a Model in TensorFlow

Open Google Colab, create a new Notebook, and run through these basic steps of training a machine learning model using TensorFlow as follows:

Import the necessary libraries

These libraries might include matplotlib (for data visualization), scipy (for scientific and technical computing), numpy, pandas, and others.

import tensorflow as tf 
from sklearn import datasets
#Import the following libraries if you need them
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import scipy

Enter fullscreen mode Exit fullscreen mode

Load and preview the dataset

In this example, we'll use the iris dataset, which contains 150 samples of iris flowers with four features (sepal length, sepal width, petal length, and petal width) and three classes (setosa, versicolor, and virginica).

To load the dataset, run:

iris = datasets.load_iris() 
x = iris["data"] 
y = iris["target"]

Enter fullscreen mode Exit fullscreen mode

To print the last 10 rows of the dataset to preview what it contains, run:

print(data.tail(10))

Enter fullscreen mode Exit fullscreen mode

Split the dataset into training and test sets

The training set will be used to train the model, while the test set will be used to evaluate its performance.

from sklearn.model_selection import train_test_split 
x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)

Enter fullscreen mode Exit fullscreen mode

Preprocess the data

This can include normalizing the features and converting the output labels to one-hot encoding.

from sklearn.preprocessing import StandardScaler 
scaler = StandardScaler() 
x_train = scaler.fit_transform(x_train)
x_test = scaler.transform(x)

Enter fullscreen mode Exit fullscreen mode

Define the model

In TensorFlow, you can define a model using the Keras API. A model in Keras is defined as a sequence of layers, and you can choose from a variety of layer types, including dense (fully connected), convolutional, recurrent, and more. For this example, we'll use a simple fully connected (dense) model with three hidden layers and a softmax activation function in the output layer for the three-class classification problem.

model = tf.keras.models.Sequential([
  tf.keras.layers.Dense(32, activation='relu', input_shape=(4,)),
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dense(32, activation='relu'),
  tf.keras.layers.Dense(3, activation='softmax'),
])

Enter fullscreen mode Exit fullscreen mode

Compile the model

Before training, you need to compile the model by specifying the optimizer, loss function, and metrics to use. In this example, we'll use the Adam optimizer, categorical cross-entropy loss, and accuracy as the metric.

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

Enter fullscreen mode Exit fullscreen mode

Train the model

You can train the model using the fit method, which takes the training data and target values as arguments. You can also specify the batch size and the number of epochs (iterations over the training data).

model.fit(x_train, y_train, batch_size=32, epochs=100)

Enter fullscreen mode Exit fullscreen mode

Evaluate the model

After training, you can evaluate the model's performance on the test data using the evaluate method. This will return the loss and accuracy of the model on the test data.

test_loss, test_acc = model.evaluate(x_test, y_test)
print('Test Accuracy:', test_acc)

Enter fullscreen mode Exit fullscreen mode

Make predictions

You can use the trained model to make predictions on new, unseen data using the prediction method.

predictions = model.predict(x_test)

Enter fullscreen mode Exit fullscreen mode

After making predictions, there are several steps that can be taken:

  • Evaluate the performance of the model

  • Analyze the errors

  • Improve the model

  • Deploy the model

  • Monitor the performance

  • Iterate the process

Conclusion

We introduced the basics of machine learning and covered the steps to train a machine learning model using TensorFlow. TensorFlow is a powerful and flexible platform for developing machine learning models, and the high-level API, Keras, makes it easy to create and train models.

While we only covered the basics, TensorFlow offers a wide range of features and tools for developing more complex models. If you're interested in exploring further, there are many resources available, including the TensorFlow website, tutorials, and documentation.

Machine learning is a rapidly growing field with many exciting applications, and TensorFlow is a great tool for getting started in this area. Whether you're a seasoned developer or just starting, TensorFlow can help you take your machine learning skills to the next level.

Until we meet again. Same time, same place. Adios!

Top comments (0)