2
$\begingroup$

I’m new to machine learning and am a bit confused about how to create a neural network (multilayer perceptron) with a specific design.

I would like to create a neural network with 40 features/neurones in input layer, 2 hidden layers each with 200 neurones and sigmoid activation function then one neurone in output layer to predict a scalar.

Should I use NetChain and then have the layers:

LinearLayer[{40,200}],ElementwiseLayer[LogisticSigmoid],LinearLayer[{200,200}], ElementwiseLayer[LogisticSigmoid],LinearLayer[{200,1}] 

In order to produce the network I have in mind?

$\endgroup$

1 Answer 1

4
$\begingroup$
net = NetChain[{LinearLayer[200], ElementwiseLayer[LogisticSigmoid], 
   LinearLayer[200], ElementwiseLayer[LogisticSigmoid], 
   LinearLayer[1]}, "Input" -> 40, "Output" -> NetDecoder["Scalar"]]

Information[net, "SummaryGraphic"]

Mathematica graphics

This can also be produced more concisely:

net = NetChain[{200, LogisticSigmoid, 200, LogisticSigmoid, 1}, 
  "Input" -> 40, "Output" -> NetDecoder["Scalar"]]
$\endgroup$
7
  • $\begingroup$ Thank you. Could you explain please why we need a 3rd linear layer and activation layer? Why after the two hidden layers can we not go directly to an output neuron/layer? $\endgroup$
    – Gabi23
    Aug 19, 2021 at 17:09
  • $\begingroup$ I have edited the question and removed the last activation layer. You don't need it unless you want to force your output to the $[0,1]$ range. You do, however, need the last layer (imagine it as being the "output" layer) so that you get from 400 nodes to a scalar output. $\endgroup$
    – Domen
    Aug 19, 2021 at 17:15
  • $\begingroup$ Thank you for great explanation. $\endgroup$
    – Gabi23
    Aug 19, 2021 at 17:16
  • $\begingroup$ When I use this net code, I get an error when I go to train it - output is not a scalar (which I want it to be) but a 9000-element array. My trainingdata set has 9000 elements which are of the form {-4.007, 0.3698, -1., -0.1528, -0.5851, 0.4517, -0.1968, -0.1535, 0.076, 0.2107, -0.32301, -0.3082, 1., 0.2733, 0.5368, -0.31, 0.01269, 0.298, -0.3772, -0.3255, 0.223, 0.2298, -0.683, 0.0712, -0.87, 0.4012, -0.324, 0.3523, 0.8496, 0.084, -0.007, 0.6469, -0.222, 0.8281, -0.4817, 0.282618, 0.0196, -0.1807, -0.246, -0.1187} -> 0.267 . What could this be? $\endgroup$
    – Gabi23
    Aug 19, 2021 at 21:08
  • $\begingroup$ As you can see in the SummaryGraphic, the output is supposed to be a vector with size 1. So your data should look like {x1, x2, ..., x40} -> {y1}. However, the other solution is to explicitly specify that you want the output to be a scalar: NetChain[{200, LogisticSigmoid, 200, LogisticSigmoid, 1}, "Input" -> 1, "Output" -> NetDecoder["Scalar"]]. I hope either of these two solve your problem. $\endgroup$
    – Domen
    Aug 19, 2021 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.