mishMatrix function
Implementation
Tensor<Matrix> mishMatrix(Tensor<Matrix> m) {
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];
double sp = log(1.0 + exp(x));
double e2sp = exp(2.0 * sp);
double t = e2sp.isInfinite ? 1.0 : (e2sp - 1.0) / (e2sp + 1.0);
row.add(x * t);
}
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 sp = log(1.0 + exp(x));
double e2sp = exp(2.0 * sp);
double t = e2sp.isInfinite ? 1.0 : (e2sp - 1.0) / (e2sp + 1.0);
double s = 1.0 / (1.0 + exp(-x));
double grad = t + x * s * (1.0 - t * t);
m.grad[i] = m.grad[i] + out.grad[i] * grad;
}
},
opName: 'mish_matrix',
cost: numRows * numCols * 3,
);
return out;
}