Gradient w ciągłej regresji przy użyciu sieci neuronowej

Usiłuję zaimplementować regresję NN, która ma 3 warstwy (1 wejście, 1 ukryta i 1 wyjściowa warstwa z ciągłym wynikiem). Jako podstawę podjąłem klasyfikację NN zcoursera.org klasa, ale zmieniła funkcję kosztu i obliczenia gradientu tak, aby pasowały do ​​problemu regresji (a nie klasyfikacji):

Moja nnCostFunction jest teraz:

function [J grad] = nnCostFunctionLinear(nn_params, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, ...
                                   X, y, lambda)

Theta1 = reshape(nn_params(1:hidden_layer_size * (input_layer_size + 1)), ...
                 hidden_layer_size, (input_layer_size + 1));

Theta2 = reshape(nn_params((1 + (hidden_layer_size * (input_layer_size + 1))):end), ...
                 num_labels, (hidden_layer_size + 1));

m = size(X, 1);

a1 = X;
a1 = [ones(m, 1) a1];
a2 = a1 * Theta1';
a2 = [ones(m, 1) a2];
a3 = a2 * Theta2';
Y = y;

J = 1/(2*m)*sum(sum((a3 - Y).^2))

th1 = Theta1;
th1(:,1) = 0; %set bias = 0 in reg. formula
th2 = Theta2;
th2(:,1) = 0;

t1 = th1.^2;
t2 = th2.^2;
th = sum(sum(t1)) + sum(sum(t2));
th = lambda * th / (2*m);
J = J + th; %regularization


del_3 = a3 - Y;
t1 = del_3'*a2;
Theta2_grad = 2*(t1)/m + lambda*th2/m;

t1 = del_3 * Theta2;
del_2 = t1 .*  a2;
del_2 = del_2(:,2:end);
t1 = del_2'*a1;
Theta1_grad = 2*(t1)/m + lambda*th1/m;

grad = [Theta1_grad(:) ; Theta2_grad(:)];
end

Następnie używam tej funkcji wfmincg algorytm, ale w pierwszych iteracjach fmincg kończy pracę. Myślę, że mój gradient jest zły, ale nie mogę znaleźć błędu.

Czy ktoś może pomóc?

questionAnswers(3)

yourAnswerToTheQuestion