我正在使用Tensorflow 1.4。
我创建了一个自定义的tf.estimator来进行分类,就像这样:
def model_fn():
# Some operations here
[...]
return tf.estimator.EstimatorSpec(mode=mode,
predictions={"Preds": predictions},
loss=cost,
train_op=loss,
eval_metric_ops=eval_metric_ops,
training_hooks=[summary_hook])
my_estimator = tf.estimator.Estimator(model_fn=model_fn,
params=model_params,
model_dir='/my/directory')
我可以轻松地训练它:
input_fn = create_train_input_fn(path=train_files)
my_estimator.train(input_fn=input_fn)
其中,input_fn 是一个从tfrecords文件中读取数据的函数,使用 tf.data.Dataset API。
由于我是从 tfrecords 文件中读取数据,因此在进行预测时,我没有内存中的标签。
我的问题是,如何通过predict()方法或evaluate()方法返回预测结果和标签?
似乎没有办法两者兼得。 predict()没有访问标签的权限,而使用evaluate()方法无法访问predictions字典。
tf.Session()
,但我无法执行sess.run([predictions, labels])
,因为标签是使用tf.data.Dataset
API从tfrecords动态读取的,并且似乎我不能保存一个Tensor,它保存这些标签值并通过我的检查点检索。 - Benjamin Larrousse