# A Visual and Interactive Guide to the Basics of Neural Networks

Discussions: ,
Translations:

Update: Part 2 is now live: A Visual And Interactive Look at Basic Neural Network Math

## Motivation

i’m not a machine learning expert. i’m a software engineer by training and i’ve had little interaction with ai. i had always wanted to delve deeper into machine learning, but never really found my “in”. that’s why when google open sourced tensorflow in november 2015, i got super excited and knew it was time to jump in and start the learning journey. not to sound dramatic, but to me, it actually felt kind of like prometheus handing down fire to mankind from the mount olympus of machine learning. in the back of my head was the idea that the entire field of big data and technologies like hadoop were vastly accelerated when google researchers released their map reduce paper. this time it’s not a paper – it’s the actual software they use internally after years and years of evolution.

so i started learning what i can about the basics of the topic, and saw the need for gentler resources for people with no experience in the field. this is my attempt at that.

## Start here

let’s start with a simple example. say you’re helping a friend who wants to buy a house. she was quoted $400,000 for a 2000 sq ft house (185 meters). is this a good price or not? 开心时时彩计划软件下载it’s not easy to tell without a frame of reference. so you ask your friends who have bought houses in that same neighborhoods, and you end up with three data points: Area (sq ft) (x) Price (y) 2,104 399,900 1,600 329,900 2,400 369,000 开心时时彩计划软件下载personally, my first instinct would be to get the average price per sq ft. that comes to$180 per sq ft.

welcome to your first neural network! now it’s not quite at siri level yet, but now you know the fundamental building block. and it looks like this:

diagrams like this show you the structure of the network and how it calculates a prediction. the calculation starts from the input node at the left. the input value flows to the right. it gets multiplied by the weight and the result becomes our output.

multiplying 2,000 sq ft by 180 gives us $360,000. that’s all there is to it at this level. calculating the prediction is simple multiplication. but before that, we needed to think about the weight we’ll be multiplying by. here we started with an average, later we’ll look at better algorithms that can scale as we get more inputs and more complicated models. finding the weight is our “training” stage. so whenever you hear of someone “training” a neural network, it just means finding the weights we use to calculate the prediction. this is a form of prediction. this is a simple predictive model that takes an input, does a calculation, and gives an output (since the output can be of continuous values, the technical name for what we have would be a “regression model”) let us visualize this process (for simplicity, let’s switch our price unit from$1 to $1000. now our weight is 0.180 rather than 180): ## Harder, Better, Faster, Stronger 开心时时彩计划软件下载can we do better than estimate the price based on the average of our data points? let’s try. let’s first define what it means to be better in this scenario. if we apply our model to the three data points we have, how good of a job would it do? that’s quite a bit of yellow. yellow is bad. yellow is error. we want to shrink yellow as much as we can. Area (x) Price ($1000) (y_) Prediction (y) y_-y (y_-y
2,104 399.9 379 21 449
1,600 329.9 288 42 1756
2,400 369 432 -63 3969
Average: 2,058

Here we can see the actual price value, the predicted price value, and the difference between them. Then we’ll need to average these differences so we have a number that tells us how much error there is in this prediction model. The problem is, the 3rd row has -63 as its value. We have to deal with this negative value if we want to use the difference between the prediction and price as our error measuring stick. That’s one reason why we introduce an additional column that shows the error squared, thus getting rid of the negative value.

This is now our definition of doing better – a better model is one that has less error. Error is measured as the average of the errors for each point in our data set. For each point, the error is measured by the difference between the actual value and the predicted value, raised to the power of 2. This is called . Using it as a guide to train our model makes it our loss function (also, cost function).

We can't improve much on the model by varying the weight any more. But if we add a bias we can find values that improve the model.

our lines can better approximate our values now that we have this b value added to the line formula. in this context, we call it a “bias”. this makes our neural network look like this:

We can generalize it by saying that a neural network with one input and one output (spoiler warning: and no hidden layers) looks like this:

in this graph, w and b are values we find during the training process. x is the input we plug into the formula (area in sq ft in our example). y is the predicted price.

calculating a prediction now uses this formula:

how about you take a crack at training our toy neural network? minimize the loss function by tweaking the weight and bias dials. can you get an error value below 799?

 Error Weight 0 Bias 0

## Automation

 Error Weight 0 Bias 0

how does gradient descent know where its next step should be? calculus. you see, knowing the function we’re minimizing (our loss function, the average of (y_ - y)² for all our data points), and knowing the current inputs into it (the current weight and bias), the derivatives of the loss function tell us which direction to nudge w and b in order to minimize the error.

## And Then There Were Two

is the size of the house the only variable that goes into how much it costs? obviously there are many other factors. let’s add another variable and see how we can adjust our neural network to it.

say your friend does a bit more research and finds a bunch more data points. she also finds out how many bathrooms each house has:

Area (sq ft) (x1) Bathrooms (x2) Price (y)
2,104 3 399,900
1,600 3 329,900
2,400 3 369,000
1,416 2 232,000
3,000 4 539,900
1,985 4 299,900
1,534 3 314,900
1,427 3 198,999
1,380 3 212,000
1,494 3 242,500

our neural network with two variables looks like this:

but how do we find w1 and w2? this is a little trickier than when we only had to worry about one weight value. how much does having an extra bathroom change how we predict the value of a home?

take a stab at finding the right weights and bias. you will start here to see the complexity we start getting into as the number of our inputs increase. we start losing the ability to create simple 2d shapes that allow us to visualize the model at a glance. instead, we’ll have to mainly rely on how the error value is evolving as we tweak our model parameters.

 Error Weight #1 0 Weight #2 0 Bias 0

## Classification

let’s continue to tweak our example. assume your friend gives you a list of houses. this time, she has labeled which ones she thinks have a good size and number of bathrooms:

Area (sq ft) (x1) Bathrooms (x2) Label (y)
2,104 3 Good
1,600 3 Good
2,400 3 Good
1,985 4 Good
1,427 3 Good
1,380 3 Good
1,494 3 Good

she needs you to use this to create a model to predict whether she would like a house or not given its size and number of bathrooms. you will use this list above to build the model, then she will use the model to classify many other houses. one additional change in the process, is that she has another list of 10 houses she has labeled, but she’s keeping it from you. that other list would be used to evaluate your model after you’ve trained it – thus trying to ensure your model grasps the conditions that actually make her like the features of the house.

The TensorFlow app I discussed in my previous post is a good example for a classification model in practice.

softmax takes an array and outputs an array of the same length. notice that its outputs are all positive and sum up to 1 – which is useful when we’re outputting a probability value. also notice that even though 4 is double 2, its probability is not only double, but is eight times that of 2. this is a useful property that exaggerates the difference in output thus improving our training process.

output
softmax([ 1 ]) [ 1 ]
softmax([ 1, 1 ]) [ 0.5, 0.5 ]
softmax([ 0, 1 ]) [ 0.26, 0.73 ]
softmax([ 2, 4 ]) [ 0.11, 0.88 ]
softmax([ 5, 10 ]) [ 0.007, 0.993 ]
softmax([ -1, 0, 1 ]) [ 0.09, 0.24, 0.66 ]
softmax([ 1, 2, 4 ]) [ 0.04, 0.11, 0.84 ]

 Features (x): Classes (y):

you can see an example of how to create and train this network using tensorflow in i created to accompany this post.

## True Motivation

if you have reached this far, i have to reveal to you another motivation of mine to write this post. this post is meant as an even gentler intro to tensorflow tutorials. if you start working through now, and come across this graph:

I wrote this post to prepare people without machine learning experience for this graph in the TensorFlow introductory tutorial. That's why I simulated its visual style.

i hope you would feel prepared and that you have an understanding of this system and how it works. if you want to start tinkering with code, feel free to pick up from the intro and teach a neural network how to detect handwritten digits.

• What other kinds of cost functions exist? Which are better for which applications?
• What’s the algorithm to actually calculate new weights using gradient descent?
• What are the applications for machine learning in the fields you’re already knowledgeable about? What new magic can you wield by mixing this spell with others in your spell book?

• Coursera’s course by . This is the one I started with. Starts with regression then moves to classification and neural networks.
• Coursera’s by . More focused on neural networks and its visual applications.
• Stanford’s by . It’s interesting to see some advanced concepts and the state of the art in visual recognition using deep neural networks.