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

reach out for help #3

Closed
jlxAtNovozymes opened this issue May 8, 2023 · 3 comments
Closed

reach out for help #3

jlxAtNovozymes opened this issue May 8, 2023 · 3 comments

Comments

@jlxAtNovozymes
Copy link

Hi, Keydana. first of all, thanks a lot for wrting such an amazing book for deep learning. It's my first book to deep learning and I still read it. but when I went to luz chapter, I had a question in my mind: how to use L-BFGS optimizer with luz, could you give me a simple example about it, thanks a lot

@skeydan
Copy link
Owner

skeydan commented May 11, 2023

Hi, happy to hear you enjoy it!

That's a good question! Basically the way to do it is write a custom loop, similar to the examples given here: https://mlverse.github.io/luz/articles/custom-loop.html:

library(luz)
library(torch)

net <- nn_module(
  "Net",
  initialize = function(num_hidden) {
    self$fc1 <- nn_linear(1, num_hidden)
    self$fc2 <- nn_linear(num_hidden, 1)
  },
  forward = function(x) {
    x <- self$fc1(x)
    x <- nnf_relu(x)
    x <- self$fc2(x)
    x
  },
  step = function() {
    if (ctx$training) {
      closure <- function() {
        pred <- ctx$model(ctx$input)
        loss <- ctx$loss_fn(pred, ctx$target)
        loss$backward()
        loss
      }
      ctx$loss <- ctx$opt$step(closure)
    } else {
      pred <- ctx$model(ctx$input)
      ctx$loss <- ctx$loss_fn(pred, ctx$target)
    }
  }
)

train_data <- list(torch_arange(1, 100)$unsqueeze(2), 2 * torch_arange(1, 100)$unsqueeze(2))

fitted <- net %>% 
  setup(
    loss = nn_mse_loss(),
    optimizer = optim_lbfgs
  ) %>% 
  set_hparams(num_hidden = 16) %>%
  set_opt_hparams(line_search_fn = "strong_wolfe") %>%
  fit(train_data, epochs = 3, valid_data = train_data)

@jlxAtNovozymes
Copy link
Author

Hi, Keydana. A really big thanks. BTW you wrote a really awesome book, I'm new to deep learning, I think your books make me easy to understand logics behind the scene. Really really fantastic work!

@skeydan
Copy link
Owner

skeydan commented May 11, 2023

Thank you!!

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