Creating a Neural Network to Invent the Conversion Formula from Hours to Minutes Using Python and TensorFlow

Tiempo de lectura: 3 minutos

Reading time: 3 minutes

Today I’m going to show you how to create a small neural network capable of predicting the conversion formula from hours to minutes.

To create this Artificial Intelligence, we’re going to use TensorFlow, a library created by Google that will make things easier for us. To install it, we run:

python3 -m pip install tensorflow

Now we create a new file and name it “speed.py”.

Let’s start by adding the dependencies:

import tensorflow as tf
import numpy as np

Now we create some sample training data. These may not be the best training data, but they will suffice for the example.

# training data: hours and minutes (output), supervised training
hours = np.array([1.0, 2.0, 3.0, 4.0, 4.5, 5.0, 10.0, 23.0, 15.0, 24.0, 1.5, 1.2,
                  3.3, 5.3, 100.0, 12.3, 3.4], dtype=float)
minutes = np.array([60.0, 120.0, 180.0, 240.0, 270.0, 300.0, 600.0, 1380.0, 900.0, 1440.0, 90.0, 72.0,
                   198.0, 318.0, 6000.0, 738.0, 204.0], dtype=float)

We create two arrays, one for hours and one for the equivalent in minutes. Since this is supervised training, we will train the system by providing input data (hours) and the corresponding expected output data (minutes).

If we want to improve the Neural Network, we will need to add more data. The more data we add, the better.

Now let’s create the learning model.

# Create the model, use 0.1 as the learning rate and mean squared error as the loss function
modelo.compile(
    optimizer=tf.keras.optimizers.Adam(0.1),
    loss='mean_squared_error'
)

First, we add the Adam optimizer with a learning rate of 0.1 (it increments the node indices by 0.1).

In addition, we use mean squared error as the loss function.

Now we need to train our Artificial Intelligence.

# Train the model
print("Training the model...")
modelo_entrenado = modelo.fit(horas, minutos, epochs=1000, verbose=False)
print("Model trained successfully")

Here we indicate that it should perform 1000 iterations on each value and not print the log (verbose=False).

Once trained, we can make predictions.

# Make a prediction
print("How many minutes are 9 hours?")
resultado = modelo.predict([9.0])
print("9 hours are " + str(resultado) + " minutes")

The complete code looks like this:

# Neural network that converts hours to minutes
import tensorflow as tf
import numpy as np

# Training data
hours = np.array([1.0, 2.0, 3.0, 4.0, 4.5, 5.0, 10.0, 23.0, 15.0, 24.0, 1.5, 1.2,
                  3.3, 5.3, 100.0, 12.3, 3.4], dtype=float)
minutes = np.array([60.0, 120.0, 180.0, 240.0, 270.0, 300.0, 600.0, 1380.0, 900.0, 1440.0, 90.0, 72.0,
                   198.0, 318.0, 6000.0, 738.0, 204.0], dtype=float)

# Create twoneurons: one for the input layer and one for the output layer.
layer = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([layer])
Create the model, use 0.1 as the learning rate and mean squared error as the loss function
model.compile(
optimizer=tf.keras.optimizers.Adam(0.1),
loss='mean_squared_error'
)
Train the model
print("Training the model...")
trained_model = model.fit(hours, minutes, epochs=1000, verbose=False)
print("Model trained successfully")
Make a prediction
print("How many minutes are 9 hours?")
result = model.predict([9.0])
print("9 hours are " + str(result) + " minutes")
Complete code:
# Neural network that converts hours to minutes
import tensorflow as tf
import numpy as np

# Training data
hours = np.array([1.0, 2.0, 3.0, 4.0, 4.5, 5.0, 10.0, 23.0, 15.0, 24.0, 1.5, 1.2,
                  3.3, 5.3, 100.0, 12.3, 3.4], dtype=float)
minutes = np.array([60.0, 120.0, 180.0, 240.0, 270.0, 300.0, 600.0, 1380.0, 900.0, 1440.0, 90.0, 72.0,
                   198.0, 318.0, 6000.0, 738.0, 204.0], dtype=float)

# Create two neurons: one for the input layer and one for the output layer
layer = tf.keras.layers.Dense(units=1, input_shape=[1])
model = tf.keras.Sequential([layer])

# Create the model, use 0.1 as the learning rate and mean squared error as the loss function
model.compile(
    optimizer=tf.keras.optimizers.Adam(0.1),
    loss='mean_squared_error'
)

# Train the model
print("Training the model...")
trained_model = model.fit(hours, minutes, epochs=1000, verbose=False)
print("Model trained successfully")

# Make a prediction
print("How many minutes are 9 hours?")
result = model.predict([9.0])
print("9 hours are " + str(result) + " minutes")

When executed, it predicts the following:

Our Neural Network predicts that 9 hours are 551.87854 minutes, but we know that in reality it is 540 minutes. It is close to the correct solution, but this demonstrates that it needs more training data.
Training data is the most important part of an artificial intelligence network. We need to find reliable and abundant data to achieve optimal results.

Leave a Comment