2
$\begingroup$

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.

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

1 Answer 1

5
$\begingroup$

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.}

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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