📄 tansig.c
字号:
mclCopyArray(&n);
mclCopyArray(&b);
/*
* %TANSIG Hyperbolic tangent sigmoid transfer function.
* %
* % Syntax
* %
* % A = tansig(N)
* % info = tansig(code)
* %
* % Description
* %
* % TANSIG is a transfer function. Transfer functions
* % calculate a layer's output from its net input.
* %
* % TANSIG(N) takes one input,
* % N - SxQ matrix of net input (column) vectors.
* % and returns each element of N squashed between -1 and 1.
* %
* % TANSIG(CODE) returns useful information for each CODE string:
* % 'deriv' - Returns name of derivative function.
* % 'name' - Returns full name.
* % 'output' - Returns output range.
* % 'active' - Returns active input range.
* %
* % TANSIG is named after the hyperbolic tangent which has the
* % same shape. However, TANH may be more accurate and is
* % recommended for applications that require the hyperbolic tangent.
* %
* % Examples
* %
* % Here the code to create a plot of the TANSIG transfer function.
* %
* % n = -5:0.1:5;
* % a = tansig(n);
* % plot(n,a)
* %
* % Network Use
* %
* % You can create a standard network that uses TANSIG
* % by calling NEWFF or NEWCF.
* %
* % To change a network so a layer uses TANSIG set
* % NET.layers{i,j}.transferFcn to 'tansig'.
* %
* % In either case, call SIM to simulate the network with TANSIG.
* % See NEWFF or NEWCF for simulation examples.
* %
* % Algorithm
* %
* % TANSIG(N) calculates its output according to:
* %
* % n = 2/(1+exp(-2*n))-1
* %
* % This is mathematically equivalent to TANH(N). It differs
* % in that it runs faster than the MATLAB implementation of TANH,
* % but the results can have very small numerical differences. This
* % function is a good trade off for neural networks, where speed is
* % important and the exact shape of the transfer function is not.
* %
* % See also SIM, DTANSIG, LOGSIG.
*
* % Mark Beale, 1-31-92
* % Revised 12-15-93, MB
* % Revised 11-31-97, MB
* % Copyright 1992-2002 The MathWorks, Inc.
* % $Revision: 1.10 $ $Date: 2002/03/25 16:52:13 $
*
* if nargin < 1, error('Not enough arguments.'); end
*/
if (nargin_ < 1) {
mlfError(_mxarray0_, NULL);
}
/*
*
* % FUNCTION INFO
* if isstr(n)
*/
if (mlfTobool(mlfIsstr(mclVa(n, "n")))) {
/*
* switch (n)
*/
mxArray * v_ = mclInitialize(mclVa(n, "n"));
if (mclSwitchCompare(v_, _mxarray2_)) {
/*
* case 'deriv'
* a = 'dtansig';
*/
mlfAssign(&a, _mxarray4_);
/*
* case 'name'
*/
} else if (mclSwitchCompare(v_, _mxarray6_)) {
/*
* a = 'Tan Sigmoid';
*/
mlfAssign(&a, _mxarray8_);
/*
* case 'output'
*/
} else if (mclSwitchCompare(v_, _mxarray10_)) {
/*
* a = [-1 1];
*/
mlfAssign(&a, _mxarray12_);
/*
* case 'active'
*/
} else if (mclSwitchCompare(v_, _mxarray14_)) {
/*
* a = [-2 2];
*/
mlfAssign(&a, _mxarray16_);
/*
* case 'type'
*/
} else if (mclSwitchCompare(v_, _mxarray18_)) {
/*
* a = 1;
*/
mlfAssign(&a, _mxarray20_);
/*
*
* % **[ NNT2 Support ]**
* case 'delta'
*/
} else if (mclSwitchCompare(v_, _mxarray21_)) {
/*
* a = 'deltatan';
*/
mlfAssign(&a, _mxarray23_);
/*
* nntobsu('tansig','Use TANSIG(''deriv'') instead of TANSIG(''delta'').')
*/
mlfNntobsu(_mxarray25_, _mxarray27_, NULL);
/*
* case 'init'
*/
} else if (mclSwitchCompare(v_, _mxarray29_)) {
/*
* a = 'nwtan';
*/
mlfAssign(&a, _mxarray31_);
/*
* nntobsu('tansig','Use network propreties to obtain initialization info.')
*/
mlfNntobsu(_mxarray25_, _mxarray33_, NULL);
/*
*
* otherwise, error('Unrecognized code.')
*/
} else {
mlfError(_mxarray35_, NULL);
/*
* end
*/
}
mxDestroyArray(v_);
/*
* return
*/
goto return_;
/*
* end
*/
}
/*
*
* % CALCULATION
*
* % **[ NNT2 Support ]**
* if nargin == 2
*/
if (nargin_ == 2) {
/*
* nntobsu('tansig','Use TANSIG(NETSUM(Z,B)) instead of TANSIG(Z,B).')
*/
mlfNntobsu(_mxarray25_, _mxarray37_, NULL);
/*
* n=n+b(:,ones(1,size(n,2)));
*/
mlfAssign(
&n,
mclPlus(
mclVa(n, "n"),
mclArrayRef2(
mclVa(b, "b"),
mlfCreateColonIndex(),
mlfOnes(
_mxarray20_,
mlfSize(mclValueVarargout(), mclVa(n, "n"), _mxarray39_),
NULL))));
/*
* end
*/
}
/*
*
* a = 2 ./ (1 + exp(-2*n)) - 1;
*/
mlfAssign(
&a,
mclMinus(
mclRdivide(
_mxarray39_,
mclPlus(_mxarray20_, mlfExp(mclMtimes(_mxarray40_, mclVa(n, "n"))))),
_mxarray20_));
/*
* i = find(~finite(a));
*/
mlfAssign(&i, mlfFind(NULL, NULL, mclNot(mlfFinite(mclVv(a, "a")))));
/*
* a(i) = sign(n(i));
*/
mclArrayAssign1(
&a, mlfSign(mclArrayRef1(mclVa(n, "n"), mclVv(i, "i"))), mclVv(i, "i"));
return_:
mclValidateOutput(a, 1, nargout_, "a", "tansig");
mxDestroyArray(ans);
mxDestroyArray(i);
mxDestroyArray(b);
mxDestroyArray(n);
mclSetCurrentLocalFunctionTable(save_local_function_table_);
return a;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -