Search code examples
tensorflowtensorflow-estimatortensorflow2.0

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


I have tried this with both tensorflow v2.0 and v1.12.0 (with tf.enable_eager_execution() ). So apparently if I call numpy() with the code snippet shown below in my main() function, it works perfectly. However if I use it in my estimator model function i.e., model_fn(features, labels, mode, params) then it complains that 'Tensor' object has no attribute 'numpy'.

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

Has anyone else experienced similar problem? Seems like a big issue for tf.estimator no?


Solution

  • It won't work. Estimator API is tied to graph construction and it doesn't fully support eager execution. As per official documentation.

    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 won't even support custom estimators, only premade ones.