@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
39 lines • 1.79 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { OnnxNode } from '../node';
export class CeluNode extends OnnxNode {
constructor(attributes, inputs, outputs, constants, onnxVersion, mode) {
super(attributes, inputs, outputs, constants, onnxVersion, mode);
this.alpha = this.getAttributeFloat('alpha') || 1.0;
}
forward(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const tensor = inputs[0];
const below = tensor.clip(undefined, 0);
const above = tensor.clip(0, undefined);
// max(0,x) + min(0,alpha*(exp(x/alpha)-1))
const belowDiv = below.multiplyScalar(1 / this.alpha);
below.delete();
const belowExp = belowDiv.exp();
belowDiv.delete();
const belowRes = belowExp.addMultiplyScalar(this.alpha, -this.alpha);
belowExp.delete();
const result = belowRes.add(above);
belowRes.delete();
above.delete();
return [result];
});
}
delete() { }
getType() {
return 'Celu';
}
}
//# sourceMappingURL=celu.js.map