5
$\begingroup$

NetChain provides a method NetExtract.
I want to see how standClassify = Classify[trainingData, Method -> "NeuralNetwork"] works,beacause it has a better accuracy.
I cann't find a method to do a such thing like NetExtract.
I ranSave["standClassify ", standClassify ],and got all information standClassify = ClassifierFunction[ <|"Basic" -> ....|>] ,deleted ClassifierFunction[],ran Get["standClassify"].
Then ran standClassify[["Models"]].
I still cann't understand the structure.
Dimensions /@ \ (standClassify[["Models"]][[1]][["NeuralNetwork"]][["Weights"]])

{{972, 324}, {972, 972}, {10, 972}}

My input Dimension is 784, enter image description here

Maybe other good solution to get those information.

reduce = DimensionReduction[Keys[trainingData], 324] to get DimensionReducerFunction
Then transform input transform[data_] := Replace[#, RuleDelayed[image_ -> label_, reduce[image] -> label]] & /@ data;
Train standClassify = NetTrain[lineNet, trainingData // transform, MaxTrainingRounds -> 3, TargetDevice -> "GPU"].

This didn't work well,classifier-accuracy is 0.8676.
Accuracy can be 0.9379 if I use Classify.

$\endgroup$

1 Answer 1

11
$\begingroup$

For example, we have data:

SeedRandom[0];
n = 1000;
X = RandomReal[{-1, 1}, {n, 10}];
Y = RandomInteger[1, n];
c = Classify[X -> Y, Method -> "NeuralNetwork"]

Let's see the options:

Options[c]

We can see neuron types: enter image description here

Let's reconstruct this network:

net = NetChain[
  {LinearLayer[30], Ramp, LinearLayer[30], Ramp, 2, SoftmaxLayer[]},
  "Input" -> 10,
  "Output" -> NetDecoder[{"Class", {0, 1}}]
  ]

enter image description here

But weights are uninitialized. Let's initialize them with NetReplacePart. This is the new function in V11.1. As I see you are using V11.0. But you can experiment with V11.1 in the Wolfram Open Cloud for free.

weights = Options[c][[1]]["Models"][[1]]["NeuralNetwork"]["Weights"];
biases = Options[c][[1]]["Models"][[1]]["NeuralNetwork"]["Biases"];

net = NetReplacePart[net,
  {
   {1, "Weights"} -> weights[[1]],
   {1, "Biases"} -> biases[[1]],
   {3, "Weights"} -> weights[[2]],
   {3, "Biases"} -> biases[[2]],
   {5, "Weights"} -> weights[[3]],
   {5, "Biases"} -> biases[[3]]
   }
  ]

Let's compare our classifiers:

ClassifierMeasurements[c, X -> Y, "ConfusionMatrixPlot"]

enter image description here

In V11.1 network can be converted into a ClassifierFunction.

ClassifierMeasurements[Classify[net], Standardize[X] -> Y, "ConfusionMatrixPlot"]

enter image description here

Result is the same!

Additional links:

  1. Wolfram Open Cloud: https://www.open.wolframcloud.com

  2. How to change weights of a neural network: https://mathematica.stackexchange.com/a/141291/23402

  3. "Is it possible to convert the result of NetTrain to a PredictorFunction?": https://mathematica.stackexchange.com/a/140095/23402

$\endgroup$
3
  • $\begingroup$ In v12.2, the Options[c] outputs an empty list, may I ask what should I do? $\endgroup$ Commented Jul 24, 2021 at 8:47
  • $\begingroup$ Hello @Charmbracelet! Try c[[1]]. $\endgroup$ Commented Jul 26, 2021 at 6:38
  • $\begingroup$ Thanks, it works! $\endgroup$ Commented Jul 27, 2021 at 12:22

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.