我正在嘗試使用 kerastuneR 來實作此代碼,以便進行超引數調整。
library(keras)
library(tensorflow)
library(kerastuneR)
library(dplyr)
x_data <- matrix(data = runif(500,0,1),nrow = 50,ncol = 5)
y_data <- ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()
x_data2 <- matrix(data = runif(500,0,1),nrow = 50,ncol = 5)
y_data2 <- ifelse(runif(50,0,1) > 0.6, 1L,0L) %>% as.matrix()
build_model = function(hp) {
model = keras_model_sequential()
model %>% layer_dense(units=hp$Int('units',
min_value=32,
max_value=512,
step=32),
input_shape = ncol(x_data),
activation='relu') %>%
layer_dense(units=1, activation='sigmoid') %>%
compile(
optimizer= tf$keras$optimizers$Adam(
hp$Choice('learning_rate',
values=c(1e-2, 1e-3, 1e-4))),
loss='binary_crossentropy',
metrics='accuracy')
return(model)
}
tuner = RandomSearch(
build_model,
objective = 'val_accuracy',
max_trials = 5,
executions_per_trial = 3,
directory = 'my_dir',
project_name = 'helloworld')
tuner %>% search_summary()
# Fit
tuner %>% fit_tuner(x_data,y_data,
epochs=5,
validation_data = list(x_data2,y_data2))
所以這段代碼運行良好,期望最后一段代碼給出這個錯誤:
Error in py_call_impl(callable, dots$args, dots$keywords) :
ValueError: Objective value missing in metrics reported to the Oracle, expected: ['val_accuracy'], found: dict_keys(['loss', 'acc', 'val_loss', 'val_acc'])
那么任何人都可以幫助如何解決這個錯誤嗎?
uj5u.com熱心網友回復:
該錯誤為您提供了解決問題的關鍵。您需要將生成的鍵的名稱fit_tuner與提供給RandomSearch函式的鍵的名稱相匹配。嘗試在函式中用 'val_accuracy' 代替 'val_acc' RandomSearch。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/411596.html
標籤:
