# Tensorflow

## Basic Dense Network Model

Instantiate a Dense network model with a single layer, the simplest possible model Tensorflow can support. This configuration will have:

* `input_shape=[1]`- Input to this layer is a single value, the shape of this model is a one-dimensional array with one member. Since this is the only layer, it is the shape of the entire model.
* `units=1` - Specifieds number of neurons in the layer. How many internal variables the layer has to try to learn how to solve the problem. Since this is the final layer, it is also the size of the model's output&#x20;
* Sequential model - Takes a list of layers as an argument specifying calculation order from input to output
* Loss function -  a way of measuring how far off predictions are from the desired outcome. In this case we'll use `mean_squared_error`
* Optimizer function - A way of adjusting the internal values in order to reduce the loss. In this case we'll use `Adam`
  * In this case we'll give it a learning rate of 0.1 but it is important to consider. If the learning rate is too small then it will take too long to train, too large and it will be inaccurate. Finding an optimal value for your use case can involve trial and error.

```python
# Initiate the layers
l0 = tf.keras.layers.Dense(units=1, input_shape=[1])

# Assemble layers into the model
model = tf.keras.Sequential([l0])

# You can also do all this by defining the layers inside the model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(units=1, input_shape=[1])
    ])
    
# Compile the model
model.compile(loss='mean_squared_error',
              optimizer=tf.keras.optimizers.Adam(0.1))

# Train the model using the fit method
history = model.fit(celsius_q, fahrenheit_a, epochs=500, verbose=False)
print("Finished training the model")

# Make a prediction
model.predict([100.0])
```

### Dense Layers

In basic terms, all neurons from one layer are connected to the neurons from the previous layer and the next layer, or _fully connected_.&#x20;

The following weights (_w's_) and variables (_b_) are changed iteratively during the training process until the final values are very close to correct.

![](https://4179876243-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lb3BUyedjtR1mtEKSX0%2F-LiKd13dDQqCSDQCTxlr%2F-LiLW-fD_jNMOvBkIgHe%2Fimage.png?alt=media\&token=2d6d16e4-504d-49e7-b510-53bf67e6dcd1)

The 1 layer Dense network we established previously to learn the equation for Celsius to Fahrenheit pretty much looks like this:

![](https://4179876243-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-Lb3BUyedjtR1mtEKSX0%2F-LiKd13dDQqCSDQCTxlr%2F-LiLWtsI5UdT05erdlOm%2Fimage.png?alt=media\&token=5c18bdc2-6402-4e4d-b387-9201cdc99d89)
