The ‘Hello World’ of neural networks¶
A neural network is basically a set of functions which can learn patterns.
The layers and the neurons¶
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, you use the word dense to define a layer of connected neurons. There's only one dense here
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.
You 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
The optimizer
and the loss
¶
Here are two function roles that you should be aware of though and these are loss functions and optimizers. This code defines them.
As the guesses get better and better, an accuracy approaches 100 percent, the term convergence is used.
The data¶
the next step is the data, in this case we use the library numpy
The training¶
The training takes place in the fit command.
The epochs equals 500 value means that it will go through the training loop 500 times.Then the model has finished training, it will then give you back values using the predict method.
The prediction¶
you'll see that it will return a value very close to 19 but not exactly 19. there are two main reasons. The first is that you trained it using very little data. There's only six points.
The second main reason, when we use neural networks, as they try to figure out the answers for everything, they deal in probability. You'll see that a lot and you'll have to adjust how you handle answers to fit.
Example:¶
Imagine if house pricing was as easy as a house costs 50k + 50k per bedroom, so that a 1 bedroom house costs 100k, a 2 bedroom house costs 150k etc.
How would you create a neural network that learns this relationship so that it would predict a 7 bedroom house as costing close to 400k etc.
Hint: Your network might work better if you scale the house price down. You don't have to give the answer 400...it might be better to create something that predicts the number 4, and then your answer is in the 'hundreds of thousands' etc.