How can I create and train a neural network with a scalar Boolean output for binary classification, and hopefully have it automatically compute the "ErrorRate" property (described here) during training?

Here is an attempt that resulted in errors:

(* create training data *)

rmf = RegionMember[Disk[]];
pts = RandomReal[{-1, 1}, {1000, 2}];
data = Thread[pts -> rmf[pts]];

(* construct net *)

net = NetChain[{100, Ramp, 1}, "Input" -> 2, "Output" -> NetDecoder["Boolean"]]

(* attempt training *)

NetTrain[net, data]

This results in:

First::nofirst: {} has zero length and no first element.

NetTrain::encgenfail1: Could not encode input number 459 for port "Output": input was not a sequence of length 1. Please check the example.

I am not experienced with neutral networks. I am generally confused about how to specify that the output of a net is even a scalar (not a vector). In the example above, this does not seem to be the problem as changing the training data to use size-1 Boolean vectors instead of Boolean scalars does not fix the error.


You can attach a SequenceLastLayer to your chain to get this to work:

net = NetChain[{100, Ramp, 1, SequenceLastLayer[]}, "Input" -> 2, "Output" -> NetDecoder["Boolean"]]

Normally, to get a network to output a scalar, you can do "Output"->"Scalar", but since you're already using a NetDecoder, this doesn't work easily. A SequenceLastLayer always returns a scalar, so this is an easy way to work around this issue.

It does seem to me that the boolean NetDecoder could handle this a little better. You can set the "InputDepth" of the NetDecoder like so: NetDecoder[{"Boolean", "InputDepth"->2}], but you should also be able to specify that the input should be a scalar here.

| improve this answer | |
  • $\begingroup$ +1. Similar trick is PartLayer[1]. $\endgroup$ – Silvia Sep 17 '19 at 17:09

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.