# Unable to attach a new NetDecoder to a NetModel following the documentation

I am following the documentation in the page https://reference.wolfram.com/language/ref/NetModel.html.

I am getting error when I try to execute the code given for attaching NetDecoder for a given model.

NetModel["LeNet", "Output" -> NetDecoder[{"Class", {"cat", "dog"}}]]


Output

NetReplacePart::invspdec: NetDecoder[{"Class", \[Ellipsis]}] taking a length-2 vector of real numbers, cannot be attached to port Output, which produces a length-10 vector of real numbers.

Can someone explain me what is wrong here?

The simplest way I can find to replace the output decoder is by copying over the net up to the last Ramp layer and then adding the rest back in manually:

net = NetModel["LeNet"];
net = NetFlatten @ NetChain[
{ NetTake[net, 9], (* the Ramp layer has index 9 *)
LinearLayer[], SoftmaxLayer[]},
"Output" -> NetDecoder[{"Class", {"cat", "dog"}}]
]

• Yes, this is the way I'd normally do it as well. I wish NetReplace had a nice use-case here as well but it doesn't seem like it. – Carl Lange Oct 9 '19 at 14:55
• Thank you @Sjoerd Smit – Kr Sudharsan Oct 9 '19 at 16:28

First and foremost, this example given by the documentation won't evaluate and (as far as I'm aware) never could have evaluated. You didn't do anything wrong here; it's a documentation bug. The following explains why it doesn't evaluate.

LeNet's "Output" port (before decoding) is a vector of reals of length 10 - in the case of LeNet, it outputs something approximating the probabilities that the input was a 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9.

LeNet has a NetDecoder by default, which we can look at with NetExtract[NetModel["LeNet"], "Output"].

This is also a "Class" NetDecoder, but unlike yours it has 10 labels. This means that the output from the network (remember, a length-10 vector of probabilities per class) can be mapped to this list of labels.

The NetDecoder that you've defined only has two labels ("cat" and "dog"), and so the decoder doesn't match up with the network output.

• In all fairness to the OP, though, the line NetModel["LeNet", "Output" -> NetDecoder[{"Class", {"cat", "dog"}}]] is verbatim from the documentation of NetModel (3rd line in the Scope section). – Sjoerd Smit Oct 9 '19 at 13:03
• Totally agree, it's a bug in the documentation to have given an example that won't evaluate. – Carl Lange Oct 9 '19 at 13:08
• @CarlLange, could you please tell how to change the number of labels in network? That would be helpful – Kr Sudharsan Oct 9 '19 at 13:11
• @KrSudharsan Sorry, I missed this; Sjoerd's answer is exactly what I would have added. – Carl Lange Oct 9 '19 at 15:32

Thanks for spotting the documentation error and the limitation of NetReplacePart.

Good news for the future: NetReplacePart will be improved in the next version.

In 12.1, it will be possible to write:

NetReplacePart[NetModel["LeNet"],
{10 -> LinearLayer[], "Output" -> NetDecoder[{"Class", {"cat", "dog"}}]}
]


(replacing the layer(s) that need to be "reshaped", additionally to the output)