This content originally appeared on Level Up Coding - Medium and was authored by Ryan Chou
No experience required!
Packages and Setup
We only need TensorFlow and NumPy.
# In terminal or command line:
pip install tensorflow
pip install numpy
# Or, if you are in a Colab notebook (to install) :
! pip install numpy
! pip install tensorflow
# In project:
import numpy as np
import tensorflow as tf
from tensorflow import keras
We’re using the latest version of TensorFlow, TensorFlow 2.0.
What is a neural network?
Neural networks use data to improve themselves over time. They’re like generations which gradually get better over time. Each node has its input data, weights, bias, and output with an equation that looks like this:

I’m going through these concepts very briefly, but if you would like to learn more. I highly recommend these resources:
- Neural Network in 5 minutes | By: Simplilearn
- Types of Neural Networks | By: Databricks
- But what is a Neural Network? | By: 3Blue1Brown
Generating data
Let’s define our datasets.
We can just use a linear equation, y = mx + b, to make an equation to give our arrays a pattern.
Let’s say y = 9x + 2, now we can substitute the values to get the other.
For example,
x = 1, y = 11, x = 2, y = 20, x = 3, y = 29.0.
We’re going to store this in an NumPy array.
xs = np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], dtype=float)
ys = np.array([11.0, 20.0, 29.0, 38.0, 47.0, 56.0], dtype=float)
Defining our model
The fun part!
In this instance, we’ll be using a sequential model. This groups a linear stack of layers into a TensorFlow model. But, this is only good for scenarios with one input tensor and one output tensor, which is what we have right now.
model = tf.keras.Sequential(
[keras.layers.Dense(units=1, input_shape=[1])]
)
input_shape=[1] There is only have one input with one value in our data.
units=1 This is our only layer, so we only need 1 neuron/cell for it.
We now need to compile our model, and we can use stochastic gradient descent or SGD, as our optimizer. This way we can analyze and update our model and improve it at a much faster way.
I really like the way Sujan Dutta explains it:
“Suppose you have made an app and want to improve it by taking feedback from 100 customers. You can do it in two ways. In the first way, you can give the app to the first customer and take his feedback then to the second one, then the third, and so on. After collecting feedback from all of them you can improve your app. But in the second way, you can improve the app as soon as you get feedback from the first customer. Then you give it to the second one and you improve again before giving it to the third one. Notice that in this way you are improving your app at a much faster rate and can reach an optimal point much earlier.”
We can calculate the loss by squaring the mean of the error.
model.compile(optimizer='sgd', loss='mean_squared_error')
Let’s train our model!
We train our model by fitting it with our data.
model.fit(xs, ys, epochs=100)
You can increase/decrease the epochs to achieve different results.
Running it will fill up your terminal with this data
Epoch 1/100 1/1 [==============================] - 0s x ms/step - loss: x
The loss indicates how flawed the model’s prediction was on a single iteration.
In my training, my loss at the first epoch was about 1480, but then dropped to an astonishing 0.030 by the 100th epoch!
Making predictions
We can simply call our model to predict something by calling model.predict().
I’ll try predicting 10.
print(model.predict([10.0]))
We can find the actual answer by plugging it into our aforementioned equation, y = 9x + 2.
We’ll find out that the exact output should be 92.
What will the model think?
It turns out, really close!
The model predicted 91.8272, just short of 92.
Finished code:
Conclusion
Thanks!
I hope you enjoyed reading this, and that it taught you more about Tensorflow and neural networks. If you have any questions, suggestions, general feedback, or your code doesn’t work, put in the comments!
Keep on coding!
Make your first neural network with TensorFlow! was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.
This content originally appeared on Level Up Coding - Medium and was authored by Ryan Chou
Ryan Chou | Sciencx (2021-04-15T15:20:58+00:00) Make your first neural network with TensorFlow!. Retrieved from https://www.scien.cx/2021/04/15/make-your-first-neural-network-with-tensorflow/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.