recurrent-js-gpu
Version:
GPU-accelerated Deep Recurrent Neural Networks and LSTMs in Typescript. Ported, object-oriented and refactored version of Andrej Karpathy's recurrent-js (https://github.com/karpathy/recurrentjs)
62 lines • 2.16 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Mat_1 = require("./Mat");
const RandMat_1 = require("./RandMat");
class Net {
constructor(opt) {
this.W1 = null;
this.b1 = null;
this.W2 = null;
this.b2 = null;
if (Net.has(opt, ['W1', 'b1', 'W2', 'b2'])) {
this.W1 = Mat_1.Mat.fromJSON(opt['W1']);
this.b1 = Mat_1.Mat.fromJSON(opt['b1']);
this.W2 = Mat_1.Mat.fromJSON(opt['W2']);
this.b2 = Mat_1.Mat.fromJSON(opt['b2']);
}
else if (Net.has(opt, ['inputSize', 'hiddenSize', 'outputSize'])) {
const mu = opt['mu'] ? opt['mu'] : 0;
const std = opt['std'] ? opt['std'] : 0.01;
this.W1 = new RandMat_1.RandMat(opt['hiddenSize'], opt['inputSize'], mu, std);
this.b1 = new Mat_1.Mat(opt['hiddenSize'], 1);
this.W2 = new RandMat_1.RandMat(opt['outputSize'], opt['hiddenSize'], mu, std);
this.b2 = new Mat_1.Mat(opt['outputSize'], 1);
}
}
update(alpha) {
this.W1.update(alpha);
this.b1.update(alpha);
this.W2.update(alpha);
this.b2.update(alpha);
}
static toJSON(net) {
const json = {};
json['W1'] = Mat_1.Mat.toJSON(net.W1);
json['b1'] = Mat_1.Mat.toJSON(net.b1);
json['W2'] = Mat_1.Mat.toJSON(net.W2);
json['b2'] = Mat_1.Mat.toJSON(net.b2);
return json;
}
forward(observations, graph) {
const weightedStates = graph.mul(this.W1, observations);
const a1mat = graph.add(weightedStates, this.b1);
const h1mat = graph.tanh(a1mat);
const weightedActivations = graph.mul(this.W2, h1mat);
const a2Mat = graph.add(weightedActivations, this.b2);
return a2Mat;
}
static fromJSON(json) {
return new Net(json);
}
static has(obj, keys) {
for (const key of keys) {
if (Object.hasOwnProperty.call(obj, key)) {
continue;
}
return false;
}
return true;
}
}
exports.Net = Net;
//# sourceMappingURL=Net.js.map