AlKobtan AlKobtan - 6 months ago 20
Vb.net Question

how to create a module for segmoid function on VB?

I have tried this

Module module1
<System.Runtime.CompilerServices.Extension()> _
Public Function sigmoid(ByRef x As Integer, ByVal y As Integer) As Integer
y = 1 / (1 + Math.Exp(-x))
Return y
Return Nothing
End Function
End Module


and when I want to call it I use

y3 = (x1 * w13 + x2 * w23 - seta3).sigmoid


I just want to put x as (x1 * w13 + x2 * w23 - seta3)
and get the y value of it
it is related to neural network
but I got an error always .. what is the wrong ??

Answer

I see several things I would change for the Sigmoid function:

Public Module module1
    <System.Runtime.CompilerServices.Extension()> _
    Public Function Sigmoid(ByRef x As Integer) As Double
        Return 1 / (1 + Math.Exp(-x))
    End Function

    <System.Runtime.CompilerServices.Extension()> _
    Public Function Sigmoid(ByRef x As Double) As Double
        Return 1 / (1 + Math.Exp(-x))
    End Function
End Module
  1. Extensions methods tend to work better when the Module is Public. Otherwise, you might not see the method available when you need it.
  2. Return Nothing had no meaning, as it directly followed another return statement.
  3. The y input to the function was never used, so don't ask for it.
  4. You need a Double, rather than an Integer for the return type. It's unlikely that a division expression with a 1 for the first term will ever result in an workable Integer.
  5. This means you also want to use Double for the y3 variable, and that in turn makes we wonder how many other values here should really be doubles, hence the overloaded method.

Now you can call it like this:

y3 = (x1 * w13 + x2 * w23 - seta3).Sigmoid()

Note that in VB.Net, it's a best practice to always use the () notation when calling a function. This is not required, but still a good habit to have. This is a change from older VB6/VBA/VBScript code, where using parentheses had undesirable side effects in some cases.