0

Hello I have been porting Karpathy's ReinforceJs to C# and I was doing OK until I ran into this

    backward: function() {
      for(var i=this.backprop.length-1;i>=0;i--) {
      this.backprop[i](); // tick!
    }

backprop is an array in Graph and as you can tell from above the array contains fuctions here how they are stored:

    if(this.needs_backprop) {
    var backward = function() {
    for(var i=0,n=d;i<n;i++){ m.dw[d * ix + i] += out.dw[i]; }
    }
    this.backprop.push(backward);
    }

I had just converted this(backward) into a function that runs when needs_backprop is true but after testing my fuctions just produce the same action every time C# code in repo summary:

    if (NeedsBackPropagation)
    {
        Backward(m, outt, n);
    }

and Backward:

    public void Backward(Matrix m, Matrix outt,int n)
    {
        for (var i = 0; i < n; i++)
        {
            // grad for z = tanh(x) is (1 - z^2)
            var mwi = outt.Weights[i];
            m.BackPropWeights[i] += (1.0 - mwi * mwi)* outt.BackPropWeights[i];
       }
    }

I want to know A. Straight up storing functions in array like he did then call them later or B. the equivalent of this in C#, I wouldn't mind contributions to the repository itself

1
  • 1
    You can store functions in List<Action> _backprop = new List<Action>(). Then add like this: _backprop.Add(() => { function here });. And call the same way as in javascript: _backprop[i](). Commented Dec 18, 2017 at 8:12

1 Answer 1

1

You can store functions in c# too, here's an example using your code:

public class Graph
{

    public bool NeedsBackPropagation { get; }
    public List<Action> BackProp { get; }


    public Graph(bool backProp)
    {
        NeedsBackPropagation = backProp;
        BackProp = new List<>();
    }


    public Matrix RowPluck(Matrix m, int ix)
    {
        Util.Assert(ix >= 0 && ix < m.NumberOfRows);
        var d = m.NumberOfColumns;
        var outt = new Matrix(d, 1);
        for (int i = 0, n = d; i < n; i++)
        {
            outt.Weights[i] = m.Weights[d * ix + i];
        }

        if (NeedsBackPropagation)
        {
            BackProp.Add(new Action(() => {
                for (int i = 0, n = d; i < n; i++)
                {
                    m.BackPropWeights[d * ix + i] += outt.BackPropWeights[i];
                }
            }));
        }

        return outt;
    }

}

And then all you need to do to call them is

foreach (Action action in BackProp)
{
    action();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.