```

Your First Neural Network

In this lesson we will build and run the same simple neural network in two different ways: Python with TensorFlow/Keras and JavaScript with TensorFlow.js.

Both programs are based on exactly the same machine-learning idea. We provide examples to a neural network and allow it to discover the relationship hidden inside the data.

```
```

The Learning Problem

The network receives the following pairs of values:

x: 0, 1, 2, 3, 4, 5

y: 1, 3, 5, 7, 9, 11

We can immediately see that the actual relationship is:

y = 2x + 1

The important point is that the neural network is not simply given this formula and asked to calculate an answer.

Instead, it sees examples and gradually adjusts its internal weight and bias so that its predictions become closer to the supplied answers.

```
```
Python + TensorFlow

Version 1: Build the Neural Network in Python

This version uses Python, NumPy, TensorFlow and Keras.

The training values are stored in NumPy arrays. A very small Keras Sequential model is then created with one numerical input and one Dense neuron.

The model is compiled using the SGD optimizer and mean squared error as the loss function.

During training, TensorFlow repeatedly compares the predicted values with the expected values. The difference is measured by the loss function, and the optimizer adjusts the weight and bias in an attempt to reduce that error.

After training, the program asks the model to predict the result for x = 10, a value that was not present in the original training data.

Since the real relationship is y = 2x + 1, the expected answer for x = 10 is approximately 21.

The program also displays the weight and bias learned by the neuron. Ideally, they should approach: weight = 2 and bias = 1.

```
```
JavaScript + TensorFlow.js

Version 2: Train the Same Neural Network in the Browser

This version demonstrates the same idea using TensorFlow.js.

Instead of running TensorFlow through Python, TensorFlow.js allows the neural network to be created, trained and used directly inside a web browser with JavaScript.

The model still has one input and one Dense neuron. It still learns a weight and a bias, and it still attempts to approximate the same hidden relationship:

y = 2x + 1

This version is particularly useful for experimentation because you can control the number of training epochs from the interface.

Start with 500 epochs and train the model. Watch the loss during training and then inspect the learned weight and bias.

After training, enter different x values and press Predict y.

For example, enter 10. A well-trained model should give a result close to 21.

Then try values such as 6, 20 or 50. These values were not included in the original training examples, so the model is using the relationship it learned rather than simply recalling an answer.

```
```

What Should You Notice?

The two programs look very different because one is written in Python and the other in JavaScript, but the neural-network process is the same.

First we provide training examples. Then we create a model containing a neuron. The model begins with unsuitable values for its weight and bias.

Training repeatedly adjusts those values in order to reduce prediction error. Eventually the neuron should discover a relationship close to:

output = input × 2 + 1

This is the central lesson: in ordinary programming we normally provide the rule and ask the computer to calculate the answer. In this machine-learning example, we provide examples and allow the computer to discover an approximate rule.

```