Luke Xu
Luke Xu

Reputation: 2440

Why does the pipe backward operator not work in this case? 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

Upvotes: 3

Views: 131

Answers (2)

scrwtp
scrwtp

Reputation: 13577

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

Upvotes: 5

yuyoyuppe
yuyoyuppe

Reputation: 1602

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

Upvotes: 3

Related Questions