Hello World Program in Machine Learning

Everyone start programming with hello world program right !. We did in our collage time when start learning c, c++.

Similarly here the Hello World program in ML (Machine Learning). That might give you better clarity of ML work.

In this program we first we infer the relationship between data manually then same with ML.

Lets example we have a data

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

Y = -2, 1, 4, 7, 10, 13

As i found relation in x data is: x = x + 1, in y data y = y + 3. So now we need to find relation between x and y, so y = 3x+1.

  • y = 3*0 + 1 = 1
  • y = 3*1 + 1 = 4
  • y = 3*2 + 1 = 7
  • so on.

So manually we found the relationship between data. Next do it via ML.

This we will achieve via neuron network via TensorFlow and numpy.

import tensorflow as tf
import numpy as np
from tensorflow import keras

# define neural network with one layer and one neuron and only one value.
model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])

# loss and optimizer functions
# loss: Measure guessed answer against the correct answer.
# optimize: make another guess based upon loss function result and minimize the loss.
model.compile(optimizer='sgd', loss='mean_squared_error')

#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)
xs = np.array(np.arange(-1, 10, 1), dtype=float)
ys = np.array(np.arange(-2, 30, 3), dtype=float)

# training a model to learn relationship between x's and y's.
model.fit(xs, ys, epochs=10)

print(model.predict([10.0]))

As per our manual understanding, if x = 10 when what would be y value. As per relation we found y = 3x+1, y would be 31. But ML not work on exact value, it always work on probability. More accuracy we need more data.

Hello World Program in Machine Learning
Show Buttons
Hide Buttons