我正在按照“使用 R 进行深度学习,第二版”一书第 10 章中的示例,使用 R 包
keras3
。出现错误:W tensorflow/core/framework/op_kernel.cc:1841] OP_REQUIRES failed at reshape_op.h:65 : INVALID_ARGUMENT: 只有一个输入大小可以为 -1,不能同时为 0 和 1。
我是 R 深度学习框架的新手,我想知道如何修复这个错误?非常感谢!
本书在此链接中提供了数据集。
library(keras3)
library(dplyr)
# Preparing the Data
full_df <- readr::read_csv("jena_climate_2009_2016.csv")
full_df$`Date Time` %<>%
as.POSIXct(tz = "Etc/GMT+1", format = "%d.%m.%Y %H:%M:%S")
num_train_samples <- round(nrow(full_df) * 0.5)
num_val_samples <- round(nrow(full_df) * 0.25)
num_test_samples <- nrow(full_df) - num_train_samples - num_val_samples
train_df <- full_df[seq(num_train_samples), ]
val_df <- full_df[seq(from = nrow(train_df) + 1,
length.out = num_val_samples), ]
test_df <- full_df[seq(to = nrow(full_df),
length.out = num_test_samples), ]
## Normalize the data
input_data_colnames <- names(full_df) %>%
setdiff(c("Date Time"))
normalization_values <-
zip_lists(mean = lapply(train_df[input_data_colnames], mean),
sd = lapply(train_df[input_data_colnames], sd))
str(normalization_values)
normalize_input_data <- function(df) {
normalize <- function(x, center, scale)
(x - center) / scale
for(col_nm in input_data_colnames) {
col_nv <- normalization_values[[col_nm]]
df[[col_nm]] %<>% normalize(., col_nv$mean, col_nv$sd)
}
df
}
## Instantiating datasets for training, validation, and testing
sampling_rate <- 6
sequence_length <- 120
delay <- sampling_rate * (sequence_length + 24 - 1)
batch_size <- 256
df_to_inputs_and_targets <- function(df) {
inputs <- df[input_data_colnames] %>%
normalize_input_data() %>%
as.matrix()
targets <- as.array(df$`T (degC)`)
list(head(inputs, -delay), tail(targets, -delay))
}
make_dataset <- function(df) {
c(inputs, targets) %<-% df_to_inputs_and_targets(df)
timeseries_dataset_from_array(
inputs, targets,
sampling_rate = sampling_rate,
sequence_length = sequence_length,
shuffle = TRUE,
batch_size = batch_size
)
}
train_dataset <- make_dataset(train_df)
val_dataset <- make_dataset(val_df)
test_dataset <- make_dataset(test_df)
## A densely connected model
ncol_input_data <- length(input_data_colnames)
inputs <- layer_input(shape = c(sequence_length, ncol_input_data))
outputs <- inputs %>%
layer_flatten() %>%
layer_dense(16, activation = "relu") %>%
layer_dense(1)
model <- keras_model(inputs, outputs)
callbacks = list(
callback_model_checkpoint("jena_dense.keras",
save_best_only = TRUE)
)
model %>%
compile(optimizer = "rmsprop",
loss = "mse",
metrics = "mae")
history <- model %>%
fit(train_dataset,
epochs = 10,
validation_data = val_dataset,
callbacks = callbacks)
model <- load_model_tf("jena_dense.keras")
sprintf("Test MAE: %.2f", evaluate(model, test_dataset)["mae"])
这似乎是一个“已知问题”,在使用 keras2 的代码运行 keras3 时会发生。解决这个问题的简单方法是您应该使用 keras
而不是像书中原始代码中那样的
keras3
库library(keras)