UNPKG

@ai-on-browser/data-analysis-models

Version:

Data analysis model package without any dependencies

40 lines (34 loc) 685 B
import Layer from './base.js' /** * Scaled tanh layer */ export default class ScaledTanhLayer extends Layer { /** * @param {object} config config * @param {number} [config.a] a * @param {number} [config.b] b */ constructor({ a = 1, b = 1, ...rest }) { super(rest) this._a = a this._b = b } calc(x) { this._o = x.copy() this._o.map(v => this._a * Math.tanh(this._b * v)) return this._o } grad(bo) { const bi = bo.copy() bi.broadcastOperate(this._o, (a, b) => a * (this._a * this._b - (this._b / this._a) * b ** 2)) return bi } toObject() { return { type: 'stanh', a: this._a, b: this._b, } } } ScaledTanhLayer.registLayer('stanh')