Yayun Liu's Blog Engineer, Blogger, Learner, Runner

Deep Learning 3.1.1: What Is TensorFlow?

5 minutes to read
2024-05-18

Introduction

TensorFlow is an open source platform for machine learning and deep learning developed by Google. It provides a comprehensive ecosystem of tools, libraries, and community resources that lets researchers and developers build and deploy machine learning models at scale.

TensorFlow high-level architecture

TensorFlow high-level architecture

Training is the process of creating machine learning models. It involves feeding the model with input data and the corresponding labels, and adjusting the model’s parameters to minimize the difference between the predicted output and the actual output. For example, if you want a computer to recognize images of cats, you would train a model on a dataset of images of cats and non-cats.

Beyond creating models, TensorFlow also provides tools for serving and deploying models. Serving is the process of making trained models available for inference, which is the process of using a trained model to make predictions on new data. Deploying is the process of making trained models available to end users. TensorFlow provides APIs for serving, where you can provide model inference over an HTTP connection for cloud or web users.

For models to run on mobile devices, TensorFlow provides TensorFlow Lite, a lightweight version of TensorFlow that is optimized for mobile and embedded devices such as Raspberry Pi.

If you want to provide models to your browser, TensorFlow provides TensorFlow.js, a JavaScript library that allows you to run machine learning models in the browser or on Node.js.

Using TensorFlow

Installing TensorFlow in Python

pip install tensorflow-cpu

Using TensorFlow in Google Colab

Google Colab is a free cloud service that provides a Jupyter notebook environment. It comes with TensorFlow pre-installed. Colab provides GPU and TPU backends so you can train models using state-of-the-art hardware for free.

Google Colab

Getting started with TensorFlow

Here’s the code to create a simple neural network using TensorFlow:

import tensorflow as tf
import numpy as np
from tensorflow.keras import Sequential
from tensorflow.keras.layers import Dense

# Sequential is used to define layers. Inside the Sequential, you can define the layers of the neural network. 
#   In this example, we have only one layer `[Dense(units=1, input_shape=[1])]`.
# Dense is a layer type that connects every neuron in the previous layer to every neuron in the current layer.
#   The `units=1` argument specifies the number of neurons in the layer. In this case, we have only one neuron.
#   The `input_shape=[1]` argument specifies the shape of the input data. In this case, the input data is a single number.
model = Sequential([Dense(units=1, input_shape=[1])])

# The optimizer is the algorithm used to adjust the weights of the neural network during training to minimize the loss.
#   `sgd` stands for Stochastic Gradient Descent. It is a simple and widely used optimizer.
#   `loss='mean_squared_error'` specifies the loss function is the mean squared error. It is used to measure the difference between the predicted output and the actual output.
model.compile(optimizer='sgd', loss='mean_squared_error')

# Specify the input data and the corresponding output data. In this example, we are trying to learn the function y = 2x - 1.
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)

# The `fit` method is used to train the model on the input data and the corresponding output data.
model.fit(xs, ys, epochs=500)

# The `predict` method is used to make predictions on new data. In this example, we are predicting the output for x = 10.
print(model.predict([10.0]))

The illustration of the above neural network is shown below: Neural Network

Conclusion

This post explains TensorFlow, an open source platform for machine learning and deep learning developed by Google. TensorFlow provides a comprehensive ecosystem of tools, libraries, and community resources that lets researchers and developers build and deploy machine learning models at scale. TensorFlow provides tools for training, serving, and deploying models, as well as APIs for serving models over HTTP connections, TensorFlow Lite for mobile and embedded devices, and TensorFlow.js for running models in the browser or on Node.js. TensorFlow is widely used in the machine learning community and is a powerful tool for building and deploying machine learning models.

References

  • Chapter 1 “Introduction to TensorFlow”, “AI and Machine Learning for Coders: A Programmer’s Guide to Artificial Intelligence”, Laurence Moroney, 2021

Comments

Content