Building a neural network

The syntax for building a neural network is similar to building a neuron. Rather than pontificating about how this is done, we give two examples. You may have to spend a little time digesting what is going on, but afterwards, you should be able to build your own custom neural network.

Single hidden layer

biases1={.3,-.4};​​weights1={{.7},{-.2}};​​biases2={.4};​​weights2={{.9,.7}};​​activation=Ramp;​​layer1=LinearLayer[2,"Biases"biases1,"Weights"weights1];​​layer2=LinearLayer[1,"Biases"biases2,"Weights"weights2];​​network1=NetChain[{layer1,activation,layer2,activation},"Input"1];​​Plot[{network1[x]},{x,-10,10},PlotRange{-3,3},PlotStyleThickness[0.01]]

Two hidden layers

In[]:=
biases1={.3,-.4};​​weights1={{.7},{-.2}};​​biases2={1.4,-.7};​​weights2={{.9,-.9},{1.2,.5}};​​biases3={.1};​​weights3={{.9,-.8}};​​activation=Ramp;​​layer1=LinearLayer[2,"Biases"biases1,"Weights"weights1];​​layer2=LinearLayer[2,"Biases"biases2,"Weights"weights2];​​layer3=LinearLayer[1,"Biases"biases3,"Weights"weights3];​​network2=NetChain[{layer1,activation,layer2,activation,layer3,activation},"Input"1];​​Plot[{network2[x]},{x,-15,15},PlotRange{-4,4},PlotStyleThickness[0.015]]

Mystery function

Find a neural network whose output function has the following graph:
In[]:=
g[x_]:=Ramp[x-1]+Ramp[-x-1]+2;​​Plot[{g[x]},{x,-10,10},PlotRange{-5,5},PlotStyle{Thickness[0.015]}]
Out[]=
HINT:TheanswerisaneuralnetworkwithonehiddenlayerandRampactivationfunctions.Yourjobistofindtheappropriateweights.Youcanthinkdeeplyaboutwhattheseoughttobe,orexperimentabit.Usethefollowingcodetostart:
In[]:=
biases1={.3,-.4};​​weights1={{.7},{-.2}};​​biases2={.4};​​weights2={{.9,.7}};​​activation=Ramp;​​layer1=LinearLayer[2,"Biases"biases1,"Weights"weights1];​​layer2=LinearLayer[1,"Biases"biases2,"Weights"weights2];​​network=NetChain[{layer1,activation,layer2,activation},"Input"1];Plot[{g[x],network[x]},{x,-10,10},PlotRange{-5,5},PlotLegends{"g(x)","neural network"},PlotStyle{Thickness[0.015],Thickness[0.006]}]
Out[]=
g(x)
neural network