如何找到訓練 keras 模型的時代數?
我使用callback_early_stopping()提前停止訓練以避免過度擬合。
我一直在使用callback_csv_logger()來記錄訓練表現。但有時,我訓練了 100 個 keras 模型,僅僅為了知道每個模型的 epoch 數而記錄整個訓練是沒有意義的。
library(keras)
library(kerasR)
library(tidyverse)
# Data
x = matrix(data = runif(30000), nrow = 10000, ncol = 3)
y = ifelse(rowSums(x) > 1.5 runif(10000), 1, 0)
y = to_categorical(y)
# keras model
model <- keras_model_sequential() %>%
layer_dense(units = 50, activation = "relu", input_shape = ncol(x)) %>%
layer_dense(units = ncol(y), activation = "softmax")
model %>%
compile(loss = "categorical_crossentropy",
optimizer = optimizer_rmsprop(),
metrics = "accuracy")
model %>%
fit(x, y,
epochs = 1000,
batch_size = 128,
validation_split = 0.2,
callbacks = callback_early_stopping(monitor = "val_loss", patience = 5),
verbose = 1)
uj5u.com熱心網友回復:
要列印時代數(無論您想要什么),您都可以使用回呼。下面是一個例子:
class print_log_Callback(Callback):
def __init__(self, logpath, steps):
self.logpath = logpath
self.losslst = np.zeros(steps)
def on_train_batch_end(self, batch, logs=None):
self.losslst[batch] = logs["loss"]
with open(logpath, 'a') as writefile:
with redirect_stdout(writefile):
print("For batch {}, loss is {:7.2f}.".format(batch, logs["loss"]))
writefile.write("\n")
def on_test_batch_end(self, batch, logs=None):
with open(logpath, 'a') as writefile:
with redirect_stdout(writefile):
print("For batch {}, val_loss is {:7.2f}.".format(batch, logs["loss"]))
writefile.write("\n")
def on_epoch_end(self, epoch, logs=None):
with open(logpath, 'a') as writefile:
with redirect_stdout(writefile):
print("The val_loss for epoch {} is {:7.2f}.".format(epoch, logs['val_loss']))
writefile.write("\n")
print("The mean train loss is: ", np.mean(self.losslst))
writefile.write("\n")
writefile.write("\n")
self.losslst = np.zeros(steps)
你這樣稱呼它:
print_log_Callback(logpath=logpath, steps=int(steps))
其中 logpath 是您撰寫代碼的文本檔案的路徑,steps 是步驟數。
此回呼基本上將網路的整個歷史記錄列印在文本檔案上。
每批和每個時期結束后的損失。
如果您只需要時代,您可以只使用該方法on_epoch_end并洗掉其他所有內容。
如果你想在每個 epoch 之后列印損失,你可以使用這個修改后的版本:
class print_log_Callback(Callback):
def __init__(self, logpath, steps):
self.logpath = logpath
self.losslst = np.zeros(steps)
def on_train_batch_end(self, batch, logs=None):
self.losslst[batch] = logs["loss"]
def on_epoch_end(self, epoch, logs=None):
with open(logpath, 'a') as writefile:
with redirect_stdout(writefile):
print("The val_loss for epoch {} is {:7.2f}.".format(epoch, logs['val_loss']))
writefile.write("\n")
print("The mean train loss is: ", np.mean(self.losslst))
writefile.write("\n")
writefile.write("\n")
self.losslst = np.zeros(steps)
您可以修改此回呼以列印指標:logs["accuracy"]例如,只需列印即可。
uj5u.com熱心網友回復:
我在 python 中使用 tensorflow keras 但是,我的初始搜索將在歷史記錄中保存擬合后模型相關日志記錄中的所有資訊(損失、驗證損失、準確性、F1 等)
我懷疑這在 R 中是一樣的 -
根據:https : //keras.rstudio.com/articles/training_visualization.html
只需為您的模型擬合呼叫分配一個歷史變數,例如:
history <- model %>%
fit(x, y,
epochs = 1000,
batch_size = 128,
validation_split = 0.2,
callbacks = callback_early_stopping(monitor = "val_loss", patience = 5),
verbose = 1)
將歷史轉換為資料框 ( as.data.frame(history)),您將在其中找到您的指標 - 指標的長度與模型訓練的時期數相同
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/383111.html
