tensorflow 是否可以显示tf.estimator.LinearClassifier().train()的训练进度

g52tjvyc  于 2023-01-09  发布在  其他
关注(0)|答案(1)|浏览(115)

是否有任何方法可以显示TensorFlow线性估计量的训练进度:tf.estimator.LinearClassifier().train()类似于model.fit每个历元的www.example.com()进度输出方式?tensorflow==2.9.2

Epoch 1/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.4964 - accuracy: 0.8270
Epoch 2/10
1875/1875 [==============================] - 4s 2ms/step - loss: 0.3751 - accuracy: 0.8652
Epoch 3/10
1875/1875 [==============================] - 5s 3ms/step - loss: 0.3382 - accuracy: 0.8762

下面是我的代码示例

#input function 
def make_input_fn(data_df, label_df, num_epochs=1000, shuffle=True, batch_size=32):
  def input_function():  # inner function, this will be returned
    ds = tf.data.Dataset.from_tensor_slices((dict(data_df), label_df))  # create tf.data.Dataset object with data and its label
    if shuffle:
      ds = ds.shuffle(1000)  # randomize order of data
    ds = ds.batch(batch_size).repeat(num_epochs)  # split dataset into batches of 32 and repeat process for number of epochs
    return ds  # return a batch of the dataset
  return input_function  # return a function object for use

train_input_fn = make_input_fn(dftrain, y_train)  # here we will call the input_function that was returned to us to get a dataset object we can feed to the model
eval_input_fn = make_input_fn(dfeval, y_eval, num_epochs=1, shuffle=False)
pre_input_fn = make_input_fn(dfpre, y_pre, num_epochs=1, shuffle=False)

linear_est = tf.estimator.LinearClassifier(feature_columns=feature_columns)
 
linear_est.train(train_input_fn)  # train
result = linear_est.evaluate(eval_input_fn)
vjrehmav

vjrehmav1#

我一直在做的事情(我确信这不是推荐的方法(但我还没有看到其他方法))是多次运行linear_est.train,并访问linear_est.evaluate()的返回值,如下所示:

loss_by_epoch   = list()
current_loss    = 1.0
acceptable_loss = 0.4

while current_loss > acceptable_loss:
    linear_est.train(train_input_fn)
    result = linear_est.evaluate(eval_input_fn)
    current_loss = result['loss']
    loss_by_epoch.append(current_loss)
print(loss_by_epoch)

P. S.如果还有人想回答这个问题,请随意;这个答案似乎是唯一的办法,我希望不是

相关问题