Porting JavaScript to C# (functions stored in array)

时间:2017-12-18 08:02:12

标签: javascript c# function porting

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

sc.exe

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

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

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

and Backward:

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

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 个答案:

答案 0 :(得分:1)

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

import React, { Component } from 'react';
import DatePicker from 'material-ui/DatePicker';
import { Table, TableBody, TableHeader, TableHeaderColumn, TableRow, TableRowColumn } from 'material-ui/Table';

export default class AddTaskDialog extends Component {
  constructor(props) {
    super(props);
    this.state = { controlledDate: {} };
    this.handleChangeDate = this.handleChangeDate.bind(this);
  }

  handleChangeDate = (event, date) => {
    this.setState({
      controlledDate: date,
    });
  };

  render() {
    return (
      <div>
        <DatePicker hintText="Date" value={this.state.controlledDate} onChange={this.handleChangeDate}/>
        <Table>
          <TableHeader>
            <TableRow>
              <TableHeaderColumn>Date</TableHeaderColumn>
            </TableRow>
          </TableHeader>
          <TableBody>
            <TableRow>
              <TableRowColumn>{this.state.controlledDate}</TableRowColumn>
            </TableRow>
          </TableBody>
        </Table>
      </div>
    );
  }
}

And then all you need to do to call them is

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

}