移植JavaScript以C#(存储在阵列功能)

你好,我已经移植Karpathy's ReinforceJs到C#和我做的不错,直到我遇到了这个移植JavaScript以C#(存储在阵列功能)

backward: function() { 

for(var i=this.backprop.length-1;i>=0;i--) {

this.backprop[i](); // tick!

}

backprop是图一个数组,因为你可以告诉上面的数组包含这里fuctions它们是如何存储:

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

}

我刚刚转换这个(向后)成时needs_backprop是真正运行的功能,但在测试我的fuctions后每次只产生相同的作用C# code in repo摘要:

if (NeedsBackPropagation) 

{

Backward(m, outt, n);

}

和落后:

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

}

}

我想知道A.直上在阵列中存储的功能,如他后来打电话给他们或B.这在C#中的等价物,我不会介意库本身

回答:

可以存储功能,在C#中也贡献,这是一个使用你的代码的例子:

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;

}

}

然后你需要做的,叫他们是

foreach (Action action in BackProp) 

{

action();

}

以上是 移植JavaScript以C#(存储在阵列功能) 的全部内容, 来源链接: utcz.com/qa/261612.html

回到顶部