shiffman/Neural-Network-p5

Reload a pre-trained model

Opened this issue · 3 comments

Right now there is a button to download a JSON file with all of the network weights. It would be nice to add a feature to reload weights.

storing and reloading network weights is something I wish I had done ... especially for long training sessions with a laptop I need to turn off sometimes

This feature should be built into nn.js as a save() and load() function.

Saving is not that hard, JSON.stringify(nn) does the trick. Exporting, however, is painful.

function saveNN() {
	localStorage.setItem("nn", JSON.stringify(nn));
}

To load, I've tried so many things, but Matrices are hard to load.

function loadNN() {
	if (localStorage.getItem("nn") === null) {
		nn = new NeuralNetwork(100, 100, 100, 1/100, "tanh");
		console.log("Loaded a new neural network.");
	} else {
		nn = JSON.parse(localStorage.getItem("nn"));
		nn = Object.assign(new NeuralNetwork, nn);

		// This doesn't work. How do we do that?
		nn.wih = Object.assign(new Matrix, nn.wih);
		nn.woh = Object.assign(new Matrix, nn.woh);

		console.log("Loaded an existing neural network.");
	}
}

So, if you have an idea about how to do this, I'd be grateful :)