Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does the pipe backward operator not work in this case? F#

Tags:

f#

let J = (-1.0 * y) .* MatrixMath.log  <| Activation.sigmoid (X * theta)
let J = (-1.0 * y) .* MatrixMath.log  (Activation.sigmoid (X * theta))

I have the follow two sets of code with the former giving a compile error saying "This funciton takes too many arguements", but the latter works fine.

I thought the pipe backwards operator's goal is essentially to change the order of evaluation which is what I'm trying to do.

How do I write my expression without using parenthesis?

EDIT:

MatrixMath.log just does this

type MatrixMath =
    static member log X = X |> Matrix.map log
    static member log X = X |> Vector.map log
like image 665
Luke Xu Avatar asked Dec 03 '25 13:12

Luke Xu


2 Answers

Pipe operator has low precedence. What you try to evaluate is something like this:

let J = ((-1.0 * y) .* MatrixMath.log) <| (Activation.sigmoid (X * theta))

or a similar atrocity.

If you really need to put backwards pipe in there, try something like this:

let J = (-1.0 * y) .* (MatrixMath.log <| Activation.sigmoid <| X * theta)

or break it up in two lines:

let J = 
    let K = MatrixMath.log <| Activation.sigmoid (X * theta)
    (-1.0 * y) .* K
like image 123
scrwtp Avatar answered Dec 05 '25 07:12

scrwtp


In F# (<|) operator has left associativity, so your expression is parsed like this:

let J = ((-1.0 * y) .* MatrixMath.log) <| Activation.sigmoid (X * theta)

If you feel opposed to parentheses, you could possibly rewrite the code like this:

(*not tested*)
let flip f a b = f b a
let J = MatrixMath.log <| Activation.sigmoid (X * theta) |> flip (.*) <| -1.0 * y
like image 31
yuyoyuppe Avatar answered Dec 05 '25 06:12

yuyoyuppe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!