5
$\begingroup$

I created and trained a simple neural net, which takes some inputs and produces a 3-class classification. Then I feed it to CM[] function with some test data, and... nothing. Haven't been successful with this no matter what I change about the net or the various function parameters. What am I missing?

the errors I get

$\endgroup$
  • $\begingroup$ Can you post the data somewhere? $\endgroup$ – Vahagn Tumanyan Apr 2 '17 at 14:05
  • $\begingroup$ You can generate sample data which will suit the network with a line like this: data=(#/100-0.5)->If[Total@#>=7257&&Total@#<7736,0,If[Total@#<7257,-1,1]]&/@RandomInteger[100,{50000,150}]; It trains within 1500-2000 rounds to accuracy in excess of 99.5% $\endgroup$ – Gregory Klopper Apr 3 '17 at 2:23
7
$\begingroup$

For classification, you usually need a softmax layer in order for the default cross-entropy loss function to work.

So to solve your problem, you can just add a SoftmaxLayer to your network:

net = NetInitialize@
  NetChain[{3, LogisticSigmoid, 3, SoftmaxLayer[]}, "Input" -> {150}, 
   "Output" -> NetDecoder[{"Class", {-1, 0, 1}}]]

data = Table[RandomReal[1, {150}] -> RandomInteger[{-1, 1}], {5000}];

trained = 
 NetTrain[net, Take[data, 4500], 
  ValidationSet -> {Take[data, -500], "Interval" -> 5}]

cm = ClassifierMeasurements[trained, Take[data, -500]];

cm["Accuracy"]
(* 0.324 *)
$\endgroup$
  • $\begingroup$ Worked great! I was getting my results as 3-tuples of probabilities, then decoding them with the Class decoder, but a SoftmaxLayer[] produced a single class predictor and changed the loss function behind the scenes and that did the trick. Thank you! $\endgroup$ – Gregory Klopper Apr 3 '17 at 2:26
  • $\begingroup$ what is "Interval" -> 5 in the option of ValidationSet mean? $\endgroup$ – partida Jun 16 '17 at 8:53
  • $\begingroup$ @partida It "specify the interval at which to calculate validation loss", i.e. calculate validation loss every 5 training round. $\endgroup$ – xslittlegrass Jun 16 '17 at 15:18

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.