0
$\begingroup$

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?

$\endgroup$
3
  • 2
    $\begingroup$ You need to specify output data as well as input data. $\endgroup$ Commented Jun 1, 2019 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$ Commented Jun 1, 2019 at 14:35
  • $\begingroup$ Sure! Sorry about the terseness of my comment - I'm glad you found the solution yourself. $\endgroup$ Commented Jun 1, 2019 at 16:02

1 Answer 1

1
$\begingroup$

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]
$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.