Tensorflow: Machine Learning Patterns

0

Machine learning is all about a computer learning the patterns that distinguish things. In this article, we will show how that works by taking a look at a set of numbers and seeing if you can determine the pattern between them. The numbers are as follows:

X = 0, Y = -1

X = 1, Y = 1

X = 2, Y = 3

X = 3, Y = 5

X = 4, Y = 7

Can you spot the pattern between X and Y? The answer is Y = 2X - 1. So, whenever you see a Y, it's twice the corresponding X minus 1.

If you figured it out for yourself, well done! But how did you do that? How would you think you could figure this out?

Maybe you noticed that the Y increases by 2 every time the X increases by 1. So, it probably looks like Y = 2X + or - something. Then, when you saw X = 0 and Y = -1, you might have thought that the "something" is a -1, so the answer might be Y = 2X - 1. You probably tried that out with a couple of other values and saw that it fits.

Now, let's take a look at the code that we can use to solve this problem.

import tensorflow as tf


model = tf.keras.Sequential([

    tf.keras.layers.Dense(1, input_shape=(1,))

])


model.compile(loss='mse', optimizer='sgd')


X = np.array([0, 1, 2, 3, 4])

Y = np.array([-1, 1, 3, 5, 7])


model.fit(X, Y, epochs=500)


print(model.predict([10]))

This code will first create a simple neural network with one neuron. Then, it will train the neural network on the data that we provided. Finally, it will print the prediction of the neural network for the value X = 10.

If you run this code, you should see that the neural network predicts a value of 18.99 for X = 10. This is very close to the actual value of Y = 19, which we know is the correct answer.

The reason why the neural network doesn't predict the exact value of 19 is because it was only trained on a small amount of data. If we had more data, the neural network would be able to learn the pattern more accurately and would be able to predict the exact value of 19.

Tags

Post a Comment

0Comments
Post a Comment (0)