UNPKG

@hoff97/tensor-js

Version:

PyTorch like deep learning inferrence library

40 lines 1.22 kB
export class ProductBack { constructor(input, product, sumDims, keepDims) { this.input = input; this.product = product; this.sumDims = sumDims; this.keepDims = keepDims; } backward(grad) { const inShape = this.input.value.getShape(); let mult = grad.multiply(this.product); if (!this.keepDims) { const newShape = []; let sumI = 0; for (let i = 0; i < inShape.length; i++) { if (sumI < this.sumDims.length && this.sumDims[sumI] === i) { newShape.push(1); sumI++; } else { newShape.push(inShape[i]); } } mult = mult.reshape(newShape, false); } const expanded = mult.expand(inShape); mult.delete(); const gradIn = expanded.divide(this.input.value); expanded.delete(); const needed = this.input.backward(gradIn); if (!needed) { gradIn.delete(); } } delete() { if (!this.input.isLeaf()) { this.input.delete(); } } } //# sourceMappingURL=productBack.js.map