我已经建立了一个keras模型,并将它转换为一个估计器模型。在训练过程中,只输出损失值。我还想打印像accuracy这样的度量值,所以我想向train_spec添加一个LoggingTensorHook。但是LoggingTensorHook将张量名称作为参数。我试过几个名字,也查过张卡上的名字。但是它不起作用,错误如下图所示。我该怎么做才能得到正确的张量名字才能起作用?
错误引起的
KeyError: "The name 'dense_1_loss:0' refers to a Tensor which does not exist. The operation, 'dense_1_loss', does not exist in the graph."这里是我尝试过的示例代码:
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import sys
import numpy as np
import tensorflow as tf
def input_fn():
x = np.random.random((1024, 10))
y = np.random.randint(2, size=(1024, 1))
x = tf.cast(x, tf.float32)
dataset = tf.data.Dataset.from_tensor_slices((x, y))
dataset = dataset.repeat(100)
dataset = dataset.batch(32)
return dataset
def main(args):
if len(args) < 2:
print('You must specify model_dir for checkpoints such as'
' /tmp/tfkeras_example/.')
return
model_dir = args[1]
print('Using %s to store checkpoints.' % model_dir)
# Define a Keras Model.
model = tf.keras.Sequential()
model.add(tf.keras.layers.Dense(
16, activation='relu', input_shape=(10,)))
model.add(tf.keras.layers.Dense(1, activation='sigmoid'))
# Compile the model.
optimizer = tf.train.GradientDescentOptimizer(0.2)
model.compile(loss='binary_crossentropy',
optimizer=optimizer, metrics=['accuracy'])
model.summary()
tf.keras.backend.set_learning_phase(True)
# Define DistributionStrategies and convert the Keras Model to an
# Estimator that utilizes these DistributionStrateges.
# Evaluator is a single worker, so using MirroredStrategy.
config = tf.estimator.RunConfig(
experimental_distribute=tf.contrib.distribute.DistributeConfig(
train_distribute=tf.contrib.distribute.CollectiveAllReduceStrategy(),
eval_distribute=tf.contrib.distribute.MirroredStrategy()))
keras_estimator = tf.keras.estimator.model_to_estimator(
keras_model=model, config=config, model_dir=model_dir)
# Train and evaluate the model. Evaluation will be skipped if there is not an
# "evaluator" job in the cluster.
logging_hook = tf.estimator.LoggingTensorHook(
['dense_1_loss'], every_n_iter=10)
tf.estimator.train_and_evaluate(
keras_estimator,
train_spec=tf.estimator.TrainSpec(
input_fn=input_fn, hooks=[logging_hook]),
eval_spec=tf.estimator.EvalSpec(input_fn=input_fn))
if __name__ == '__main__':
tf.logging.set_verbosity(tf.logging.INFO)
tf.app.run(argv=sys.argv)这里是整个模型的一个子图

将度量添加到估计量
def my_auc(labels, predictions):
auc_metric = tf.keras.metrics.AUC(name="my_auc")
auc_metric.update_state(
y_true=labels, y_pred=predictions['classes'])
return {'auc': auc_metric}
# add metric to estimator
tf.estimator.add_metrics(keras_estimator, my_auc)发布于 2022-11-08 12:34:21
logging_hook = tf.estimator.LoggingTensorHook(
{"acc": "metrics/accuracy/Identity:0"}, every_n_iter=100)您可以通过生成pbtxt或tensorboard来获得这个op名称,我不确定"Identity“是否是正确的op,我选择它是因为它是tensorboard度量中的最后一个op
https://stackoverflow.com/questions/58054933
复制相似问题