Understanding TensorFlow Architecture

This Guide gives you an overview of TensorFlow architecture which helps you in TensorFlow programming and think of what’s going on under the hood. You must have a solid understanding of its architecture to write high computational algorithms using the TensorFlow framework.

What is TensorFlow

TensorFlow is an open-source machine learning library that is developed by Google. Below are the 3 main requirements which are fulfilled by this machine learning framework.

  • Performance – This framework is able to handle very large about of data and it’s computation by using CPU, GPU or TPU units.
  • Flexibility – This framework has enough capability to implement on-going research and new powerful machine learning algorithms to implement state-of-art artificial intelligence techniques.
  • Portability – And It is well organized for continuous development for application scaling and deploy the application on various devices e.g mobiles, embedded devices, and a web server.

Core Concept of TensorFlow

The Core of TensorFlow is written in C++. The core concept is to separate the design of data workflow to the actual data. So first, we build a data flow graph and then stream the data into the graph. Each node in the graph represents a mathematical operation e.g Activation Method ReLu. And edges of the graph represent a multi-dimensional array. These multi-dimensional array are known as Tensor.

Throughout your development, Keep in mind that TensorFlow uses Graph concepts. Let’s dig into the main phases in a machine learning implementation using TensorFlow.

Load Data

The tf.data API is used to load the data and data pre-processing. We create data input pipelines to be used in the training of the model. These pipelines make it easy to handle large amounts of data without worrying about memory issues and these support many types of data formats. e.g Images, text.

Model Training

The tf.keras API is used to train the model, evaluate the performance of the model on the test data and build the model. This is a widely used high-level API in TensorFlow development and it’s very user-friendly as well. If you’re a python developer then use Keres.io for the same purpose.

The alternative of Keras is Estimators in the TensorFlow. These Estimators API empowers you to build complex machine learning models by utilizing low-level APIs of TensorFlow. You can use premade estimators e.g linear regression, logistic regression or write your own estimator.

Computing Device

Machine learning algorithms require complex computations so it needs high computation power. TensorFlow enables you to code the model from independent to the hardware used for the computation. You can use CPU for simple algorithms e.g linear regression or GPU for the large neural network for image recognition. You can switch from CPU to GPU if needed without changing any model training programming.

It’s super easy to distribute the training workload to multiple machines. e.g multiple CPU, GPU or TPU devices.

Deploy Model

Once a mode is ready, It needs to be deployed to be used in the real application. E.g if you’re building a prediction application then you can use the linear regression model in your application.

TensorFlow provides different deployment libraries according to the production environment. Below are the main deployment libraries:

  • TensorFlow Serving: It allows us to deploy the model on the cloud server and access the model using the REST API.
  • TensorFlow Lite: You can use this to deploy the model into mobile or embedded devices.
  • TensorFlow JS: It allows us to deploy the model on the webserver. e.g Node.js development environment.

So I have given you an easy to understand overview of Tensorflow which can motivate you to use TensorFlow as your preferred machine learning framework.

Solving Regression Problems Using Neural Network

This tutorial explains solving regression problems using a neural network approach instead of using Supervised Machine Learning Algorithm. You’ll be able to solve simple regression problems at the end of this tutorial.

We’ll use Jupyter Notebook to write the code and TensorFlow as our machine learning framework. Our guide about TensorFlow Architecture will help you to understand its working architecture.

In this Tutorial:

  • Setup Notebook
  • Load Dataset
  • Data Preprocessing
  • Build Model
  • Train Model
  • Make Prediction

Setup Notebook

Use Setup Kaggle Notebook guide to create a notebook on kaggle.com and give it a name Simple Linear Regression Using Neural Network and Add Simple Linear Regression dataset to the project.

So we’ll do all the coding in this notebook. You can remove the default code and add the required libraries to be used in this project.

import tensorflow as tf
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split

Load Data

We have added the Simple Linear Regression dataset in the previous step. Use the below code to load the data from the CSV file to the pandas data frame and show the top 5 rows from the data frame.

housing_data_df = pd.read_csv('../input/simple-linear-regression/kc_house_data.csv')
housing_data_df.head()

Data Preprocessing

In this problem, we’re estimating House Price based on various properties of the house. e.g number of bedrooms, bathrooms, sqft area, etc. So the Price column is our target column and rest are features to be used in the training process.

features = housing_data_df[['bedrooms','bathrooms','sqft_living','sqft_lot','floors','yr_built']]
target = housing_data_df[['price']]

Though we’re using bedrooms, bathrooms, sqft_living,sqft_lot, floors, and yr_build columns as main features. You can try with different columns in your experiment.

And then split the datasets into training and test dataset using the train_test_split method.

train_features,test_features,train_target,test_target = train_test_split(features,target)

So train_features and train_target will be used in the training process and test_features and test_target will be used for prediction and evaluation purposes.

Build Model

We’ll use the Sequential model with two dense layers, and one output layer that returns a value, House Price.

no_features_columns = len(train_features.keys())
no_target_columns = 1
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(32,activation='relu',input_shape=[no_features_columns]),
    tf.keras.layers.Dense(16,activation='relu'),
    tf.keras.layers.Dense(no_target_columns)
])

And then compile the model.

model.compile(optimizer=tf.keras.optimizers.RMSprop(),
             loss='mean_squared_error',
             metrics=['mean_squared_error'])

Train Model

Train the model for 10 epoch using train_features and train_target.

history = model.fit(train_features,train_target,epochs=10)

And you will see each iteration log with mean_squared_error values.

As you noted, mean_squared_error is very high because our model is not optimized. We use hyper optimization tunning techniques to solve this issue but this is out of the scope for this tutorial.

Make Prediction

To test our trained model, we’ll use the test dataset which is unseen data for the model. And create a data frame to compare predicted values with the actual values and show the top 10 results.

test_predictions = model.predict(test_features).flatten()
results_df = pd.DataFrame({'Predicted Price': test_predictions,'Actual Price':test_target.values.flatten()})
results_df.head(10)

So this tutorial explained the build process for simple neural network using TensorFlow to solve a regression problem.