A few days ago I was interested in studying Tensorflow, and in my study I came across a simple example that made me understand the capabilities of Tensorflow as a tool to predict something. In this article I will demonstrate a simple prediction implementation using Tensorflow.
Let's start with what is a prediction? In my opinion prediction is a statement about an event that has not happened and may occur in the future. A prediction can usually be made based on the knowledge or experience we have.
Let's look at a prediction with a mathematical context.
Suppose:
X = 1, 2, 3, 4, 5, 6, 7
y = 1, 2, 3, 4, 5, 6, 7
What is the relationship between X
and y
?
Of course the relationship between the two variables is x = y
. So we can conclude that if x
is equal to 8, then we can predict that y
will also be equal to 8.
Let's see another example. Suppose:
X = 1, 2, 3, 4, 5, 6, 7
y = 4, 7, 10, 13, 16, 19, 22
What is the relationship between X
and y
? And can you predict the value of y
given x
is 8?
When compared to the initial example, this example is a bit more challenging. Sufficient analysis is needed to determine the relationship between the two variables. The relationship between the two variables is 3x+1
, which means if the value of x
is 8, we can predict that the value of y
is 25.
Well, what if the relationship between the two variables is very complicated and there are millions of numbers? The task of analyzing these relationships will surely become more difficult. But fortunately Tensorflow can help us to analyze the relationship of these variables.
As I said above that Tensorflow can help us analyze and make predictions. We will try to make a simple implementation to do this.
To follow this tutorial, you must first create a python environment with tensorflow, hard, and numpy already installed there. Or a better option is to use Google Colab because tensorflow, hard, and numpy are already installed there.
Source code 1:
import tensorflow as tf
from tensorflow import keras
import numpy as np
The first step, we will import the required libraries, as I can see in Source code 1 above.
Source code 2:
X = np.array([1, 2, 3, 4, 5, 6, 7])
y = np.array([4, 7, 10, 13, 16, 19, 22])
Next we will define the X
and y
variables, as in Source code 2.
Source code 3:
model = keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
model.compile(optimizer='sgd', loss='mean_squared_error')
After that, we will define the model to perform the calculation. In this case, we will use one neuron unit as seen in the parameter units=1
.
Source code 4:
model.fit(X, y, epochs=500)
Finally, we can fit variable X
and y
to the model as seen in Source code 4 and try to predict the value of y
given x
as shown in Source code 5.
Source code 5:
print(model.predict([8]))
Output 1 shows the result of Source code 5. Take note that the value produced is not in the form of integer instead of floating point but of you can round it into an Integer.
Output 1:
[[25.007504]]
The following is the full code of this implementation: https://github.com/luthfisauqi17/simple-prediction-implementation-using-tensorflow/blob/main/Simple_Prediction_Implementation_Using_Tensorflow.ipynb
That's my little explanation of what prediction is and how to implement a simple prediction using Tensorflow. Hope this article can help you or maybe get some new insight about prediction and Tensorflow library.
Note that Artificial Intelligence is not really my forte, I will try to learn it and document what I have learned in my articles. Feel free to comment on those of you who think I might be wrong at some point and if there's something I can improve.
Cover image: https://i.picsum.photos/id/6/1920/720.jpg?hmac=M6g-CIj3LNu1zPsoHojcrOHo24qXbLmyuIUwrELe9XQ
Top comments (0)