简体   繁体   English

当张量不提供 numpy() 时如何获取数据

[英]How to get data out when tensor doesn't provide numpy()

I've got a tensor which is provided as data parameter by keras to my custom model's train_step .我有一个张量,它由 keras 作为data参数提供给我的自定义模型的train_step It seems to be handled just fine by the self(data, training=True) call. self(data, training=True)调用似乎可以很好地处理它。 For evaluation of result I want to look at the data though.为了评估结果,我想查看数据。

When I print it out, I get:当我打印出来时,我得到:

Tensor("IteratorGetNext:0", shape=(120, 1080), dtype=float32)

But I can't seem to get the data itself.但我似乎无法获得数据本身。 data.numpy() raises exception AttributeError: 'Tensor' object has no attribute 'numpy' . data.numpy()引发异常AttributeError: 'Tensor' object has no attribute 'numpy'

I don't have a session available at that point for eval() either.那时我也没有可用于eval()的 session 。 How can I get the data out?我怎样才能把数据拿出来?

You cannot access tensor values as NumPy arrays in graph mode.您不能在图形模式下访问张量值作为 NumPy arrays。 This data is "leaving" Python and therefore inaccessible ( read more ).此数据“离开” Python,因此无法访问(阅读更多)。 In order to access the tensors, you can run eagerly ( read more ) like this:为了访问张量,您可以像这样急切地运行阅读更多):

model.compile(... run_eagerly=True)

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

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