I want to train a network,the first input is a Integer,the second input is 10-dims vector.

Assume we have those data:

Labels = RandomReal[1, {INPUTNOTE, 43}];
data = Transpose[{Join[List /@ Range[INPUTNOTE], RandomReal[1, {INPUTNOTE, 10}],2], Labels}];
generator = Function[<|"EmbeddingLayerInput" -> #[[1, 1]], "Input2" -> #[[1, 2 ;;]], "OutPut" -> #[[2]]|> & /@ RandomSample[data, #BatchSize]];
generator[<|"BatchSize" -> 3|>]

enter image description here

net = NetGraph[{EmbeddingLayer[128, "Input" -> NetEncoder[{"Class", Range[INPUTNOTE]}]], 
 CatenateLayer[], 512, Ramp, 43},
{NetPort["EmbeddingLayerInput"] -> 1 -> 2, NetPort["Input2"] -> 2, 2 -> 3 -> 4 -> 5}]

enter image description here

NetTrain[net, generator]


But I see the doc of NetTrain has this description:

enter image description here

So What's wrong?

How to fix it?


1 Answer 1


I see two small mistakes: First, if I run your code, I see this message in the message window:

NetTrain::nfspec: Cannot train net: "Input" port of layer 2 is not fully specified.

So you need to specify the size of the Input2 output port:

net = NetGraph[..., "Input2" -> 10]

When I run this, I get the message:

NetTrain::invgenout: Output of generator function (<<1>>&)/@RandomSample[<<2>>]& was incorrect: generator did not return an association or list of rules.

(emphasis mine). So the generator is supposed to return an association of lists, not a list of associations:

generator = Function[
   Module[{samples = RandomSample[data, #BatchSize]},
    <|"EmbeddingLayerInput" -> samples[[All, 1, 1]], 
     "Input2" -> samples[[All, 1, 2 ;;]], 
     "Output" -> samples[[All, 2]]|>]];

With these changes, NetTrain runs fine.

(I ran into exactly the same problem the first time I used a generator function. IMO an example in the documentation would have been helpful)

  • $\begingroup$ Your response is soooo fast! Thank you!!! It solve my problem $\endgroup$
    – partida
    May 11, 2017 at 13:50

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.