forked from pillowlab/neuroGLM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nonlinear functions imported from ncclabcode
- Loading branch information
Showing
9 changed files
with
194 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,33 @@ | ||
function [f,df,ddf] = logexp1(x) | ||
% [f,df,ddf] = logexp1(x); | ||
% | ||
% Also known as the soft-rectifier | ||
% | ||
% Computes the function: | ||
% f(x) = log(1+exp(x)) | ||
% and returns first and second derivatives | ||
|
||
f = log(1+exp(x)); | ||
|
||
if nargout > 1 | ||
df = exp(x)./(1+exp(x)); | ||
end | ||
if nargout > 2 | ||
ddf = exp(x)./(1+exp(x)).^2; | ||
end | ||
|
||
% Check for small values | ||
if any(x(:)<-20) | ||
iix = (x(:)<-20); | ||
f(iix) = exp(x(iix)); | ||
df(iix) = f(iix); | ||
ddf(iix) = f(iix); | ||
end | ||
|
||
% Check for large values | ||
if any(x(:)>500) | ||
iix = (x(:)>500); | ||
f(iix) = x(iix); | ||
df(iix) = 1; | ||
ddf(iix) = 0; | ||
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 [f,df,ddf] = logexp2(x); | ||
% [f,df,ddf] = logexp_pow(x); | ||
% | ||
% Implements the nonlinearity: | ||
% f(x) = log(1+exp(x)).^pow; | ||
% where pow=2, | ||
% plus first and second derivatives | ||
|
||
pow = 2; | ||
f0 = log(1+exp(x)); | ||
f = f0.^pow; | ||
|
||
if nargout > 1 | ||
df = pow*f0.^(pow-1).*exp(x)./(1+exp(x)); | ||
end | ||
if nargout > 2 | ||
ddf = pow*f0.^(pow-1).*exp(x)./(1+exp(x)).^2 + ... | ||
pow*(pow-1)*f0.^(pow-2).*(exp(x)./(1+exp(x))).^2; | ||
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,18 @@ | ||
function [f,df,ddf] = logexp3(x); | ||
% [f,df,ddf] = logexp3(x); | ||
% | ||
% Implements the nonlinearity: | ||
% f(x) = log(1+exp(x)).^3; | ||
% plus first and second derivatives | ||
|
||
|
||
f0 = log(1+exp(x)); | ||
f = f0.^3; | ||
|
||
if nargout > 1 | ||
df = 3*f0.^2.*exp(x)./(1+exp(x)); | ||
end | ||
if nargout > 2 | ||
ddf = 6*f0.*(exp(x)./(1+exp(x))).^2 + ... | ||
3*f0.^2.*exp(x)./(1+exp(x)).^2; | ||
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,23 @@ | ||
function [f,df,ddf] = logexp4(x) | ||
% [f,df,ddf] = logexp4(x); | ||
% | ||
% Implements the nonlinearity: | ||
% f(x) = log(1+exp(x)).^4; | ||
% plus first and second derivatives | ||
% | ||
% General formulas: | ||
% f(x) = log(1+exp(x))^k | ||
% f'(x) = k log(1+e^x)^(k-1) * e^x/(1+e^x); | ||
% f"(x) = k(k-1) log(1+e^x)^(k-2) * (e^x/(1+e^x))^2 | ||
% + k log(1+e^x)^(k-1) * e^x/(1+e^x)^2 | ||
|
||
f0 = log(1+exp(x)); | ||
f = f0.^4; | ||
|
||
if nargout > 1 | ||
df = 4*f0.^3.*exp(x)./(1+exp(x)); | ||
end | ||
if nargout > 2 | ||
ddf = 12*f0.^2.*(exp(x)./(1+exp(x))).^2 + ... | ||
4*f0.^3.*exp(x)./(1+exp(x)).^2; | ||
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,23 @@ | ||
function [f,df,ddf] = logexp5(x); | ||
% [f,df,ddf] = logexp4(x); | ||
% | ||
% Implements the nonlinearity: | ||
% f(x) = log(1+exp(x)).^4; | ||
% plus first and second derivatives | ||
% | ||
% General formulas: | ||
% f(x) = log(1+exp(x))^k | ||
% f'(x) = k log(1+e^x)^(k-1) * e^x/(1+e^x); | ||
% f"(x) = k(k-1) log(1+e^x)^(k-2) * (e^x/(1+e^x))^2 | ||
% + k log(1+e^x)^(k-1) * e^x/(1+e^x)^2 | ||
|
||
f0 = log(1+exp(x)); | ||
f = f0.^5; | ||
|
||
if nargout > 1 | ||
df = 5*f0.^4.*exp(x)./(1+exp(x)); | ||
end | ||
if nargout > 2 | ||
ddf = 20*f0.^3.*(exp(x)./(1+exp(x))).^2 + ... | ||
5*f0.^4.*exp(x)./(1+exp(x)).^2; | ||
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,22 @@ | ||
function [f,df,ddf] = logexp_pow(x,pow); | ||
% [f,df,ddf] = logexp_pow(x); | ||
% | ||
% Implements the nonlinearity: | ||
% f(x) = log(1+exp(x)).^pow; | ||
% plus first and second derivatives | ||
|
||
|
||
f0 = log(1+exp(x)); | ||
f = f0.^pow; | ||
|
||
if nargout > 1 | ||
df = pow*f0.^(pow-1).*exp(x)./(1+exp(x)); | ||
end | ||
if nargout > 2 | ||
if pow == 1 | ||
ddf = pow*f0.^(pow-1).*exp(x)./(1+exp(x)).^2; | ||
else | ||
ddf = pow*f0.^(pow-1).*exp(x)./(1+exp(x)).^2 + ... | ||
pow*(pow-1)*f0.^(pow-2).*(exp(x)./(1+exp(x))).^2; | ||
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,11 @@ | ||
function [f,df,ddf] = quadnl(x); | ||
% [f,df,ddf] = quadnl(x); | ||
% | ||
% Implements the nonlinearity: | ||
% f(x) = 1+x.^2; | ||
% Where pow = 1; | ||
% plus first and second derivatives | ||
|
||
f = 1+x.^2; | ||
df = 2.*x; | ||
ddf = 2; |
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,21 @@ | ||
function [f, df, ddf] = scaledNlfun(x, fhandle, scale) | ||
% [f, df, ddf] = scaledNlfun(x, fhandle, scale) | ||
% Scale a nonlinear function so that its operating point is shifted. | ||
% | ||
% Evaluates: fhandle(x*scale) / scale | ||
% | ||
% For example, to make log(1+exp(x)) more linear, use scale of 1e3 | ||
|
||
if nargout > 2 | ||
[f, df, ddf] = fhandle(x * scale); | ||
f = f / scale; | ||
df = df; | ||
ddf = ddf * scale; | ||
elseif nargout > 1 | ||
[f, df] = fhandle(x * scale); | ||
f = f / scale; | ||
df = df; | ||
else | ||
[f] = fhandle(x * scale); | ||
f = f / scale; | ||
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,24 @@ | ||
function [f, df, ddf] = threshLinear(x) | ||
% Threshold linear, nonlinearity | ||
% For numerical stability, the smallest value it returns is eps, not zero. | ||
% | ||
% / | ||
% / | ||
% / | ||
% -----------' | ||
% | ||
% Consider using scaledNlfun with logexp1 instead (it's smoother) | ||
|
||
f = zeros(size(x)); | ||
f(x > 0) = x(x > 0); | ||
f(x <= 0) = eps; | ||
|
||
if nargout > 1 | ||
df = zeros(size(x)); | ||
df(x > 0) = 1; | ||
df(x == 0) = 0.2; % subgradient | ||
end | ||
|
||
if nargout > 2 | ||
ddf = zeros(size(x)); | ||
end |