[英]How to convert a Tensor to Eager tensor in Tensorflow 2.1.0?
我一直在嘗試轉換類型的張量:
tensorflow.python.framework.ops.Tensor
對一個熱心的人:
<class 'tensorflow.python.framework.ops.EagerTensor'>
我一直在尋找解決方案,但找不到。 任何幫助,將不勝感激。
語境:
我已經使用 Keras Sequential 模型的特征提取方法獲得了張量。 輸出是第一個提到的類型的張量。 但是,當我嘗試使用 .numpy() 將其轉換為 numpy 時,它並沒有出現以下錯誤:
'Tensor' object has no attribute 'numpy'
但是當我嘗試使用 tf.constant 創建張量然后使用 .numpy() 來轉換它時,它工作正常!
我發現唯一的區別是張量的類型不同:Keras序列生成的張量是上面提到的第一種類型,而我手動創建的第二種張量是第二種類型(Eager tensor)。
如果您可以共享可重現的代碼,本可以更好地回答。
下面是一個簡單的場景,我在其中重新創建了您的錯誤。 在這里,我正在讀取圖像文件的路徑。
重新創建錯誤的代碼:
%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
def get_path(file_path):
print("file_path: ", bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
return file_path
train_dataset = tf.data.Dataset.list_files('/content/bird.png')
train_dataset = train_dataset.map(lambda x: (get_path(x)))
for one_element in train_dataset:
print(one_element)
輸出:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-8-2d5db8425f67> in <module>()
8
9 train_dataset = tf.data.Dataset.list_files('/content/bird.png')
---> 10 train_dataset = train_dataset.map(lambda x: (get_path(x)))
11
12 for one_element in train_dataset:
10 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
256 except Exception as e: # pylint:disable=broad-except
257 if hasattr(e, 'ag_error_metadata'):
--> 258 raise e.ag_error_metadata.to_exception(e)
259 else:
260 raise
AttributeError: in user code:
<ipython-input-8-2d5db8425f67>:10 None *
train_dataset = train_dataset.map(lambda x: (get_path(x)))
<ipython-input-8-2d5db8425f67>:6 get_path *
print("file_path: ", bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
AttributeError: 'Tensor' object has no attribute 'numpy'
以下是我在代碼中為修復此錯誤而實施的步驟。
tf.py_function(get_path, [x], [tf.string])
修飾了 map 函數。 您可以在此處找到有關 tf.py_function 的更多信息。bytes.decode(file_path.numpy())
來獲取字符串部分。固定代碼:
%tensorflow_version 2.x
import tensorflow as tf
import numpy as np
def get_path(file_path):
print("file_path: ",bytes.decode(file_path.numpy()),type(bytes.decode(file_path.numpy())))
return file_path
train_dataset = tf.data.Dataset.list_files('/content/bird.jpg')
train_dataset = train_dataset.map(lambda x: tf.py_function(get_path, [x], [tf.string]))
for one_element in train_dataset:
print(one_element)
輸出:
file_path: /content/bird.jpg <class 'str'>
(<tf.Tensor: shape=(), dtype=string, numpy=b'/content/bird.jpg'>,)
希望這能回答你的問題。
由於相同的錯誤出現在不同的場景中,所以再寫一個答案。
你得到的錯誤是因為版本問題 .ie tensorflow version 2.1.0
。 我通過跳過安裝tensorflow==2.1.0
和keras==2.3.1
的前兩段來運行代碼,並且錯誤沒有再次出現。
您的問題在最新版本的tensorflow version 2.3.0
消失了。 在最新版本上運行程序,這意味着不要再次安裝 tensorflow 和 keras,因為 Google Colab 已經預先安裝了最新且穩定的版本。
features.numpy()
輸出 -
array([[0. , 0.3728346, 0. , ..., 1.0103987, 0. ,
0.4194043]], dtype=float32)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.