4
$\begingroup$

I would like to choose my loss function. From the references for MeanSquaredLossLayer, I can add it to NetChain, NetGraph, or NetTrain, but when I try to add to NetChain I get an error; when I add it to NetGraph, NetTrain will train the network, but it complains about my dataset. Finally, when I add it to NetTrain, I get an error.

Here is my code:

coder = NetChain[
           {9, LogisticSigmoid, 9, LogisticSigmoid, 1}, 
           "Input" -> 3, "Output" -> "Scalar"
        ]
 trainer = NetInitialize[coder]
 trained[dataReady]

Any suggestion is appreciated.

$\endgroup$
1
  • $\begingroup$ Can you specify what errors you get specifically, and what complaint about your data set? $\endgroup$
    – MarcoB
    Nov 23, 2016 at 14:30

2 Answers 2

6
$\begingroup$

Here is an example of specifying a MeanSquaredLossLayer loss function:

some example data

data = Flatten[Table[{x, y, z} -> Norm[{x, y, z}], {x, -1, 1, 0.2}, {y, -1, 1, 0.2}, {z, -1, 1, 0.2}]];

train net with MeanSquaredLossLayer

coder = NetChain[{9, LogisticSigmoid, 9, LogisticSigmoid, 1}, 
"Input" -> 3, "Output" -> "Scalar"];
trained = NetTrain[coder, data, MeanSquaredLossLayer["Target" -> "Scalar"]]

By the way, the default loss layer is the MeanSquaredLossLayer, so you can just omit the loss layer and just write it as

trained = NetTrain[coder, data]
$\endgroup$
0
4
$\begingroup$

The problem is that NetChain does not support any layers with multiple inputs or outputs (it would no longer be a chain). Thus you cannot put a loss layer into a NetChain. You have to use a NetGraph. For an example, see the MeanSquaredLossLayer documentation. One example from there: net = NetGraph[{ElementwiseLayer[Tanh], MeanSquaredLossLayer[]}, {1 -> NetPort[2, "Input"]}]

You can of course embed your NetChain into a NetGraph along with the loss, and extract it again using NetExtract after training.

$\endgroup$
2
  • 1
    $\begingroup$ Thank You for the clarification! $\endgroup$
    – user34018
    Apr 20, 2017 at 20:50
  • $\begingroup$ @user34018, you should accept the answer so that other users knew that it addresses the issue. $\endgroup$
    – garej
    May 26, 2019 at 8:51

Your Answer

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

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