Mbithy Mbithy
Mbithy Mbithy

Reputation: 163

Porting JavaScript to C# (functions stored in array)

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

Upvotes: 0

Views: 105

Answers (1)

Haytam
Haytam

Reputation: 4733

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();
}

Upvotes: 1

Related Questions