0
$\begingroup$

In this example,generator[<|"BatchSize" -> 4|>] get an association of lists, but NetTrain[net, generator] can't be used.

resource = ResourceObject["MNIST"];
trainingData = ResourceData[resource, "TestData"];
encoder1 = NetChain[{FlattenLayer[], 128, 8}, 
            "Input" -> NetEncoder[{"Image", {28, 28}, ColorSpace -> "Grayscale"}]];
encoder2 = NetChain[{8}, "Input" -> NetEncoder[{"Class", Range[0, 9], "UnitVector"}]];

decoder = NetChain[{128, 28*28, ReshapeLayer[{1, 28, 28}]}, "Output" -> NetDecoder[{"Image", ColorSpace -> "Grayscale"}]];

net = NetGraph[{encoder1, encoder2, ThreadingLayer[#1*(1 - #2) &], 
                ThreadingLayer[#1*#2 &], ThreadingLayer[Plus], 
                decoder, ReplicateLayer[8], FlattenLayer[]}, 
                {NetPort["image"] -> 1 -> 3, NetPort["digit"] -> 2 -> 4, 
                NetPort["switch"] -> 7 -> 8 -> {3, 4} -> 5 -> 6 -> NetPort["Output"]}, "switch" -> 1]

enter image description here

This net can predict the image although it haven't be trained

NetInitialize[net][<|"image" -> trainingData[[1, 1]], 
                     "digit" -> trainingData[[1, 2]], 
                     "switch" -> 0|>]

enter image description here

generator also works well

generator = 
  Function[Block[{data = RandomSample[trainingData, #BatchSize]}, <|
    "image" -> data[[All, 1]], "digit" -> data[[All, 2]], 
    "switch" -> RandomInteger[1, #BatchSize], 
    "Output" -> data[[All, 1]]|>]];

generator[<|"BatchSize" -> 4|>]

enter image description here

But when type NetTrain[net, generator],it throws error.

enter image description here

weird!What's wrong?

$\endgroup$
  • $\begingroup$ @nikie But in mathematica.stackexchange.com/a/145780/15961, you say "the generator is supposed to return an association of lists, not a list of associations" $\endgroup$ – partida Sep 2 '17 at 0:18
  • $\begingroup$ Good point, I can never remember which it is... $\endgroup$ – Niki Estner Sep 2 '17 at 7:34
  • $\begingroup$ I think ThreadingLayer needs two inputs with the same dimensions. So you'll need to pass a tensor for the input switch $\endgroup$ – Niki Estner Sep 2 '17 at 7:37
  • $\begingroup$ @it is the same dimension actually... $\endgroup$ – partida Sep 2 '17 at 8:12
1
$\begingroup$

You get much better error messages if you pass a list of samples instead of a generator to NetTrain:

NetTrain[net, generator[<|"BatchSize" -> 10|>]]

NetTrain::invindim: Data provided to port "switch" should be a list of length-1 vectors.

This is much more helpful, and if we change the generator so it creates a list of length-1 vectors for switch:

generator = 
  Function[Block[{data = RandomSample[trainingData, #BatchSize]}, <|
     "image" -> data[[All, 1]], "digit" -> data[[All, 2]], 
     "switch" -> ({#} & /@ RandomInteger[1, #BatchSize]), 
     "Output" -> data[[All, 1]]|>]];

Training with the generator works, too:

NetTrain[net, generator]
|improve this answer|||||
$\endgroup$
  • $\begingroup$ This way looks interesting,use generator[<|"BatchSize" -> 10|>] as param,then solve the problem.you are so expert $\endgroup$ – partida Sep 2 '17 at 9:02

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.