Home » Code with Me » Machine Learning » Day 1 – Introduction to TensorFlow

# Day 1 – Introduction to TensorFlow

So I decided to take up the #100DaysOfCode challenge on Twitter. Doing this mainly because I need the motivation to get started with my Final Year Project. I’m using Google Colab to try out the code and you can check my GitHub repository for updates. You can check out my blog to read various articles related to life, design, and tech. Let’s begin!

### What is Machine Learning?

As humans, we are capable of learning from our past experiences and improving accordingly. As an example say I want to learn how to drive. In the beginning, I will start looking at the past experiences of others and learn how to drive. Gradually I will become better as I keep learning from my own experiences as well. I’ll learn how to control speed, drive safely and avoid accidents with time.

Similarly, computers or machines can also learn from past data with our instructions. When past data is fed to a machine learning algorithm, it’ll be trained and a prediction model will be built. When this machine receives new data it’ll process it and predict the output.

### Day 1 – Introduction to TensorFlow

Let’s consider two sets of numbers where;

x = -1, 0, 1, 2, 3, 4, 5

y= -5, -2, 1, 4, 7, 10, 13

You might figure out that there’s a pattern with the two number sets which is y = 3x – 2. You can use machine learning with TensorFlow to find the actual relationship between x and y.

To get started you will have to either install Python and other related packages for Tensorflow on your developer box using the command line or using any popular IDE (integrated development environment) or you can use Google Colab to access TensorFlow code with a cloud-based backend in your browser.

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

# method 1
model = Sequential([Dense(units=1, input_shape=)])

# or method 2
model = Sequential()

model.compile(optimizer='sgd', loss='mean_squared_error')

x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
y = np.array([-5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)

model.fit(x, y, epochs=500)

print(model.predict([10.0]))``````

The above code is a simple example of how a model is trained and used for predictions. I have imported the required modules to build the model using a neural network with TensorFlow Keras API.

When using TensorFlow you use Sequential to define the number of layers in the neural network. Inside it, you can specify what each layer looks like. To define the type of the layer we use keras.layers API. I will discuss the different types of layers later in the series.

Our Dense layer has units=1 specified, so we have just one dense layer with one neuron in our entire neural network. Next, you have to specify what the shape of the input data is. Here input data is our X, which is just a single value.

Therefore the first line of code defines the simplest possible neural network which contains only one layer and one neuron.

Since the computer is making a guess here to identify the relationship between x and y we use a loss function, loss=’mean_squared_error’. This measures how good or bad the guess is. The loss function will compare its answers to the already available data and make guesses about the new instances.

An optimizer is used to improve the machine learning model and minimize the loss by bringing the guessed formula closer to the correct answer.

With model.fit(x, y, epochs=500) the learning process will begin, It can be read as ‘fit the Xs to the Ys, and try it 500 times.’ In each iteration, the computer will guess the relationship, feed it to the optimizer and repeat it until the loss or the error decreases.

``````# first three lines
Epoch 1/500
1/1 [==============================] - 0s 252ms/step - loss: 24.2956
Epoch 2/500
1/1 [==============================] - 0s 8ms/step - loss: 17.5651
Epoch 3/500
1/1 [==============================] - 0s 13ms/step - loss: 12.9195

#last three lines
Epoch 498/500
1/1 [==============================] - 0s 8ms/step - loss: 2.4898e-04
Epoch 499/500
1/1 [==============================] - 0s 10ms/step - loss: 2.4433e-04
Epoch 500/500
1/1 [==============================] - 0s 10ms/step - loss: 2.3977e-04
1/1 [==============================] - 0s 56ms/step
``````

From the above result, you can see that the loss started at 24.295 and has decreased to 2.397 x 10-4 which means the model has learned the pattern between x and y as y = 3x-2.

In the last line we are doing a prediction, to see the accuracy of our model. We are trying to predict the value of y when x is equal to 10. This might be a slightly different answer to you but this is what I got.

``````print(model.predict([10.0]))
[[27.964153]]``````

Here you can see it has picked a value very close to 28. You might have noticed earlier that our loss was not 0. It was a very small value so we should expect the answer to be slightly off by the actual value. Another reason for this is that we have only used 7 data items which means we need a large dataset to get very accurate results.

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

z = Dense(units=1, input_shape=)
model = Sequential([z])

model.compile(optimizer='sgd', loss='mean_squared_error')

x = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0, 5.0], dtype=float)
y = np.array([-5.0, -2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)

model.fit(x, y, epochs=500)

print(model.predict([10.0]))
print("Here is what I learned: {}".format(z.get_weights()))``````

In the above code, I have done a small change and created a variable z to hold the dense layer and after the model learns, I will print the values that the layers learned.

``Here is what I learned: [array([[2.9927597]], dtype=float32), array([-1.9727358], dtype=float32)]``

This means the relationship between x and y is y = 2.9927597x – 1.9727358 which is very close to our actual relationship y = 3x – 2.

That’s it for Day 1, let’s see how machines can identify patterns in pictures in our next chapter. Happy coding! 😃🔥