Skip to content

Instantly share code, notes, and snippets.

@gognjanovski
Last active December 14, 2018 09:25
Show Gist options
  • Save gognjanovski/fe4bdadb5c363755af0b7b250f434a88 to your computer and use it in GitHub Desktop.
Save gognjanovski/fe4bdadb5c363755af0b7b250f434a88 to your computer and use it in GitHub Desktop.
Feedforward Neural Network Cost Function
...
% Feedforward the neural network and return the cost in the variable J
summary = 0;
for j = 1:m
y_label = y(j,:);
a_one = X(j,:);
a_one = [1 a_one];
z_one = a_one * Theta1';
a_two = sigmoid(z_one);
m_temp = size(a_two, 1);
a_two = [ones(m_temp, 1) a_two];
z_two = a_two * Theta2';
a_three = sigmoid(z_two);
h = a_three;
sum_temp = 0;
sum_temp = (-y_label .* log(h)) - ((1 - y_label) .* log(1 - h));
summary = summary + sum(sum_temp);
...
end
...
% Add Regularization
regularization = (lambda/(2*m)) * (sum(sum(Theta1(:,2:end) .^ 2, 2)) + sum(sum(Theta2(:,2:end) .^ 2, 2)));
J = (summary/m) + regularization;
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment