Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion lib/nn.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ class ActivationFunction {
this.func = func;
this.dfunc = dfunc;
}

static deserialize(data) {
if (typeof data == 'string') {
data = JSON.parse(data);
}
return new ActivationFunction(eval(data.func), eval(data.dfunc));
}
}

let sigmoid = new ActivationFunction(
Expand Down Expand Up @@ -139,7 +146,7 @@ class NeuralNetwork {
}

serialize() {
return JSON.stringify(this);
return JSON.stringify(this, (k, v) => typeof v == 'function' ? v.toString() : v);
}

static deserialize(data) {
Expand All @@ -152,6 +159,7 @@ class NeuralNetwork {
nn.bias_h = Matrix.deserialize(data.bias_h);
nn.bias_o = Matrix.deserialize(data.bias_o);
nn.learning_rate = data.learning_rate;
nn.activation_function = ActivationFunction.deserialize(data.activation_function);
return nn;
}

Expand Down