lysergic
Version:
Synaptic's neural network compiler
201 lines • 7.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Topology = require("./Topology");
exports.Topology = Topology;
const AST = require("./AST");
exports.AST = AST;
const Heap = require("./Heap");
exports.Heap = Heap;
const nodes = require("./ast/nodes");
exports.nodes = nodes;
const Activations = require("./ast/activations");
exports.Activations = Activations;
var LysergicStatus;
(function (LysergicStatus) {
LysergicStatus[LysergicStatus["UNLOCKED"] = 0] = "UNLOCKED";
LysergicStatus[LysergicStatus["LOCKED"] = 1] = "LOCKED";
})(LysergicStatus = exports.LysergicStatus || (exports.LysergicStatus = {}));
var StatusTypes;
(function (StatusTypes) {
StatusTypes[StatusTypes["IDLE"] = 0] = "IDLE";
StatusTypes[StatusTypes["INIT"] = 1] = "INIT";
StatusTypes[StatusTypes["REVERSE_INIT"] = 2] = "REVERSE_INIT";
StatusTypes[StatusTypes["ACTIVATING"] = 3] = "ACTIVATING";
StatusTypes[StatusTypes["PROPAGATING"] = 4] = "PROPAGATING";
StatusTypes[StatusTypes["TRAINING"] = 5] = "TRAINING";
StatusTypes[StatusTypes["BUILDING"] = 6] = "BUILDING";
})(StatusTypes = exports.StatusTypes || (exports.StatusTypes = {}));
class Lysergic {
constructor(options = {}) {
this.options = options;
this.engineStatus = StatusTypes.IDLE;
this.topology = null;
this.ast = null;
this.heap = null;
this.status = LysergicStatus.UNLOCKED;
this.heap = options.heap || new Heap.Heap();
const { learningRate = 0.1, momentum = 0 } = options;
this.learningRate = learningRate;
this.momentum = momentum;
this.topology = new Topology.Topology({ heap: this.heap, bias: options.bias });
this.ast = new AST.AST({ topology: this.topology });
}
get learningRate() {
return this.heap.getVariable(`learningRate`).initialValue;
}
set learningRate(val) {
let lr = +val;
if (isNaN(lr) || lr <= 0) {
throw new Error('learningRate must be a positive number');
}
this.heap.setVariable(`learningRate`, lr);
}
get momentum() {
return this.heap.getVariable(`momentum`).initialValue;
}
set momentum(val) {
let lr = +val;
if (isNaN(lr) || lr < 0) {
throw new Error('momentum must be a positive number');
}
this.heap.setVariable(`momentum`, lr);
}
addUnit(options) {
if (this.status === LysergicStatus.LOCKED)
throw new Error('The network is locked');
options = Object.assign({ bias: this.options.bias }, options);
return this.topology.addUnit(options);
}
addLayer(size, options) {
if (this.status === LysergicStatus.LOCKED)
throw new Error('The network is locked');
options = Object.assign({ bias: this.options.bias }, options);
return this.topology.addLayer(size, options);
}
addConnection(from, to, weight) {
if (this.status === LysergicStatus.LOCKED)
throw new Error('The network is locked');
this.topology.addConnection(from, to, weight);
}
addGate(from, to, gater) {
if (this.status === LysergicStatus.LOCKED)
throw new Error('The network is locked');
this.topology.addGate(from, to, gater);
}
async build() {
if (this.status === LysergicStatus.UNLOCKED) {
this.topology.normalize();
this.ast.build();
await this.heap.build();
this.status = LysergicStatus.LOCKED;
}
}
getAST() {
if (this.status == LysergicStatus.UNLOCKED) {
throw new Error('You need to build the network first');
}
return this.ast.getDocument();
}
async getBuffer() {
if (this.status === LysergicStatus.UNLOCKED) {
throw new Error('You need to build the network first');
}
return this.heap.buffer;
}
async getMemory() {
if (this.status === LysergicStatus.UNLOCKED) {
throw new Error('You need to build the network first');
}
return this.heap.memory;
}
async setInputs(inputs) {
if (this.status === LysergicStatus.UNLOCKED) {
throw new Error('You need to build the network first');
}
const memory = await this.getMemory();
for (let i = 0; i < inputs.length; i++) {
memory[this.ast.inputs[i].id] = inputs[i];
}
}
async getOutputs() {
if (this.status === LysergicStatus.UNLOCKED) {
throw new Error('You need to build the network first');
}
const memory = await this.getMemory();
const outputs = new Array(this.ast.outputs.length);
for (let i = 0; i < this.ast.outputs.length; i++) {
outputs[i] = memory[this.ast.outputs[i].id];
}
return outputs;
}
async setTargets(targets) {
if (this.status === LysergicStatus.UNLOCKED) {
throw new Error('You need to build the network first');
}
const memory = await this.getMemory();
for (let i = 0; i < this.ast.targets.length; i++) {
memory[this.ast.targets[i].id] = targets[i];
}
}
toJSON(asString = false) {
let variables = {};
this.heap.sortVariables();
this.heap.getVariables().forEach($ => {
variables[$.key] = $.initialValue;
});
const stringified = JSON.stringify({
learningRate: this.learningRate,
momentum: this.momentum,
variables,
biasUnit: this.topology.biasUnit,
inputsOf: this.topology.inputsOf,
projectedBy: this.topology.projectedBy,
gatersOf: this.topology.gatersOf,
gatedBy: this.topology.gatedBy,
inputsOfGatedBy: this.topology.inputsOfGatedBy,
projectionSet: this.topology.projectionSet,
gateSet: this.topology.gateSet,
inputSet: this.topology.inputSet,
connections: this.topology.connections,
gates: this.topology.gates,
units: this.topology.units,
layers: this.topology.layers,
activationFunction: this.topology.activationFunction,
unitParameters: this.topology.unitParameters
});
return asString ? stringified : JSON.parse(stringified);
}
static fromJSON(json) {
const data = typeof json === 'string' ? JSON.parse(json) : json;
const variables = data.variables;
const heap = new Heap.Heap({});
Object.keys(variables).map($ => {
heap.setVariable($, variables[$]);
});
const compiler = new Lysergic({ heap });
compiler.learningRate = data.learningRate;
compiler.momentum = data.momentum;
compiler.topology.biasUnit = data.biasUnit;
compiler.topology.inputsOf = data.inputsOf;
compiler.topology.projectedBy = data.projectedBy;
compiler.topology.gatersOf = data.gatersOf;
compiler.topology.gatedBy = data.gatedBy;
compiler.topology.inputsOfGatedBy = data.inputsOfGatedBy;
compiler.topology.projectionSet = data.projectionSet;
compiler.topology.gateSet = data.gateSet;
compiler.topology.inputSet = data.inputSet;
compiler.topology.connections = data.connections;
compiler.topology.gates = data.gates;
compiler.topology.layers = data.layers;
compiler.topology.units = data.units;
compiler.topology.activationFunction = data.activationFunction;
compiler.topology.unitParameters = data.unitParameters;
return compiler;
}
clone() {
return Lysergic.fromJSON(this.toJSON());
}
}
exports.default = Lysergic;
exports.Lysergic = Lysergic;
//# sourceMappingURL=index.js.map