我怎样才能找到keras模型被训练的历元数?

vof42yt1  于 2023-02-10  发布在  其他
关注(0)|答案(3)|浏览(168)

我怎样才能找到训练keras模型的历元数?
1.我使用callback_early_stopping()提前停止训练,以避免过度拟合。
1.我一直在使用callback_csv_logger()来记录训练性能。但有时,我训练100个keras模型,仅仅为了知道每个模型的纪元数量而记录整个训练是没有意义的。

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)
zbsbpyhn

zbsbpyhn1#

要打印历元数(无论你想打印什么),你可以使用回调函数。

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方法,并删除其他所有内容。
如果您想在每个时段后打印损失,您可以使用此修改版本:

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"]

l3zydbqr

l3zydbqr2#

我在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)

将历史记录转换为 Dataframe (as.data.frame(history)),您将在其中找到您的指标-指标的长度与模型在其上进行训练的时期数相同

3pvhb19x

3pvhb19x3#

也许这对答案没有多大帮助,但值得一提的是:
假设拟合使用给定的batch_size,并且没有EarlyStopping回调函数,那么可以推断出Python中训练过的时期的数量:

np.ceil(model.optimizer.iterations.numpy() / np.ceil(len(X_train) / batch_size))

Kera的github引用

相关问题