0

Can anyone code a simple sigmoid function in vb.net? I have tried and I don't think it works. If anyone could suggest any improvements and/or fixes, I would be very happy. Thanks!

        Function sigmoid(inputs As Single)
        Dim finish As New Decimal
        finish = 1 / 1 + 2.71828183 ^(-inputs)
        Return finish
        End Function

1 Answers1

2

The division operator has a higher precendence than the addition operator, so your function is calculating (1/1) + e^-x. All you need to do is add brackets to force the evaluation to be 1/(1+e^-x).

There is a built in e^x function.

There is probably no need to use a Single rather than a Double.

You need to declare the return type of the function - a Double is probably better for what you are using it for than a Decimal. The use of Option Strict On would have told you about the former; I strongly recommend that you use it and set it to be the default for new VB projects.

Function sigmoid(x As Double) As Double
    Return 1 / (1 + Math.Exp(-x))
End Function

Please see Operator Precedence in Visual Basic and Option Strict On.

Andrew Morton
  • 24,203
  • 9
  • 60
  • 84