Post installing Tensorflow, you are enabled with power of neural networks to perform deep learning.
TensorFlow is an open-source end-to-end platform for Machine Learning
1. First thing first:
import tensorflow as tf
import numpy as np
from tensorflow import keras
2. Load model configurations
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])
It has 1 layer, and that layer has 1 neuron, and the input shape to it is just 1 value.
3. Compiling neural network
The model uses the optimizer function to make another guess. Based on the loss function's result, it will try to minimize the loss. The model will repeat this for the number of epochs which you will see shortly.
But first, here's how we tell it to use `mean squared error` for the loss and `stochastic gradient descent` (sgd) for the optimizer. You don't need to understand the math for these yet, but you can see that they work!
model.compile(optimizer='sgd', loss='mean_squared_error')
4. Defining or creating data of 2 random arrays
xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)
ys = np.array([-2.0, 1.0, 4.0, 7.0, 10.0, 13.0], dtype=float)
The algorithm will try and see the relationship between the 2 arrays and allow the prediction for any value which we input.
5. Fitting the model with 500 epochs
model.fit(xs, ys, epochs=500)
An epoch is a term used in machine learning and indicates the number of passes of the entire training dataset the machine learning algorithm has completed. Datasets are usually grouped into batches (especially when the amount of data is very large)
6. Predicting
print(model.predict([10.0]))
You might have thought 31, right? But it ended up being a little over.
Neural networks deal with probabilities, so given the data that we fed the NN with, it calculated that there is a very high probability that the relationship between X and Y is Y=3X+1, but with only 6 data points we can't know for sure. As a result, the result for 10 is very close to 31, but not necessarily 31.
Thanks,
Happy coding