Is there anyway to show the training progress from tf.estimator.LinearClassifier().train()

60 Views Asked by At

Is there any way to show the training progress from the TensorFlow linear estimator: tf.estimator.LinearClassifier().train() similar to how the progress output would be with a model.fit() for each Epoch? 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

Here is a sample of my code

#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)
1

There are 1 best solutions below

0
R-Rothrock On

What I've been doing, and I'm sure this isn't recommended (but I've not seen another method) is to run linear_est.train multiple times, and access the return of linear_est.evaluate() as so:

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. If anyone else wants to answer this question, feel free; this answer seems like the only way, and I hope it isn't.