UNPKG

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

Version:

Data analysis model package without any dependencies

39 lines (33 loc) 633 B
import Layer from './base.js' /** * Hard tanh layer */ export default class HardTanhLayer extends Layer { /** * @param {object} config object * @param {number} [config.v] v */ constructor({ v = 1, ...rest }) { super(rest) this._v = v } calc(x) { this._i = x const o = x.copy() o.map(v => (v < -this._v ? -this._v : this._v < v ? this._v : v)) return o } grad(bo) { this._bo = bo const bi = bo.copy() bi.broadcastOperate(this._i, (a, b) => a * (b < -this._v || this._v < b ? 0 : 1)) return bi } toObject() { return { type: 'hard_tanh', v: this._v, } } } HardTanhLayer.registLayer()