-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Common useful functions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
function numgrad = computeNumericalGradient(J, theta) | ||
%COMPUTENUMERICALGRADIENT Computes the gradient using "finite differences" | ||
%and gives us a numerical estimate of the gradient. | ||
% numgrad = COMPUTENUMERICALGRADIENT(J, theta) computes the numerical | ||
% gradient of the function J around theta. Calling y = J(theta) should | ||
% return the function value at theta. | ||
|
||
% Notes: The following code implements numerical gradient checking, and | ||
% returns the numerical gradient.It sets numgrad(i) to (a numerical | ||
% approximation of) the partial derivative of J with respect to the | ||
% i-th input argument, evaluated at theta. (i.e., numgrad(i) should | ||
% be the (approximately) the partial derivative of J with respect | ||
% to theta(i).) | ||
% | ||
|
||
numgrad = zeros(size(theta)); | ||
perturb = zeros(size(theta)); | ||
e = 1e-4; | ||
for p = 1:numel(theta) | ||
% Set perturbation vector | ||
perturb(p) = e; | ||
loss1 = J(theta - perturb); | ||
loss2 = J(theta + perturb); | ||
% Compute Numerical Gradient | ||
numgrad(p) = (loss2 - loss1) / (2*e); | ||
perturb(p) = 0; | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
function [y] = l2row(x) | ||
normeps = 1e-8; | ||
epssumsq = sum(x.^2, 2) + normeps; | ||
l2rows = sqrt(epssumsq); | ||
y = bsxfun(@rdivide, x, l2rows); | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
function [grad] = l2rowg(x, y, outderv) | ||
%L2ROWG Assumes examples in columns | ||
|
||
normeps = 1e-8; | ||
if (~exist('outderv','var')||isempty(outderv)) | ||
error('Requires outderv of previous layer to compute gradient!'); | ||
end | ||
|
||
epssumsq = sum(x.^2,2) + normeps; | ||
|
||
l2rows = sqrt(epssumsq); | ||
|
||
if (~exist('y','var')||isempty(y)) | ||
y = bsxfun(@rdivide,x,l2rows); | ||
end | ||
|
||
grad = bsxfun(@rdivide, outderv, l2rows) - ... | ||
bsxfun(@times, y, sum(outderv.*x, 2) ./ epssumsq); | ||
end |