AlKobtan - 10 months ago 46

Vb.net Question

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
```

- Extensions methods tend to work better when the Module is
`Public`

. Otherwise, you might not see the method available when you need it. `Return Nothing`

had no meaning, as it directly followed another return statement.- The
`y`

input to the function was never used, so don't ask for it. - 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. - 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.