I try to use tf.metrics.mean_absolute_error to evaluate model during training process. However, it raise FailedPreconditionError. I have initilized all varaibles. I am so confused about the error.

def get_loss(prediction, label):
    return tf.losses.absolute_difference(tf.expand_dims(label, -1), prediction)


def make_train_op(opt, loss):
    apply_gradient_op = opt.minimize(loss)
    return apply_gradient_op


def evaluate_mae(prediction, label):
    return tf.metrics.mean_absolute_error(tf.cast(label, tf.float32), prediction)

optimizer = tf.train.AdamOptimizer()
global_step = tf.train.get_or_create_global_step()

with tf.variable_scope('input', reuse=True):
    training_inp = InputPipe()
    data_size = training_inp.data_size
    validate_inp = InputPipe(is_train=False)

model = Model(training_inp.feature_num)

scope = tf.get_variable_scope()
training_prediction, train_true_y = model(training_inp)
train_loss = get_loss(training_prediction, train_true_y)
train_op = make_train_op(optimizer, train_loss)

validate_prediction, validate_true_y = model(validate_inp)
validate_loss = evaluate_mae(validate_prediction, validate_true_y)

init = tf.global_variables_initializer()

with tf.Session() as sess:
    sess.run(init)
    training_inp.init_sess(sess)
    list_grads = []
    for epoch in range(n_repeats):
        tqr = trange(steps_per_epoch, desc="%2d" % (epoch + 1), leave=False)

        for _ in tqr:
            op, train_loss_value, validate_loss_value = sess.run([train_op, train_loss, validate_loss])

The error is

FailedPreconditionError (see above for traceback): Attempting to use uninitialized value mean_absolute_error/count [[node mean_absolute_error/count/read (defined at D:/OneDrive/Programs/python_ai/test/trainer_with_validate.py:27) ]]

line 27 is:

return tf.metrics.mean_absolute_error(tf.cast(label, tf.float32), prediction)

0 Answers