FAQ
Hi, I'm working on writing a simple neural network code, and am having some
trouble getting my head around the proper way to create interfaces.

I like the gist of what I have so far
http://play.golang.org/p/nwFOIzZjMS

but the problem is that if one is doing classification, the final layer
should just be a weighted sum of the outputs instead of the sigmoid of the
weighted sum. One way is to hard code in that rule, but it seems like it
would be cleaner, more generalizable, and more go-like to make the neuron
class an interface, and then have SigmoidNeurons, linear neurons, etc. I'm
having trouble deciding how to best use an interface to capture this
difference.

A neuron has a vector of weights, and a function which takes in a vector of
floats and returns a float. One way is to define an interface with a getter
on the weight

type neuron interface{
GetWeight(int) float64
Process([] float64) float64
}

and then have SigmoidNeuron and LinearNeuron each get GetWeight and Process
methods. However, it seems like this would repeat a lot of code, as the
GetWeight function would be identical, and the process functions would be
almost identical instead of the last step. I tried to follow the example of
effective go with embedding, to do something like:
http://play.golang.org/p/GkprmeHpXE , where the final transformation of the
weighted sum is embedded as an interface, and then that interface is called
as the final step of the process code.

which gives the error of "Embedded type cannot be a pointer to interface".
The error is crystal clear (yay go), but I do not understand how to
accomplish what I am trying to do by doing struct embedding rather than
interface embedding. Thank you

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Nigel Tao at Feb 5, 2013 at 10:59 pm

    On Wed, Feb 6, 2013 at 9:46 AM, Brendan Tracey wrote:
    which gives the error of "Embedded type cannot be a pointer to interface".
    I'm not exactly sure what you're trying to do, but you rarely want
    pointers to interface types. Instead, if you want to embed an
    interface, just, uh, embed the interface (without the *):

    http://play.golang.org/p/b8et-emBXU

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Feb 5, 2013 at 11:01 pm
    How would I initialize the interface to the specific one I want?
    On Tuesday, February 5, 2013 2:59:20 PM UTC-8, Nigel Tao wrote:
    On Wed, Feb 6, 2013 at 9:46 AM, Brendan Tracey wrote:
    which gives the error of "Embedded type cannot be a pointer to
    interface".

    I'm not exactly sure what you're trying to do, but you rarely want
    pointers to interface types. Instead, if you want to embed an
    interface, just, uh, embed the interface (without the *):

    http://play.golang.org/p/b8et-emBXU
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nigel Tao at Feb 5, 2013 at 11:02 pm

    On Wed, Feb 6, 2013 at 10:01 AM, Brendan Tracey wrote:
    How would I initialize the interface to the specific one I want?
    Isn't that the
    n := newNeuron(10, &sigmoidneuron{})
    in http://play.golang.org/p/b8et-emBXU

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Brendan Tracey at Feb 5, 2013 at 11:21 pm
    Oh wow, that's really simple. Thanks a lot.

    One final question, in the full version of the code,
    http://play.golang.org/p/QkcT1UF-Qm

    I'm confused as to what's happening in the newLayer script. In the loop
    over neurons, I'm passing the same reshaper. I would imagine this means
    that each neuron in the layer has a pointer to the same sigmoidneuron.
    Since it is an empty struct, is this a problem? What if I wanted to compute
    all the neurons at the same time? Should I instead be using the reflect
    package to create a new sigmoid neuron each time in the loop?

    Thanks

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Nigel Tao at Feb 6, 2013 at 12:05 am
    On Wed, Feb 6, 2013 at 10:21 AM, Brendan Tracey > I'm confused as to
    what's happening in the newLayer script. In the loop over
    neurons, I'm passing the same reshaper. I would imagine this means that each
    neuron in the layer has a pointer to the same sigmoidneuron. Since it is an
    empty struct, is this a problem?
    No problem; sharing a pointer to the empty struct is fine.

    P.S. make([]T, n, n) is equivalent to make([]T, n), but the latter
    form is idiomatic when the len equals the cap.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedFeb 5, '13 at 10:46p
activeFeb 6, '13 at 12:05a
posts6
users2
websitegolang.org

2 users in discussion

Nigel Tao: 3 posts Brendan Tracey: 3 posts

People

Translate

site design / logo © 2022 Grokbase