2
$\begingroup$

I am trying to check the accuracy of a neural network by using ClassifierMeasurements. I take the MNIST as example:-

Code for setting

k = 4;
resource = ResourceObject["MNIST"];
trainingData = ResourceData[resource, "TrainingData"];
trainingSubset = Select[trainingData, Last[#] <= k &];
testData = ResourceData[resource, "TestData"];
testSubset = Select[testData, Last[#] <= k &];
RandomSample[trainingSubset, 8]
trainingImages = Keys[trainingSubset];
meanImage = Image[Mean@Map[ImageData, trainingImages]]

net = NetGraph[{FlattenLayer[], 50, Ramp, 784, Tanh, ReshapeLayer[{1, 28, 28}], 
MeanSquaredLossLayer[]}, {1 -> 2 -> 3 -> 4 -> 5 -> 6 -> NetPort["Output"], 
6 -> NetPort[7, "Input"], NetPort["Input"] -> NetPort[7, "Target"]}, 
"Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale", "MeanImage" -> meanImage}],
"Output" -> NetDecoder[{"Image", "Grayscale"}]]

trained = NetTrain[net, <|"Input" -> trainingImages|>, "Loss"];

The network would be be successfully trained and can output something like trained[trainingImages[[1]]]. However, when I tried to create ClassifierMeasurements, there is an error.

Code to create ClassifierMeasurements:-

cm = ClassifierMeasurements[trained, testData]

Error message:-

ClassifierMeasurements: This neural network cannot be converted to a ClassifierFunction.

How can I get the accuracy checking function for the network? Many thanks!

$\endgroup$
  • $\begingroup$ Your network is autoencoder. Classification network should have SoftmaxLayer as the last layer. NetChain[{...,2, SoftmaxLayer[]}, "Output" -> NetDecoder[{"Class", {0, 1}}]] $\endgroup$ – Alexey Golyshev May 1 '18 at 6:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.