# Scalar vs vector of size 1 in neural networks

I've been playing with Mathematica's neural network functions, and I keep getting stuck on the same annoying problem. Often Mathematica declares that the output of my neural network is a vector of size 1, rather than a scalar. The only way around this problem I found is to train an uninitialized network with scalar expected output, and then the network has output "scalar". But sometimes I can't do this, for example I have an existing network which I just want to modify by adding some extra layers, and then I'm stuck. Any suggestions?

ex: given a network x that has scalar as output, try

NetFlatten[NetChain[{x, Tanh}]]


the output will be a vector of size 1... trying

NetFlatten[NetChain[{x, Tanh}, "Output" -> "Real"]]


produces the error message Update: still stuck on this one, it's really frustrating. An even simpler version of the same problem: given a chain x that outputs scalar, try

NetChain[Normal[x]]


the result outputs a vector of size 1.

• Perhaps you could add an example network "x" so that we can copy-paste an exact test? – Carl Lange May 21 '19 at 8:40
• Well, that's part of the problem -- I don't know how to define a neural network that has scalar as output. A typical chain might be NetChain[{50,Ramp,50,Ramp,50,Ramp,1,Tanh}] but to get it to have scalar output the only way I found is to train it (with NetTrain) with scalar expected output. – Paul Zinn-Justin May 22 '19 at 3:07

Posting the answer I finally found. A neural network with output "scalar" secretly has a NetDecoder attached to it. What works in the example above would be
NetFlatten[NetChain[{x, Tanh}, "Output" -> NetDecoder["Scalar"]]]