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).
开心时时彩计划软件下载now that we defined our measuring stick for what makes a better model, let’s experiment with a couple more weight values and compare them with our average pick:
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:
开心时时彩计划软件下载so our current model calculates predictions by plugging in the area of house as x in this formula:
Train Your Dragon
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
开心时时彩计划软件下载congratulations on manually training your first neural network! let’s look at how to automate this training process. below is another example with an additional autopilotlike functionality. these are the gd step buttons. they use an algorithm called “gradient descent” to try to step towards the correct weight and bias values that minimize the loss function.
Error  
Weight  0  
Bias  0 
开心时时彩计划软件下载the two new graphs are to help you track the error values as you fiddle with the parameters (weight and bias) of the model. it’s important to keep track of the error as the training process is all about reducing this error as much possible.
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.
开心时时彩计划软件下载learn more about gradient descent and how to use it to calculate the new weights & bias in the first lectures of coursera’s course.
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:
开心时时彩计划软件下载we now have to find two weights (one for each input) and one bias to create our new model.
开心时时彩计划软件下载calculating y 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 
开心时时彩计划软件下载our trusty gradient descent is here to help once again. it still is valuable in helping us find the right weights and bias.
Features
开心时时彩计划软件下载now that you’ve seen neural networks with one and two features, you can sort of figure out how to add additional features and use them to calculate your predictions. the number of weights will continue to grow, and our implementation of gradient descent will have to be tweaked as we add each feature so that it can update the new weights associated with the new feature.
开心时时彩计划软件下载it’s important to note here that we don’t blindly feed the network everything we know about our examples. we have to be selective about which features we feed the model. feature selection/processing is an entire discipline with its own set of best practices and considerations. if you want to see an example of the process of examining a dataset to choose which features to feed a prediction model, check out . it’s a notebook where narrates his process for solving kaggle’s titanic challenge. kaggle makes available the passenger’s manifest of the titanic including data like name, sex, age, cabin, and whether the person survived or not. the challenge is to build a model that predicts whether a person survived or not given their other information.
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,416  2  Bad 
3,000  4  Bad 
1,985  4  Good 
1,534  3  Bad 
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 neural networks we’ve been toying around with until now are all doing “regression” – they calculate and output a “continuous” value (the output can be 4, or 100.6, or 2143.342343). in practice, however, neural networks are more often used in “classification” type problems. in these problems, the neural network’s output has to be from a set of discrete values (or “classes”) like “good” or “bad”. how this works out in practice, is that we’ll have a model that will say that it’s 75% sure that a house is “good” rather than just spit out “good” or “bad”.
开心时时彩计划软件下载one way we can transform the network we’ve seen into a classification network is to have it output two values – one for each class (our classes now being “good” and “bad”). we then pass these values through an operation called “”. the output of softmax is the probability of each class. for example, say that layer of the network outputs 2 for “good” and 4 for “bad”, if we feed [2, 4] to softmax, it will return [0.11, 0.88] as the output. which translates the values to say the network is 88% sure that the inputted value is “bad” and our friend would not like that house.
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 ] 
开心时时彩计划软件下载as you can see in the last two rows, softmax extends to any number of inputs. so now if our friend adds a third label (say “good, but i’ll have to airbnb one room”), softmax scales to accomedate that change.
开心时时彩计划软件下载take a second to explore the shape of the network as you vary the number of features (x1, x2, x3…etc) (which can be area, number of bathrooms, price, proximity to school/work…etc) and vary the number of classes (y1, y2, y3…etc) (which can be “too expensive”, “good deal”, “good if i airbnb”, “too small”):
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 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.
开心时时彩计划软件下载you should also continue your education by learning the theoretical and mathematical underpinnings of the concepts we discussed here. good questions to ask now include:
 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?
开心时时彩计划软件下载great learning resources include:
 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.
 The is a great resource to learn more about the different types of neural networks.
Acknowledgements
开心时时彩计划软件下载thanks to , , , , , and for their assistance in reviewing previous versions of this post.
开心时时彩计划软件下载please contact me on with any corrections or feedback.