I'm trying to train the following structure of neural networknet
with customized loss functionlossnet

each code and structure is below.

net = NetChain[{LinearLayer[10], BatchNormalizationLayer[], 
   ElementwiseLayer[Tanh], LinearLayer[10]}, "Input" -> 10, 
  "Output" -> 10]

enter image description here

lossnet = NetGraph[<|
   "rest" -> SequenceRestLayer[],
   "reverse" -> SequenceReverseLayer[],
   "firstelement" -> SequenceLastLayer[],
   "elementwise" -> ElementwiseLayer[Power[# - 1]^2 &],
   "most" -> SequenceMostLayer[],
   "last" -> ThreadingLayer[(#1 - #2 - 1)^2 &],
   "out" -> SummationLayer[],
   "summary" -> ThreadingLayer[Plus]
   NetPort["Input"] -> "rest",
   NetPort["Input"] -> "reverse",
   "reverse" -> "firstelement",
   "firstelement" -> "elementwise",
   NetPort["Input"] -> "most",
   "rest" -> "last",
   "most" -> "last",
   "last" -> "out",
   {"out", "elementwise"} -> "summary"}]

enter image description here

but when start training,I encountered this error.

NetTrain[net, <|NetPort["Input"] -> {Range[1, 10]}|>, 
 LossFunction -> lossnet]

NetTrain::missinslot: Training data specification should include values for the port Input, but only contains values for the port NetPort[Input]. You may need to explicitly specify the loss port(s) of the net using LossFunction.

it seems that something related to my input is wrong,but I don't know where it is.
perhaps,this is a rudimentary mistake,though.

what is wrong?

  • 2
    $\begingroup$ You need to specify output data as well as input data. $\endgroup$ – Carl Lange Jun 1 at 14:31
  • $\begingroup$ @Carl Lange NetTrain[net, <|"Input" -> {Range[1, 10]}, "Output" -> {ConstantArray[0, {10}]}|>, LossFunction -> lossnet] works ! thank you!. would you re-post it as answer for accept ? $\endgroup$ – Xminer Jun 1 at 14:35
  • $\begingroup$ Sure! Sorry about the terseness of my comment - I'm glad you found the solution yourself. $\endgroup$ – Carl Lange Jun 1 at 16:02

For most neural networks, you need to specify output data as well as input data for NetTrain. Your code only specifies input data.

NetTrain[net, <|"Input" -> {Range[1, 10]}, "Output" -> {ConstantArray[0, {10}]}|>,
   LossFunction -> lossnet]

works, as will the shorthand {examplein1 -> exampleout1, examplein2 -> exampleout2, ...} like:

NetTrain[net, {examplein1 -> exampleout1, examplein2 -> exampleout2, ...}, 
   LossFunction -> lossnet]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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