0

I have a 1x1 EagerTensor object that I'm trying to convert to a single float value. i.e.

tf.Tensor([[-0.04473801]], shape=(1, 1), dtype=float32) -> -0.04473801

There seemed to be a simple answer that I've used on other tensors in the past - just use the item() method a la this answer. However, when I try to use the .item() method on an EagerTensor object, I get the following error:

AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'item'

Why is this happening? Do EagerTensors not have the item() method? One workaround I've found is using float(tensor_variable.numpy()[0]), but it seems like there should be a better way.

jgholder
  • 302
  • 2
  • 12

1 Answers1

0

Use squeeze to remove dimensions of size 1:

import tensorflow as tf


t = tf.constant([[-0.04473801]])
tf.squeeze(t).numpy()
>>> -0.04473801
Plagon
  • 2,689
  • 1
  • 11
  • 23