神经网络代价函数在Matlab中返回Nan

时间:2018-11-11 19:20:42

标签: matlab machine-learning neural-network octave

我正在研究NN,并试图生成学习和验证曲线。

这是我的向量化成本函数

sum(sum((-Y).*log(h) - (1-Y).*log(1-h), 2))/m + lambda*p/(2*m);

但是,当我在计算验证错误之前在较小的训练集大小上训练参数时,验证错误返回Nan。

这是我要实现的代码:

function [error_train, error_val, m] = ...
learningCurve(input_layer_size,hidden_layer_size,num_labels,X, y, Xval, yval, lambda)
% returns the train and
% cross validation set errors for a learning curve.
% Number of training examples
m = size(X,1);

% You need to return these values correctly
error_train = zeros(m, 1);
error_val   = zeros(m, 1);


for i = 1:m
    nn_params = trainNeural(input_layer_size, hidden_layer_size, num_labels,X(1:i, :), y(1:i), lambda); 
    [error_train(i), grad] = nnCostFunction(nn_params, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, ...
                X(1:m), :), y(1:m), 0);

    [error_val(i), grad] = nnCostFunction(nn_params, ...
                                   input_layer_size, ...
                                   hidden_layer_size, ...
                                   num_labels, ...
                   Xval, yval,0);
end

0 个答案:

没有答案