Message Boards Message Boards

Visualize weights of convolutional layer built locally?

Posted 6 years ago

Hi, First of all i apologize if this question has already been answered but i am unable to find a solution hence i am asking. I have been trying to build a CNN to classify the MNIST dataset. I have imported the data from local folder and have trained the network. My problem is that i would like to visualize the weights but have been receiving an error. I have tried various sources but have failed to find out what the problem is. Please kindly help me to find out what is wrong with my code.

conv1 = ConvolutionLayer[20, {5, 5}, "PaddingSize" -> 0, 
   "Stride" -> 1 ];
conv2 = ConvolutionLayer[50, {5, 5}, "PaddingSize" -> 0, 
   "Stride" -> 1];
block1 = {conv1, Ramp, PoolingLayer[2, 2], conv2, Ramp, 
   PoolingLayer[2, 2], FlattenLayer[], 500, Ramp, class, 
   SoftmaxLayer[]};

leNet = NetChain[block1, 
  "Output" -> 
   NetDecoder[{"Class", {"0", "1", "2", "3", "4", "5", "6", "7", "8", 
      "9"}}], "Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}]]

trainedNet = 
 NetTrain[leNet, trainingData, ValidationSet -> testingData, 
  MaxTrainingRounds -> maxTrainingRounds, BatchSize -> batchSize]

weights = NetExtract[leNet, {"conv1", "Weights"}];
Dimensions[weights]

out = { }

ImageAdjust[Image[#, Interleaving -> False]] & /@ weights

out = Missing["NotPresent", {"conv1", "Weights"}]
POSTED BY: Ashish Sharma
2 Replies
Posted 6 years ago

I thank you so much for giving me the solution and such a quick response.

POSTED BY: Ashish Sharma

Ashish, please change some codes.

leNet = NetChain[block1, 
  "Output" -> NetDecoder[{"Class", {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}}], 
  "Input" -> NetEncoder[{"Image", {28, 28}, "Grayscale"}]]

weights = NetExtract[trainedNet, {1, "Weights"}];

You can visualize weights of conv1(Layer 1).

ImageAdjust[Image[#, Interleaving -> False]] & /@ weights

enter image description here

POSTED BY: Kotaro Okazaki
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract