Help with my code layer_dense_features #140515
Unanswered
andymmm31
asked this question in
Programming Help
Replies: 1 comment
-
Thanks for posting in the GitHub Community, @andymmm31 ! We’ve moved your post to our Programming Help 🧑💻 category, which is more appropriate for this type of discussion. Please review our guidelines about the Programming Help category for more information. |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Select Topic Area
Question
Body
I am new to this and I am doing an exercise, but I am encountering an issue because layer_dense_features no longer exists. What alternatives are there, or how can I fix this? Please help me."
library("dplyr")
library("mlr")
library("plotly")
library("SPOT")
library("SPOTMisc")
library("farff")
library(tensorflow)
library(keras3)
library(keras)
library(tfdatasets)
library(recipes)
target <- "age"
task.type <- "classif"
nobs <- 1e4
nfactors <- "high"
nnumericals <- "high"
cardinality <- "high"
data.seed <- 1
cachedir <- "oml.cache"
prop <- 2 / 3
dfCensus <- getDataCensus(
task.type = task.type,
nobs = nobs,
nfactors = nfactors,
nnumericals = nnumericals,
cardinality = cardinality,
data.seed = data.seed,
cachedir = cachedir,
target = target
)
data <- getGenericTrainValTestData(dfGeneric = dfCensus, prop = prop)
batch_size <- as.integer(32)
specList <- genericDataPrep(data = data, batch_size = batch_size)
df_to_dataset <- function(data, shuffle = TRUE, batch_size = 32) {
dataset <- tensorflow::tf$data$Dataset$from_tensor_slices(data)
if (shuffle) {
dataset <- dataset$shuffle(buffer_size = nrow(data))
}
dataset <- dataset$batch(batch_size)
return(dataset)
}
na_count <- sapply(data$trainGeneric, function(x) sum(is.na(x)))
data$trainGeneric$wage_per_hour <- as.numeric(data$trainGeneric$wage_per_hour)
data$trainGeneric$capital_gains <- as.numeric(data$trainGeneric$capital_gains)
data$trainGeneric$capital_losses <- as.numeric(data$trainGeneric$capital_losses)
data$trainGeneric$divdends_from_stocks <- as.numeric(data$trainGeneric$divdends_from_stocks)
data$trainGeneric$num_persons_worked_for_employer <- as.numeric(data$trainGeneric$num_persons_worked_for_employer)
data$trainGeneric$weeks_worked_in_year <- as.numeric(data$trainGeneric$weeks_worked_in_year)
data$trainGeneric$target <- as.numeric(data$trainGeneric$target)
train_ds_generic <- df_to_dataset(data$trainGeneric, batch_size = batch_size)
val_ds_generic <- df_to_dataset(data$valGeneric, shuffle = FALSE, batch_size = batch_size)
train_batch <- train_ds_generic %>%
reticulate::as_iterator() %>%
reticulate::iter_next()
if (!is.null(reticulate::py_last_error())) {
print(reticulate::py_last_error())
}
specGeneric <- feature_spec(dataset = train_ds_generic, target ~ .)
minLevelSizeEmbedding <- 100
embeddingDim <- floor(log(minLevelSizeEmbedding))
df <- data$trainGeneric
df <- df[-which(names(df) == "target")]
embeddingVars <- names(df %>%
mutate_if(is.character, factor) %>%
select_if(~ is.factor(.) & nlevels(.) > minLevelSizeEmbedding))
noEmbeddingVars <-
names(df %>%
mutate_if(is.character, factor) %>%
select_if(~is.factor(.) & nlevels(.) <= minLevelSizeEmbedding))
specGeneric <- specGeneric %>%
step_numeric_column(all_numeric(),
normalizer_fn = scaler_standard()
) %>%
step_categorical_column_with_vocabulary_list(all_nominal()) %>%
step_indicator_column(matches(noEmbeddingVars)) %>%
step_embedding_column(matches(embeddingVars), dimension = embeddingDim)
specGeneric_prep <- fit(specGeneric)
specList <- genericDataPrep(data = data, batch_size = batch_size)
names(specList$specGeneric_prep$dense_features())
cfg <- getModelConf(model = "dl")
layer <- layer_dense_features(
feature_columns = dense_features(specList$specGeneric_prep)
)
iterator <- reticulate::as_iterator(specList$train_ds_generic)
next_batch <- reticulate::iter_next(iterator)
output <- layer(next_batch)
print(output)
Beta Was this translation helpful? Give feedback.
All reactions