Applying simple Neural network to number pattern problems

Applying simple Neural network to number pattern problems

Most possibly, each of us has solved the problems that need us to determine the pattern between numbers. But can artificial intelligence solve these problems? Before thinking about it, let's try to find the answer to this sample question:

6 ? 17

9 ? 26

7 ? 20

14 ? 41

22 ? ?

I think the human brain is not programmed to find the answer to this question in the first ten seconds. Speaking of programming, it is worth noting that even if you are an ultra-programmer, traditional programming is helpless in the face of this type of problem. With traditional programming, the machine can't think like us, it doesn't understand whether there can be a connection between the numbers or not. Therefore, we must make the machine learn. Machine Learning!

This question made me think yesterday, and as you can see from the video, by building a very simple (consisting of one neuron) neural network in tensorflow, these 3 problems that we thought about for seconds, maybe minutes, were solved in just 9 seconds. Of course, time performance can be improved (by reducing the number of epochs), but keep in mind that we have very little data to train the model.

The simplest possible neural network is one that has only one neuron in it, and that's what this line of code does. In keras, I used the word dense to define a layer of connected neurons. There's only one dense here. So there's only one layer and there's only one unit in it, so it's a single neuron. Successive layers are defined in sequence, hence the word sequential. Then, I define the shape of what's input to the neural network in the first and in this case the only layer, and you can see that our input shape is super simple. It's just one value. You've probably seen that for machine learning, you need to know and use a lot of math, calculus probability and the like. It's really good to understand that as we want to optimize our models but the nice thing for now about TensorFlow and keras is that a lot of that math is implemented for you in functions. There are two function roles that you should be aware of though and these are loss functions and optimizers. This code defines them. I like to think about it this way. The neural network has no idea of the relationship between X and Y, so it makes a guess. Say it guesses Y equals 10X minus 10. It will then use the data that it knows about, that's the set of Xs and Ys that we've already seen to measure how good or how bad its guess was. The loss function measures this and then gives the data to the optimizer which figures out the next guess. So the optimizer thinks about how good or how badly the guess was done using the data from the loss function. Then the logic is that each guess should be better than the one before. As the guesses get better and better, an accuracy approaches 100 percent, the term convergence is used. In this case, the loss is mean squared error and the optimizer is SGD which stands for stochastic gradient descent. 

Our next step is to represent the known data. After getting data from word file, relevant numbers are going to the Xs and the Ys that you saw earlier. The np.array is using a Python library called numpy that makes data representation particularly enlists much easier. So here you can see we have one list for the Xs and another one for the Ys. The training takes place in the fit command. Here we're asking the model to figure out how to fit the X values to the Y values. The epochs equals 1000 value means that it will go through the training loop 1000 times. This training loop is what we described earlier. Make a guess, measure how good or how bad the guesses with the loss function, then use the optimizer and the data to make another guess and repeat this. When the model has finished training, it will then give us back values using the predict method. But when we try this in the workbook yourself, you'll see that it will return a value very close to answer but not exactly answer. 

Now why do you think that would be? 

When it is said and done, there are two main reasons. The first is that we trained it using very little data. There's only six points. Those six points are linear but there's no guarantee that for every X, the relationship will be the correct equation. There's a very high probability that Y equals answer, but the neural network isn't positive. So it will figure out a realistic value for Y. That's the second main reason. When using neural networks, as they try to figure out the answers for everything, they deal in probability. 

With this code, it is possible to solve any number of tasks of this type in a word file. Firstly, the tasks are read from the word file, numbers are found, used in the model as an input values, predicted, the results are written to the txt file.


要查看或添加评论,请登录

Nijat Zeynalov的更多文章

  • Creative Shock 2019, an international social business case competition

    Creative Shock 2019, an international social business case competition

    Dear connections, I am writing on behalf of Creative Shock 2019, an international social business case competition…

  • Python T?limi

    Python T?limi

    Praktiki tap??r?qlarla Python proqramla?d?rma dilini 32 saata ?yr?n! Python proqramla?d?rma dili Google, NASA, Youtube,…

社区洞察

其他会员也浏览了