Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vectorize #2

Open
webbp opened this issue Mar 29, 2021 · 1 comment
Open

vectorize #2

webbp opened this issue Mar 29, 2021 · 1 comment

Comments

@webbp
Copy link

webbp commented Mar 29, 2021

If you vectorize cost and backprop, 50 epochs takes 2.4 seconds instead of 150 seconds.

Vectorized cost: replace this loop with

yy = zeros(size(y,1),num_labels);
yy(sub2ind(size(yy),1:length(y),y'))=1;
J = sum(sum(- yy .* log(a3) - (1 - yy) .* log(1 - a3)))/m;

Vectorized backprop: replace this loop with

d3 = a3 - yy;
d2 = (d3 * Theta2) .* [ones(m, 1) sigmoidGradient(z2)];
d2 = d2(:,2:end);
Theta1_grad = d2' * a1;
Theta2_grad = d3' * a2;
@Zhenye-Na
Copy link
Owner

Do you wanna make a PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants