t-kalinowski / deep-learning-with-R-2nd-edition-code

Code from the book "Deep Learning with R, 2nd Edition"
https://blogs.rstudio.com/ai/posts/2022-05-31-deep-learning-with-r-2e/
54 stars 22 forks source link

Chapter 3 Boston Housing Prices #9

Closed jonbry closed 8 months ago

jonbry commented 8 months ago

I apologize if this isn’t the right place to post these questions but I had a few questions about the Boston housing example in Chapter 3. I was getting strange MAE results, so I copied the code directly from the book to make sure it was the same. I still am getting over 2X the MAE in the book and sometimes as high as 17. I realize there is going to be some randomness but this is higher than I expected. It there any issues with the following code:

library(keras)

dataset <- dataset_boston_housing() 
c(c(train_data, train_targets), c(test_data, test_targets)) %<-% dataset

mean <- apply(train_data, 2, mean) 
std <- apply(train_data, 2, sd) 
train_data <- scale(train_data, center = mean, scale = std) 
test_data <- scale(test_data, center = mean, scale = std)

build_model <- function(){ 
  model <- keras_model_sequential() %>% 
    layer_dense(units = 64, activation = "relu",
                input_shape = dim(train_data)[[2]]) %>%
    layer_dense(units = 64, activation = "relu") %>%
    layer_dense(units = 1)

  model %>% compile( optimizer = "rmsprop",
                     loss = "mse",
                     metrics = c("mae"))
}

k <- 4 
indices <- sample(1:nrow(train_data)) 
folds <- cut(indices, breaks = k, labels = FALSE) 

num_epochs <- 100 
all_scores <- c() 

for (i in 1:k){ 
  cat("processing fold #", i, "\n")

  val_indices <- which(folds == i, arr.ind = TRUE) 
  val_data <- train_data[val_indices,] 
  val_targets<- train_targets[val_indices] 

  partial_train_data <- train_data[-val_indices,] 
  partial_train_targets <- train_targets[-val_indices] 

  model <- build_model()
  model %>% 
    fit(partial_train_data, partial_train_targets,
        epochs = num_epochs, batch_size = 1, verbose = 0) 
  results <- model %>% evaluate(val_data, val_targets, verbose = 0) 
  all_scores <- c(all_scores, results["mae"])
}

I have some additional questions regarding the Keras package and I was wondering if I would be allowed to use this code as an example in other forms (Stack Overflow, Rstudio, Keras repository, etc). I just wanted to make sure it was ok since the code portion comes from the book directly.

t-kalinowski commented 8 months ago

Hi.

In the 2nd edition of the book, the example showing k-fold cross validation with the Boston Housing dataset is in chapter 4, not chapter 3. Also, the example was updated in the 2nd edition of the book. You can see the updated code here. Can you please double check you're looking at the 2nd edition of the book and not the 1st?

You're welcome to reuse the code examples as you see fit. If you're sharing publicly, attribution is appreciated.

If you're producing new content, note that you can use library(keras3) to get the latest version of the package, while we transition the R dependancies from keras 2 to keras 3. All the code examples in the book should still work with keras 3 (though if you encounter any errors, please open an issue).

jonbry commented 8 months ago

Thanks for getting back to me so quickly! You're right, I was looking at the wrong version of the book. I didn't realize the publisher gave me both versions of the book.