# How to make the output of NetTrain obey constraints?

Imagine I have this

     trained = NetTrain[LinearLayer[], {1 -> {0.1,0.7,0.2}, 2 ->
{0,0,1}, 3 -> {0.4,0.4,0.2}, 4 -> {0.1,0.0,0.9}}]


I want to make sure that trained[5] or even trained[1] itself, ALWAYS gives an output like {a,b,c} where a+b+c = 1 and a,b,c are between 0 and 1.

• That's what the SoftmaxLayer is for. Commented Mar 22, 2022 at 9:20
• Please give me the actual code that uses this for this purpose ... Commented Mar 23, 2022 at 6:15

To follow up from my comment: training networks that output probability vectors is such a common use case that there is a standard way to do it. Any NetChain that ends with SoftmaxLayer will always produce an output vector (of non-negative values) that sums to one:

trained = NetTrain[
NetChain[{LinearLayer[], SoftmaxLayer[]}],
{1 -> {0.1, 0.7, 0.2}, 2 -> {0, 0, 1}, 3 -> {0.4, 0.4, 0.2}, 4 -> {0.1, 0.0, 0.9}}
]


Check:

trained[RandomReal[1, {5}]]
Total /@ %


{{0.40406, 0.397489, 0.198451}, {0.353724, 0.35222, 0.294056}, {0.394641, 0.389201, 0.216158}, {0.341313, 0.340755, 0.317933}, {0.348998, 0.347866, 0.303136}}

{1., 1., 1., 1., 1., 1., 1., 1., 1., 1.}

• Does this method guarantee that each element of the output is going to be between 0 and 1 not just sum to 1 ? Commented Mar 23, 2022 at 8:56
• Yes, a SoftmaxLayer always produces vector of probabilities. That's what it's made for. Commented Mar 23, 2022 at 8:58
• Thank you. That was very helpful. Commented Mar 23, 2022 at 9:02