Tanh

hyperbolic tangent

Members

Functions

backward
auto backward(Variable!(T, dim, HostStorage) gy)
forward
auto forward(Variable!(T, dim, HostStorage) x)

Manifest constants

mode
enum mode;
Undocumented in source.

Mixins

__anonymous
mixin FunctionCommon
Undocumented in source.

Variables

hy
Variable!(T, dim, HostStorage) hy;
Undocumented in source.

Mixed In Members

From mixin FunctionCommon

this(this)
this(this)
Undocumented in source.
DeviceRets
alias DeviceRets = Tuple!(Parameters!backward)
Undocumented in source.
DeviceArgs
alias DeviceArgs = Tuple!(Parameters!forward)
Undocumented in source.
__anonymous
mixin TypeChecker!(forward, backward)
Undocumented in source.
_mixin_dargs
DeviceArgs _mixin_dargs;
Undocumented in source.
HostRets
alias HostRets = Tuple!(Parameters!backward)
Undocumented in source.
HostArgs
alias HostArgs = Tuple!(Parameters!forward)
Undocumented in source.
__anonymous
mixin TypeChecker!(forward, backward)
Undocumented in source.
_mixin_hargs
HostArgs _mixin_hargs;
Undocumented in source.
applyForward
auto applyForward(Args args)

store grain.autograd.BackProp object in returned variables from forward function

applyBackward
void applyBackward(UntypedVariable[] ugradOutputs)

type-erased version of backward function used in grain.autograd.BackProp object

Examples

// test CPU
import grain.testing;
import std.math : tanh;
import numir;

auto func = new Tanh!(float, 1);
auto hx = [-1.0f, 1.0f, 0.0f].variable;
gradCheck(func, hx, [0.1f, 0.1f, 0.1f].variable);

auto hy = func.forward(hx);
assert(hy.data == [tanh(-1.0f), tanh(1.0f), tanh(0.0f)]);
auto hgy = [1.0f, 2.0f, 3.0f].variable;
auto hgx = func.backward(hgy);

// test CUDA
version (grain_cuda) {
    auto dfunc = new Tanh!(float, 1);
    auto dx = hx.to!DeviceStorage;
    auto dy = dfunc.forward(dx);
    assert(approxEqual(dy.to!HostStorage.sliced, hy.sliced));
    auto dgx = dfunc.backward(hgy.to!DeviceStorage);
    assert(approxEqual(dgx.to!HostStorage.sliced, hgx.sliced));
}

Meta