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 enter image description here

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


the result outputs a vector of size 1.

  • $\begingroup$ Perhaps you could add an example network "x" so that we can copy-paste an exact test? $\endgroup$ – Carl Lange May 21 '19 at 8:40
  • 1
    $\begingroup$ 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. $\endgroup$ – 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"]]]
  • $\begingroup$ Searched for this for a long time, thanks! $\endgroup$ – smörkex Jan 11 at 21:52

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.