@hoff97/tensor-js
Version:
PyTorch like deep learning inferrence library
86 lines • 2.23 kB
JavaScript
export class TanBack {
constructor(input) {
this.input = input;
}
backward(grad) {
const cos = this.input.value.cos();
const cos2 = cos.multiply(cos);
cos.delete();
const gradTan = grad.divide(cos2);
cos2.delete();
const needed = this.input.backward(gradTan);
if (!needed) {
gradTan.delete();
}
}
delete() {
if (!this.input.isLeaf()) {
this.input.delete();
}
}
}
export class ATanBack {
constructor(input) {
this.input = input;
}
backward(grad) {
const squared = this.input.value.multiply(this.input.value);
const onePlus = squared.addMultiplyScalar(1, 1);
squared.delete();
const gradATan = grad.divide(onePlus);
onePlus.delete();
const needed = this.input.backward(gradATan);
if (!needed) {
gradATan.delete();
}
}
delete() {
if (!this.input.isLeaf()) {
this.input.delete();
}
}
}
export class TanHBack {
constructor(input, tanH) {
this.input = input;
this.tanH = tanH;
}
backward(grad) {
const squared = this.tanH.multiply(this.tanH);
const onePlus = squared.addMultiplyScalar(-1, 1);
squared.delete();
const gradTanH = grad.multiply(onePlus);
onePlus.delete();
const needed = this.input.backward(gradTanH);
if (!needed) {
gradTanH.delete();
}
}
delete() {
if (!this.input.isLeaf()) {
this.input.delete();
}
}
}
export class ATanHBack {
constructor(input) {
this.input = input;
}
backward(grad) {
const squared = this.input.value.multiply(this.input.value);
const onePlus = squared.addMultiplyScalar(-1, 1);
squared.delete();
const gradATanH = grad.divide(onePlus);
onePlus.delete();
const needed = this.input.backward(gradATanH);
if (!needed) {
gradATanH.delete();
}
}
delete() {
if (!this.input.isLeaf()) {
this.input.delete();
}
}
}
//# sourceMappingURL=tanBack.js.map