Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ability to clone neural nets instead of all the weights/biases tensors manually #568

Open
creikey opened this issue Aug 31, 2022 · 3 comments

Comments

@creikey
Copy link

creikey commented Aug 31, 2022

Right now have to manually clone all the weights, bug prone

@mratsim
Copy link
Owner

mratsim commented Sep 1, 2022

What code did you use for cloning?

@creikey
Copy link
Author

creikey commented Sep 1, 2022

@mratsim Like this:

    intoBrain.gru.w3s0 = ctx.variable(fromBrain.gru.w3s0.value.clone())
    intoBrain.gru.w3sN = ctx.variable(fromBrain.gru.w3sN.value.clone())
    intoBrain.gru.u3s = ctx.variable(fromBrain.gru.u3s.value.clone())
    intoBrain.gru.bW3s = ctx.variable(fromBrain.gru.bW3s.value.clone())
    intoBrain.gru.bU3s = ctx.variable(fromBrain.gru.bU3s.value.clone())

    intoBrain.fc1.weight = ctx.variable(fromBrain.fc1.weight.value.clone())
    intoBrain.fc1.bias = ctx.variable(fromBrain.fc1.bias.value.clone())
    intoBrain.fc2.weight = ctx.variable(fromBrain.fc2.weight.value.clone())
    intoBrain.fc2.bias = ctx.variable(fromBrain.fc2.bias.value.clone())

Where the brain is like this:

  CreatureBrain[T] = object
    gru: GRULayer[T]
    memory: Variable[Tensor[T]]
    fc1: Linear[T]
    fc2: Linear[T]

@mratsim
Copy link
Owner

mratsim commented Sep 7, 2022

As a workaround in the meantime, you can use fields iterator here: https://nim-lang.org/docs/iterators.html#fields.i%2CS%2CT

proc copyWeights[T](intoBrain: var T, fromBrain: T) =
  for dst, src in fields(intoBrain, fromBrain):
    when dst is Variable:
      dst = ctx.variable(src.value.clone())
    else:
      dst.copyWeights(src)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants