10
$\begingroup$

I recently downloaded Mathematica 12, and I'm experiencing a curious failure in neural networks that had earlier worked. This failure I will demonstrate with the NN in "Classifying Data with Neural Networks—Wolfram Language Documentation" https://reference.wolfram.com/language/tutorial/NeuralNetworksClassification.html#280210622

Everything in that page works until this line:

results = NetTrain[net, trainingData, All]

It gives $Failed with this message:

NetTrain::encgenfail2: Could not encode one or more inputs for port "Output": supplied data was a length-64 vector of real numbers, but expected a class. The invalid inputs had indices {504,841,210,1645,1285,165,956,691,940,613,1426,297,1560,754,650,1725,1662,44,831,1160,1262,1289,1060,886,1147,1578,1342,424,1176,749,1664,1030,211,1011,121,475,237,1120,79,1399,287,1791,293,214,271,222,761,1511,960,1154,<<14>>}.

What is going wrong? A very similar NN worked in version 11.

ETA: detailed version info:

Mma 12.0.0.0 for Mac OS X x86 (64-bit) running on macOS Mojave 10.14.4

The complete code, from that link:

makeCluster[class_, \[Mu]_, \[Rho]_] := 
  RandomVariate[
    MultinormalDistribution[\[Mu], {{1, 2 \[Rho]}, {2 \[Rho], 4}}], 
    600] -> class; clusters = 
  makeCluster @@@ {{Red, {1.5, 1.5}, -.2}, {Green, {-1.5, 
      1}, .1}, {Blue, {0, -2.5}, .8}}; trainingData = Flatten[Thread /@ clusters]

net = NetChain[{LinearLayer[30], ElementwiseLayer[Ramp], 
   LinearLayer[3], SoftmaxLayer[]}, "Input" -> {2}, 
  "Output" -> NetDecoder[{"Class", {Red, Green, Blue}}]]

results = NetTrain[net, trainingData, All]
$\endgroup$
  • $\begingroup$ It works for me on Win 10. $\endgroup$ – b.gates.you.know.what Apr 26 at 8:21
  • $\begingroup$ Works for me on Windows 10 32 bit. $\endgroup$ – user64494 Apr 26 at 17:47
  • 1
    $\begingroup$ Can you post a standalone example which reproduces this issue for you in a fresh, clean kernel session? $\endgroup$ – Arnoud Buzing Apr 26 at 17:50
  • 3
    $\begingroup$ Can you try to run this on a fresh kernel? NeuralNetworks`Private`ReinitializeNeuralNetworks[]; DeleteDirectory[#, DeleteContents -> True] & /@ FileNames[ FileNameJoin@{"*", #} & /@ {"NeuralNetworks*", "MXNetLink*"}, FileNameJoin@{$CacheBaseDirectory, "PacletCachedData"} ]; Then Quit and try again. This will delete internal definitions for the neural net framework that we cache locally to speed up loading times. Maybe those local files got corrupted. $\endgroup$ – Matteo Salvarezza Apr 30 at 13:16
  • 1
    $\begingroup$ I tried it, and it worked. Wolfram's demo neural network seems to be working properly again. I tried the NN that I was working on, and it also works. Thanx. $\endgroup$ – lpetrich Apr 30 at 22:50
5
$\begingroup$

Since the answer that solved the problem was posted in a comment by Matteo Salvarezza, I will reproduce it here as an answer for the convenience of anyone else running into this (note that this will quit your kernel, so only run it after saving any variable definitions you may want to hang on to):

NeuralNetworks`Private`ReinitializeNeuralNetworks[];
DeleteDirectory[#, DeleteContents -> True] & /@ FileNames[
   FileNameJoin @ {"*", #} & /@ {"NeuralNetworks*", "MXNetLink*"},
   FileNameJoin @ {$CacheBaseDirectory, "PacletCachedData"}
 ];
Quit

This will delete internal definitions for the neural net framework that we cache locally to speed up loading times. These local files could have been corrupted if you have this problem.

Another line that's useful to keep in mind when you're having trouble with neural networks:

PacletUpdate["NeuralNetworks"]
$\endgroup$

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.