koalaverse / homlr

Supplementary material for Hands-On Machine Learning with R, an applied book covering the fundamentals of machine learning with R.
https://koalaverse.github.io/homlr
Creative Commons Attribution Share Alike 4.0 International
229 stars 88 forks source link

not found "inputs_currupted_gaussian" in the book "Hands-on machine learning with R" in page 391 #56

Closed panjunchang closed 2 years ago

panjunchang commented 2 years ago

when I experiment with the code examples provided in the book of page 391,

Train a denoise autoencoder

denoise_ae <- h2o.deeplearning( x = seq_along(features), training_frame = inputs_currupted_gaussian, validation_frame = features, autoencoder = TRUE, hidden = 100, activation = 'Tanh', sparse = TRUE )

the programe feedback with "not found ' nputs_currupted_gaussian' object.

bradleyboehmke commented 2 years ago

@panjunchang , you're correct, we did not show how the inputs_currupted_gaussian data frame was created. Below provides the code:

set.seed(123)
single_sample_index <- sample(seq_len(nrow(features)), 1)

# original feature
original <- features %>%
  as.data.frame() %>%
  .[single_sample_index, ]

# on-of corruption
corrupt_with_ones <- function(x) {
  n_to_sample <- floor(length(x) * .3)
  elements_to_corrupt <- sample(seq_along(x), n_to_sample, replace = FALSE)
  x[elements_to_corrupt] <- 0
  return(x)
}

inputs_currupted_ones <- features %>%
  as.data.frame() %>%
  purrr::map_df(corrupt_with_ones) %>%
  .[single_sample_index, ]

# Gaussian corruption
avg <- mean(as.matrix(features))
sd <- sd(as.matrix(features))
corrupt_with_gaussian <- function(x, mean, sd) {
  n_to_sample <- floor(length(x) * .3)
  elements_to_corrupt <- sample(seq_along(x), n_to_sample, replace = FALSE)
  random_norm_values <- rnorm(n_to_sample, mean, sd) %>% round()
  for (i in seq_along(random_norm_values)) {
    if (random_norm_values[i] < 0) random_norm_values[i] <- 0
    if (random_norm_values[i] > 255) random_norm_values[i] <- 255
  }

  x[elements_to_corrupt] <- random_norm_values
  return(x)
}

inputs_currupted_gaussian <- features %>%
  as.data.frame() %>%
  purrr::map_df(~ corrupt_with_gaussian(.x, avg, sd)) %>%
  .[single_sample_index, ]
panjunchang commented 2 years ago

thanks very much