If you need to move a model to GPU via $cuda(), please do so before constructing optimizers for it. Parameters of a model after $cuda()
will be different objects from those before the call. In general, you should make sure that the objects pointed to by model parameters subject to optimization remain the same over the whole lifecycle of optimizer creation and usage.
Examples
if(torch_is_installed()){## Not run:optimizer <- optim_adam(model$parameters(), lr =0.1)optimizer$zero_grad()loss_fn(model(input), target)$backward()optimizer$step()## End(Not run)}