AND/OR Perceptron

View: New views
6 Messages — Rating Filter:   Alert me  

AND/OR Perceptron

by Hector Guilarte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi cafe,

I'm trying to implement a Perceptron in Haskell and I found one in: http://jpmoresmau.blogspot.com/2007/05/perceptron-in-haskell.html (Thanks JP Moresmau) but there is one line I don't understand, I was wondering if someone could explain it to me. I know the theory behind a perceptron, my question is more about the Haskell syntax in that line I don't understand.

epoch :: [([Float],Float)] -> -- ^ Test Cases and Expected Values for each test case
                   [Float] -> -- ^ weights
              ([Float],Float) -- ^ New weights, delta
epoch allInputs weights=
    let
        f w (inputs,expected) = step inputs w expected -- I don't understand this line
        newWeights = foldl f weights allInputs -- Neither this one
        delta = (foldl (+) 0 (map abs (zipWith (-) newWeights weights))) / (fromIntegral $ length weights)
    in (newWeights,delta)

What is f and what is w? I really don't get it, Is like it is defining a function f which calls step unziping the input, taking one of the elements from the fst and it's corresponding snd and invoking step with that, along with w (which seems to be a list according to step's signature but I don't know where it comes from), and then applying fold to the weights and all the Inputs using that f function... But I don't get it!

Maybe if someone could rewrite that redefining f as an separate function and calling fold with that function I'll get it.

The input for epoch would be something like this:
epoch [([0,0],0),([0,1],0),([1,0],0),([1,1],1)] [-0,413,0.135]

and the output for that examples is:
([0.0,412.9],3.333537e-2)


Thanks a lot,

Hector Guilarte

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: AND/OR Perceptron

by Tim Wawrzynczak :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hector,

That line is declaring a function named 'f' of two arguments: one is 'w', and the other is a tuple.  The tuple's fst is 'inputs', and its snd is 'expected.'  This function (f) is used in the next line, in the declaration of the list 'newWeights,' which uses f as the function which does the fold over the allInputs list.

Cheers,
 - Tim

On Thu, Oct 29, 2009 at 2:27 PM, Hector Guilarte <hectorg87@...> wrote:
Hi cafe,

I'm trying to implement a Perceptron in Haskell and I found one in: http://jpmoresmau.blogspot.com/2007/05/perceptron-in-haskell.html (Thanks JP Moresmau) but there is one line I don't understand, I was wondering if someone could explain it to me. I know the theory behind a perceptron, my question is more about the Haskell syntax in that line I don't understand.

epoch :: [([Float],Float)] -> -- ^ Test Cases and Expected Values for each test case
                   [Float] -> -- ^ weights
              ([Float],Float) -- ^ New weights, delta
epoch allInputs weights=
    let
        f w (inputs,expected) = step inputs w expected -- I don't understand this line
        newWeights = foldl f weights allInputs -- Neither this one
        delta = (foldl (+) 0 (map abs (zipWith (-) newWeights weights))) / (fromIntegral $ length weights)
    in (newWeights,delta)

What is f and what is w? I really don't get it, Is like it is defining a function f which calls step unziping the input, taking one of the elements from the fst and it's corresponding snd and invoking step with that, along with w (which seems to be a list according to step's signature but I don't know where it comes from), and then applying fold to the weights and all the Inputs using that f function... But I don't get it!

Maybe if someone could rewrite that redefining f as an separate function and calling fold with that function I'll get it.

The input for epoch would be something like this:
epoch [([0,0],0),([0,1],0),([1,0],0),([1,1],1)] [-0,413,0.135]

and the output for that examples is:
([0.0,412.9],3.333537e-2)


Thanks a lot,

Hector Guilarte

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: AND/OR Perceptron

by Hector Guilarte :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Thanks Tim! I got it! I have never declared a function before in a let ... in statement, I always do it in a where after I call it...

On Fri, Oct 30, 2009 at 3:03 PM, Tim Wawrzynczak <inforichland@...> wrote:
Hector,

That line is declaring a function named 'f' of two arguments: one is 'w', and the other is a tuple.  The tuple's fst is 'inputs', and its snd is 'expected.'  This function (f) is used in the next line, in the declaration of the list 'newWeights,' which uses f as the function which does the fold over the allInputs list.

Cheers,
 - Tim

On Thu, Oct 29, 2009 at 2:27 PM, Hector Guilarte <hectorg87@...> wrote:
Hi cafe,

I'm trying to implement a Perceptron in Haskell and I found one in: http://jpmoresmau.blogspot.com/2007/05/perceptron-in-haskell.html (Thanks JP Moresmau) but there is one line I don't understand, I was wondering if someone could explain it to me. I know the theory behind a perceptron, my question is more about the Haskell syntax in that line I don't understand.

epoch :: [([Float],Float)] -> -- ^ Test Cases and Expected Values for each test case
                   [Float] -> -- ^ weights
              ([Float],Float) -- ^ New weights, delta
epoch allInputs weights=
    let
        f w (inputs,expected) = step inputs w expected -- I don't understand this line
        newWeights = foldl f weights allInputs -- Neither this one
        delta = (foldl (+) 0 (map abs (zipWith (-) newWeights weights))) / (fromIntegral $ length weights)
    in (newWeights,delta)

What is f and what is w? I really don't get it, Is like it is defining a function f which calls step unziping the input, taking one of the elements from the fst and it's corresponding snd and invoking step with that, along with w (which seems to be a list according to step's signature but I don't know where it comes from), and then applying fold to the weights and all the Inputs using that f function... But I don't get it!

Maybe if someone could rewrite that redefining f as an separate function and calling fold with that function I'll get it.

The input for epoch would be something like this:
epoch [([0,0],0),([0,1],0),([1,0],0),([1,1],1)] [-0,413,0.135]

and the output for that examples is:
([0.0,412.9],3.333537e-2)


Thanks a lot,

Hector Guilarte

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe




_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: AND/OR Perceptron

by Ryan Ingram :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, Oct 29, 2009 at 1:32 PM, Hector Guilarte <hectorg87@...> wrote:
Thanks Tim! I got it! I have never declared a function before in a let ... in statement, I always do it in a where after I call it...
 
"where" is just syntactic sugar for "let...in"

  -- ryan

_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: AND/OR Perceptron

by Bugzilla from chaddai.fouche@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Sat, Oct 31, 2009 at 8:09 PM, Ryan Ingram <ryani.spam@...> wrote:
>
> "where" is just syntactic sugar for "let...in"

That's not perfectly true : "where" and "let...in" don't attach to the
same syntactic construction, "where" attaches to a definition and can
works for several guard clauses whereas "let ... in expression" is an
expression in itself.

--
Jedaï
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe

Re: AND/OR Perceptron

by Ryan Ingram :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I suppose I wasn't entirely clear.

"where" is syntactic sugar for "let...in", pattern matching is syntactic sugar for "case", and guards are syntactic sugar for "if..then..else" and/or "case" (for pattern guards)

In fact, the whole reason for the existence of "where" is so that it can attach at a higher-level scope and make use of the other syntactic sugar (like guards).  Also, I find the postfix definitions in "where" to generally be more readable, but that is a matter of taste.

  -- ryan

On Sat, Oct 31, 2009 at 11:33 AM, Chaddaï Fouché <chaddai.fouche@...> wrote:
On Sat, Oct 31, 2009 at 8:09 PM, Ryan Ingram <ryani.spam@...> wrote:
>
> "where" is just syntactic sugar for "let...in"

That's not perfectly true : "where" and "let...in" don't attach to the
same syntactic construction, "where" attaches to a definition and can
works for several guard clauses whereas "let ... in expression" is an
expression in itself.

--
Jedaï


_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@...
http://www.haskell.org/mailman/listinfo/haskell-cafe