张量转换函数 numpy() 在 tf.estimator 模型函数中不起作用

tensor conversion function numpy() doesn't work within tf.estimator model function

我已经在 tensorflow v2.0 和 v1.12.0(使用 tf.enable_eager_execution() )上进行了尝试。所以显然,如果我在我的 main() 函数中使用下面显示的代码片段调用 numpy(),它就可以完美运行。但是,如果我在我的估算器模型函数中使用它,即 model_fn(features, labels, mode, params),那么它会抱怨 'Tensor' object has no attribute 'numpy'

ndarray = np.ones([3, 3])
tensor = tf.multiply(ndarray, 42)
print(tensor)
print(tensor.numpy())

有没有其他人遇到过类似的问题?对 tf.estimator 来说似乎是个大问题,不是吗?

不行。 Estimator API 与图形构造相关,它不完全支持 eager execution。根据官方文档。

Calling methods of Estimator will work while eager execution is enabled. However, the model_fn and input_fn is not executed eagerly

https://www.tensorflow.org/api_docs/python/tf/estimator/Estimator

TF 2.0 甚至不支持自定义估算器,只支持预制的估算器。