A Guide to Machine Learning with TensorFlow
Published: 2023-05-08 19:17:17

---
title: A Guide to Machine Learning with TensorFlow
published: true
date: 2023-02-06 22:30:04 UTC
tags: 
canonical_url: https://kambale.dev/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:

```bash
pip install tensorflow

```

_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:

```brew
pip install tensorflow-macos

```

### **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:

```bash
pip install tensorflow-cpu

```

_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:

```bash
pip install tensorflow-keras

```

## **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.

```python
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

```

### 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:

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

```

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

```python
print(data.tail(10))

```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1675772772135/e7af546a-23e6-490c-b304-60db60d840e3.png)

### 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.

```python
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)

```

### Preprocess the data

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

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

```

### 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.

```python
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'),
])

```

### 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.

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

```

### 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).

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

```

### 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.

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

```

### Make predictions

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

```python
predictions = model.predict(x_test)

```

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!_