Tensorflow Cheatsheet

Gamze Yılan
8 min readApr 4, 2023

Learn TensorFlow from scratch and create your first Neural Network model in just 8 minutes!

Getting Started

Tensorflow is an open source library that is often used for machine learning purposes that allows developers create dataflow graphs.

Although Tensorflow works with many programming languages, it’s often preferred with Python. Hence, in this tutorial we’ll work with Python. If you have no knowledge of Python but some programming skills, worry not, I’m sure you’ll be able to follow along well. If you’d like to learn Python in literally 4 minutes though, click here.

Terminology

In order to start working with real data, we must first ensure that we know the basic concepts and terminology.

  • Artificial Neural Network (ANN): Computing system inspired by the human brain.
  • Perceptron (node): Artificial neuron. The smallest component of the artificial neural network. Takes one or more inputs and produces an output.
  • Weight: The parameter that transforms the input to get the required output.
  • Bias: The case that breaks the true input-output expectation, ie. the input of value 0 in a neural network that determines whether a number is positive or negative.
  • Layer: A building block of neurons in machine learning, ie. the block that takes inputs, the block that determines whether the input is positive or negative, the block that gives the outputs… Note that some layers are created by the developer, and some are created by the neural network to get the required output & the ones that are created by the neural network are hidden.
  • Deep network: An artificial neural network that has more than two hidden layers.

Activation Functions

The mathematical functions defined within Tensorflow that adjust the weight and the bias factors to get the correct outputs are called activation functions. There are four base types of activation functions:

  • Sigmoid Function: Returns a value between 0 and 1, is often used for classification problems.
  • Tanh Function: Returns a value between -1 and 1, offers a wider variety of negativa values & is often used for classification problems.
  • RelU (Rectified Linear Unit): Returns a value between 0 and infinity, is often used for deep learning problems.
  • Linear Function: f(x)=x, can take any value but the fact that it’s linear might cause problems with the models.

Regression

Regression is a method that implies the closeness of a value to the average within a data set. For example, you can create a graph of the mothers’ heights and the daughters’ heights and match the values to create a linear function. Then, you can enter a new mother’s height data to get an idea about how tall her daughter will be using that function. The function here is an actual equation and it’s called the regression function. Of course, the result will not be definite but rather approximate but it’s good enough in most cases.

Data Sets

Data sets you need to develop machine learning applications are classified as training sets and testing sets. We’re going to use the SKLearn library to classify our data set as either and then use these data sets with tensorflow to create our neurons and neural network.

Cost Function

The function that is used to compare the output of the neural network with the expected output and see how far we are from it. It’s used for minimizing the error.

Gradient Decent Function

The function that is used to find the minimum value that the cost function can get.

We’re trying to find the output with the minimum error value, in other words, the result of the cost function applied to the gradient decent function. There will be many outputs within the gradient function, and in order to make sure that we find the right output without accidently skipping it while running the function we must set a parameter named step size right. If the step size is too small, it’ll take too long to find the right output. If the step size is too big, we might accidentally skip the right output.

Preparing the Data

In order to start creating out neural network with Tensorflow, we need data. And not just any data, we need the data to be in a format readable by Tensorflow. Data could be any piece of information, and it is up to us to convert and edit it into a form where we can actually use it.

The most common data form that we’re going to come across is and excel sheet. In order to read the data from the excel sheet and run operations on it, we can use Python language -specifically the Pandas library. If you don’t know about Python or Pandas, don’t worry! I’ve got you covered. I’ll sum what you need for Tensorflow purposes.

Now, depending on your environment I’ll need you to install Python, Numpy, Pandas and Tensorflow. If you’re new to this I recommend you use Anaconda instead of direct installation to your system. That way you can simply install Anaconda and then run the commands “conda install numpy”, “conda install pandas” and “conda install tensorflow” in that order on your terminal. Then, create a directory for your project. Inside we should have three files: one named __init__.py, one named myfile.py and your excel data file -let’s say mydata.xlsx. You can create them by creating simple text files and then changing their name & extension manually. You can install the data file from the internet or create your own: a simple table with two to three columns and a bunch of rows will do.

The init file will remain empty. Inside the myfile.py, though, we’re going to import the pandas library and use a function defined in it to convert our excel file’s input into a structure called Data Frame -something similar to a matrix. You can do that by opening your myfile.py file in any text editor and typing the following code:

import pandas as pd

myData = pd.read_excel(“mydata.xlsx”) //assigns the created data frame to the variable named myData for future use

Now, there are two other libraries we can use in order to create beautiful data graphics: Matplotlib and Seaborn. We’re going to need these in order to turn the data frame into a graph that Tensorflow can work with. You can install them however you like depending on your system, with Anaconda simply by running “conda install matplotlib” and “conda install seaborn” on your local terminal. In the given order, of course!

Then, we can start working with our data graph libraries by adding the following lines to our myfile.py file:

import seaborn as sbn

import matplotlib.pyplot as plt

Now that we have the data, we need to separate it into two data sets: one for training and one for testing. We can do that via the train_test_split function from the sklearn library. Let’s start by importing the function:

from sklearn.model_selection import train_test_split

Now this function takes a minimum of four parameters and the order matters. The first two parameters must be the input array and the output array -make sure that these arrays are in the form of numpy arrays. Then, the third parameter is the test size and stands for the proportion of the dataset to include in the test split. Then, the last parameter called random state controls the shuffling applied to the data before applying the split.

Let’s assume we’re trying to foresee someone’s height by using their parents’ height data. We have an excel file with columns motherHeight, fatherHeight and childHeight that we imported as shown above. So in a nutshell, we can use this function and create our test and train data sets as:

x = myData[“motherHeight”, “fatherHeight”].values

y = myData[“childHeight”].values

x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.33, random_state=15)

This will take about %33 of the data to create the x_test, and the remaining data to create x_train arrays. The same applies for the y array.

Sometimes the data might be a bit too rough to process. In this example, let’s assume we have the heights like 172.3, 191.2. We can process them and match them with a corresponding value between 0 and 1 to work with them easily before we actually feed them to the tensorflow. We can do so via the MinMaxScaler function defined within sklearn as:

from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()

scaler.fit(x_train)

Creating the Model

Now we can create the model and the layers. For that, we’ll use Sequential and Dense from Keras, a library that was outsourced in tensorflow v1, but is included within the current version so there’s no need to install.

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense

model = Sequential() //creates an empty model

model.add(Dense(5, activation= “relu”)) // adds one layer to the model, in it creates 5 neaurons and uses the Rectified Linear Unit function

model.add(Dense(3, activation= “relu”)) // adds another layer to the model

model.add(Dense(1)) // creates the output layer with a single neuron in it

mode.compile(optimizer = “rmsprop”, loss= “mse”) // compiles and creates what we’ve defined above, using the rmsprop algorithm. Keep in mind that most popular optimizer algorithm is adam. The second parameter stands for the regression algorithm to calculate the average of errors. Here, we’re using the mean squared error -mse- regression.

Training the Model

Now that we’ve prepared the data and created the model, we can finally start training it.

Each time we go through the dataset is called an epoch. If we don’t go through it enough, we’ll end up with wrong results. If we go through it too much, the neurons will just program themselves to give the right result instead of creating the right weights and biases — this is called overfitting. Hence, it’s important to ensure balance here.

You can train the model using fit() method as:

model.fit(x_train, y_train, epochs=250)

And voila! We’ve trained our model. Now in order to see if our model is actually working correctly, we should use the evaluate() function on both train and test data. This method will return the loss rate to us, which we can compare with another. If the loss rate is close on both train data evaluation and the test data evaluation, we can assume that our model will work successfully.

trainLoss = model.evaluate(x_train, y_train, verbose=0)

testLoss = model.evaluate(x_test, y_test, verbose=0)

Using the Model

We can now start using the model via the predict() method. This method will take input data as a parameter and give us the output data. For example, if we were to feed this method with our y_test data from before, we would expect it to return the x_test data.

testPredictions = model.predict(y_test)

So let’s try to predict a child’s height:

motherFatherHeightData = [[170, 180]]

motherFatherHeightData = scaler.transform(motherFatherHeightData)

model.predict(motherFatherHeightData)

Saving the Model

Now that we’re done we can save the model. Of course the model will remain if we save the python file, but it’s also a good option to save an abstracted version of the model itself alone and call to use in other projects. We can do that via save() method from Keras.

from tensorflow.keras.models import load_model;

model.save(“our_model_name.h5”)

newModel = load_model(“our_model_name.h5”)

--

--