Scratch的MATLAB双层神经网络

时间:2017-12-27 22:54:24

标签: matlab neural-network backpropagation

目前,出于调试原因,我正在从头开始研究一个简单的两层NN(25输入 - sigmoid,199输出 - softmax) - 确切地说,我想跟踪一些值。

我的输入是批量或一般说来的维度矩阵(行x 25),以便适合输入层结构。关于我的权重矩阵:第一行但最后一行是权重w_ij。最后一行包括偏见。

前进方法似乎工作正常,但我认为我有一个错误的反向传播。

我的反向传播代码段:

%Error gradient for the softmax output
error = single(output) - single(targets);

%Error for the input layer - W21 includes w_ij
error_out_to_input = error*(W21.');

gradient_outputLayer = single(zeros(26,199));
gradient_outputLayer = single(first_layerout_zerofilled.')*single(error);

biasGrad = single(sum(error,1));
gradient_outputLayer(26:26,:) = single(biasGrad);

%InputLayer

%derivative of sigmoid  o(1-o) 

%1
grad = single(1);
%1-o
grad = single(grad) - single(first_layerout_zerofilled);
%o(1-o)
grad = single(first_layerout_zerofilled) .* single((grad));

%final error
grad = single(grad) .* single(error_out_to_input);


gradient_inputLayer = single(zeros(26,25));
gradient_inputLayer = single(inputs.')*single(grad);

biasGrad = single(sum(grad,1));
gradient_inputLayer(26:26,:) = single(biasGrad);


%Update
W1 = W1-gradient_inputLayer  * learning_rate;
W2 = W2-gradient_outputLayer * learning_rate;

这不是效率问题。我只想确保我的backprogation计算出正确的渐变。我希望有人可以复习。

0 个答案:

没有答案