mlverse / luz

Higher Level API for torch

Home Page:https://mlverse.github.io/luz/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error if module has default initialization parameters

skeydan opened this issue · comments

This

net <- nn_module(
  initialize = function(d_in = d_in, d_hidden = d_hidden, d_out = d_out) {
    self$linear1 <- nn_linear(d_in, d_hidden)
    self$relu <- nn_relu()
    self$linear2 <- nn_linear(d_hidden, d_out)                   
  },
  forward = function(x) {
    x %>%
      self$linear1() %>%
      self$relu() %>%
      self$linear2()
  }
)

fitted <- net %>%
  setup(loss = nn_mse_loss(), optimizer = optim_adam) %>%
  #set_hparams(d_in = d_in, d_hidden = d_hidden, d_out = d_out) %>%
  fit(dl, epochs = 10, verbose = TRUE)

throws

  promise already under evaluation: recursive default argument reference or earlier problems? 
10.
initialize(...) 
9.
Module$new(in_features = in_features, out_features = out_features, 
    bias = bias) at nn.R#433
8.
nn_linear(d_in, d_hidden) 
7.
initialize(...) 
6.
Module$new(d_in = d_in, d_hidden = d_hidden, d_out = d_out) at nn.R#433
5.
(structure(function (d_in = d_in, d_hidden = d_hidden, d_out = d_out) 
{
    instance <- Module$new(d_in = d_in, d_hidden = d_hidden, 
        d_out = d_out) ... 
4.
do.call(module, get_hparams(module) %||% list()) at module.R#154
3.
fit.luz_module_generator(., dl, epochs = 10, verbose = TRUE) 
2.
fit(., dl, epochs = 10, verbose = TRUE) 
1.
net %>% setup(loss = nn_mse_loss(), optimizer = optim_adam) %>% 
    fit(dl, epochs = 10, verbose = TRUE) 

Probably we still want to enable the 2-verb-workflow setup -> fit, even though set_hparams could be used here to avoid the problem?
If not, maybe a more informative error message?

Shouldn't d_in , d_hidden and d_out be specific numbers? Where the defaults are coming from?
This is an R limitation actually, you can't have defaults that are obtained from the environment:

x <- 1
y <- 2

f <- function(x = x, y = y) {
  list(x, y)
}

f()
#> Error in f(): promise already under evaluation: recursive default argument reference or earlier problems?

Created on 2021-06-07 by the reprex package (v2.0.0)

oh, thanks, I didn't know that!