简体   繁体   English

无法记录使用Keras构建但使用Tensorflow的Estimator训练的模型的损失

[英]Cannot log the loss from a model built with Keras but trained with Tensorflow's Estimator

I created a neural model with Keras and want to train it with the Tensorflow Estimator API for Keras models. 我使用Keras创建了一个神经模型,并希望使用用于Keras 模型的Tensorflow Estimator API对其进行训练。

Further I want to simply log the loss value with a LoggingTensorHook. 此外,我只想用LoggingTensorHook记录损失值。 But I get the following error: 但是我收到以下错误:

ValueError: Passed Tensor("loss/mul:0", shape=(), dtype=float32) should have graph attribute that is equal to current graph <tensorflow.python.framework.ops.Graph object at 0x11fca7358>.

I have a rough idea of what is going on. 我对发生的事情有一个大概的了解。 (Estimator create a new graph or something), but can't solve this issue by myself. (估算者创建一个新图形或其他图形),但我自己无法解决此问题。


Now some code: 现在一些代码:

model = create_keras_model(shapes)

adam = tf.keras.optimizers.Adam(lr=conf.lr)
model.compile(loss='categorical_crossentropy',
              optimizer=adam, metrics=['acc'])

estimator = tf.keras.estimator.model_to_estimator(
    keras_model=model, model_dir=model_dir,config=run_config)


### I want to log this tensor
loss_to_log = model.total_loss
log_hook = tf.train.LoggingTensorHook(
    {'loss': loss_to_log}, every_n_iter=10, at_end=True)

train_spec = tf.estimator.TrainSpec(
    input_fn=...,
    max_steps=..., hooks=[log_hook])

tf.estimator.train_and_evaluate(estimator, train_spec, ...)

Try add codes: 尝试添加代码:

import tensorflow as tf
tf.logging.set_verbosity(tf.logging.INFO)

and modify your codes 并修改您的代码

{'loss': loss_to_log}

to

{'loss': loss_to_log.name}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM