Machine learning (ML) provides systems the ability to automatically learn and improve from experience without being explicitly programmed. It is seen as a subset of Artificial Intelligence (AI).
There are two stages of ML and they are training and inference (prediction):
Traditionally, there are three ML approaches and they are as follows:
Now you should know what machine learning is and what are its approaches. Let’s start by writing our first ML model to infer celsius to fahrenheit using the Supervised learning approach.
Let’s say we have a dataset as follows which maps the celsius to fahrenheit
Celsius (X) |
Farenheit (y) |
---|---|
-40 |
-40 |
-10 |
14 |
0 |
32 |
8 |
46 |
15 |
59 |
22 |
72 |
38 |
100 |
Now we have the sample dataset, let’s start by writing Python code to infer fahrenheit from celsius using single layer Neural Networks.
Let’s start by importing dependencies:
import tensorflow as tf
import numpy as np
After importing the dependencies, initialize the input and output.
X = np.array([-40, -10, 0, 8, 15, 22, 38], dtype=float)
y = np.array([-40, 14, 32, 46, 59, 72, 100], dtype=float)
Now we have the input/feature and output/label pairs, we initialize the single layer neural network using the Dense layer from the tensorflow and wrapping it in the Sequential class that is responsible for grouping the layers:
model = tf.keras.Sequential([
tf.keras.layers.Dense(input_shape=[1], units=1)
])
The visualization of the above neural network is as follows:
The equation of the above neural network can be compared to that of the equation of the celcius to farenheit as follows:
Once we have our neural network initialized, let’s configure our model for training. The compile method of the Model class is responsible for defining loss function, optimizer (ADAM in our case), and metrics:
model.compile(
loss='mean_squared_error',
optimizer=tf.keras.optimizers.Adam(0.1)
)
We have data and neural network setup, now we’ll train the model using the fit method with 500 epochs. The Epochs defines the number of times our model is going to be trained on the training dataset.
model.fit(X, y, epochs=500, verbose=False)
After training the model we’ll infer from new data:
print(model.predict([98, 55]))
Output:
[[208.14742]
[130.82669]]
Now, let’s check the weights of the neural network by using the get_weights() method as follows:
print(model.get_weights())
Output:
[array([[1.7981565]], dtype=float32), array([31.928087], dtype=float32)]
As you can see from the above result, the weight and bias of the trained model is closer to the value of the celsius to fahrenheit formula. The weight is 1.7981565, and bias is 31.928087 which is closer to 1.8 and 32 respectively.
This blog post should familiarize you with the very basics of writing ML programs and understanding what goes behind the scenes of learning the model.
In order to give you better service we use cookies. By continuing to use our website, you agree to the use of cookies as described in our Privacy Policy