簡體   English   中英

支持 R 中的 Callback_Early_Stopping

[英]Support Callback_Early_Stopping in R

我正在使用 Keras 和 Tensorflow 的 R 中的神經網絡項目。 我真的很想利用提前停止 function,主要是為了獲得最佳權重,但我正在努力繞過錯誤:“EarlyStopping”和“float”實例之間不支持“TypeError:'>'。 " 這里回答了一個類似的問題: TypeError: '>' not supported between instances of 'NoneType' and 'float' ,但解決方案均采用 python 編碼。 在這里使用 R 命令的任何幫助都會很棒!

這是我的代碼:

# API model
model <- local({
  input = layer_input(shape = c(44),  name = 'main_input')
  
  layer1 = input %>%
    layer_dense(units = 22, activation = "relu")
  
  layer2 = input %>%
    layer_dense(units = 22, activation = "relu")
  
  output = layer_concatenate(c(layer1, layer2)) %>%
    layer_dense(units = 10, activation = "relu") %>%
    layer_dense(units = 2, activation = "sigmoid")
  
  keras_model(inputs = input, outputs = output)
})

model %>%
  compile(loss = "binary_crossentropy",
          optimizer = 'adam',
          metrics = "accuracy")

history = model %>%
  fit(train,
      trainLabels,
      epoch = 25,
      batch_size = 32,
      validation_split = 0.2)
     callback_early_stopping(monitor = "val_acc", patience = 5, restore_best_weights = TRUE))

#Error in py_compare_impl(a, b, op) : 
  TypeError: '>' not supported between instances of 'EarlyStopping' and 'float'

我的數據已轉換為數字結構並格式化為矩陣以運行 Keras/Tensorflow 命令。 但是,我知道我的 model 中的變量是 float32:

str(train)
 num [1:2594, 1:44] 0.0202 -1.9071 0.0202 0.0202 -1.9071 ...

tail(model$variables)

...

[[5]]
<tf.Variable 'dense_21/kernel:0' shape=(10, 2) dtype=float32, numpy=
array([[-0.79153246,  0.20632687],
       [-0.21896149,  0.02037789],
       [-0.04512246,  0.29801697],
       [-0.46573785,  0.3391741 ],
       [-0.71519893,  0.8290489 ],
       [ 0.82080096, -0.13467254],
       [-0.60803676, -0.5283991 ],
       [ 0.8202912 , -0.89913267],
       [ 0.6229674 , -0.9428901 ],
       [ 1.1776545 , -0.8499227 ]], dtype=float32)>

[[6]]
<tf.Variable 'dense_21/bias:0' shape=(2,) dtype=float32, numpy=array([-0.09607901,  0.09400514], dtype=float32)>

我還嘗試使用“int32”而不是 float32 構建 model,但似乎沒有浮動我無法構建任何密集層。 關於我如何前進的任何想法/想法?

謝謝!

不知道您是否仍需要解決此問題的幫助,但您需要在列表中結束您的提前停止呼叫,如下所示。

callbacks = list(callback_early_stopping(monitor = "val_acc", patience = 5, restore_best_weights = TRUE))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM