vectorTanh function
Implementation
Tensor<Vector> vectorTanh(Tensor<Vector> v) {
double _tanh(double x) {
var e2x = exp(2 * x);
if (e2x.isInfinite) return 1.0;
return (e2x - 1) / (e2x + 1);
}
int N = v.value.length;
Vector outValue = [];
for (int i = 0; i < N; i++) {
outValue.add(_tanh(v.value[i]));
}
Tensor<Vector> out = Tensor<Vector>(outValue);
out.creator = Node(
[v],
() {
for (int i = 0; i < v.value.length; i++) {
v.grad[i] += out.grad[i] * (1 - pow(out.value[i], 2));
}
},
opName: 'tanh_vector', // <-- Renamed for clarity
cost: N,
);
return out;
}