For those of you working with Arraymancer,
Can someone show me an example of saving and loading a model? I've been banging my head against a wall all day trying to figure this out lol. I could careless if it's csv, numpy file, etc. I just have a DQN model that's training properly and I want to test it with my game visually
Because someone, someday may have this problem:
In order to save/load weights and biases of your model, you'll first need to define these manually-
working test example:
type
LinearLayer = object
weight: Variable[Tensor[float32]]
bias: Variable[Tensor[float32]]
ExampleNetwork = object
hidden: LinearLayer
output: LinearLayer
template weightInit(shape: varargs[int], init_kind: untyped): Variable =
ctx.variable(
init_kind(shape, float32),
requires_grad = true)
proc newExampleNetwork(ctx: Context[Tensor[float32]]): ExampleNetwork =
result.hidden.weight = weightInit(HIDDEN_D, INPUT_D, kaiming_normal)
result.hidden.bias = ctx.variable(zeros[float32](1, HIDDEN_D), requires_grad = true)
result.output.weight = weightInit(OUTPUT_D, HIDDEN_D, yann_normal)
result.output.bias = ctx.variable(zeros[float32](1, OUTPUT_D), requires_grad = true)
proc forward(network: ExampleNetwork, x: Variable): Variable =
result = x.linear(
network.hidden.weight, network.hidden.bias).relu.linear(
network.output.weight, network.output.bias)
Then, you'll need to create your save/load procs. I'll save you the headache here as well- use numpy files. Long story short, forget about hdf5.. and the others aren't as efficient.
working test example:
proc save(network: ExampleNetwork) =
network.hidden.weight.value.write_npy("hiddenweight.npy")
network.hidden.bias.value.write_npy("hiddenbias.npy")
network.output.weight.value.write_npy("outputweight.npy")
network.output.bias.value.write_npy("outputbias.npy")
proc load(ctx: Context[Tensor[float32]]): ExampleNetwork =
result.hidden.weight = ctx.variable(read_npy[float32]("hiddenweight.npy"), requires_grad = true)
result.hidden.bias = ctx.variable(read_npy[float32]("hiddenbias.npy"), requires_grad = true)
result.output.weight = ctx.variable(read_npy[float32]("outputweight.npy"), requires_grad = true)
result.output.bias = ctx.variable(read_npy[float32]("outputbias.npy"), requires_grad = true)
At some point in the future I'll work on getting the network macro to integrate loading and saving models but for now, this POC/example should help push you in the right direction.