eluMatrix function
Implementation
Tensor<Matrix> eluMatrix(Tensor<Matrix> m, double alpha) {
int numRows = m.shape[0];
int numCols = m.shape[1];
Matrix mMat = m.value;
Matrix outValue = [];
for (int i = 0; i < numRows; i = i + 1) {
Vector row = [];
for (int j = 0; j < numCols; j = j + 1) {
double x = mMat[i][j];
if (x > 0.0) {
row.add(x);
} else {
row.add(alpha * (exp(x) - 1.0));
}
}
outValue.add(row);
}
Tensor<Matrix> out = Tensor<Matrix>(outValue);
out.creator = Node(
[m],
() {
int length = m.data.length;
for (int i = 0; i < length; i = i + 1) {
double x = m.data[i];
double grad = x > 0.0 ? 1.0 : out.data[i] + alpha;
m.grad[i] = m.grad[i] + out.grad[i] * grad;
}
},
opName: 'elu_matrix',
cost: numRows * numCols,
);
return out;
}