我有一個資料框,它由 columns = id、Text、Media_location (這是影像檔案夾的相對路徑)組成。
現在,我正在嘗試像這樣加載 Text、Media_location 列:
features = df[['Text', 'Media_location']]
dataset = tf.data.Dataset.from_tensor_slices((features))
然后出現此錯誤:
Exception has occurred: ValueError
Failed to convert a NumPy array to a Tensor (Unsupported object type float).
During handling of the above exception, another exception occurred:
File "D:\Final\MultiCNN_test.py", line 114, in process_text_image
dataset = tf.data.Dataset.from_tensor_slices((features))
我認為這個錯誤即將到來,因為資料框列無法轉換為張量,但我不知道該怎么做,以消除錯誤。
uj5u.com熱心網友回復:
如果這些列具有相同的資料型別Text,Media_location則您的代碼將起作用:
import tensorflow as tf
import pandas as pd
df = pd.DataFrame(data={'Text': ['some text', 'some more text'],
'Media_location': ['/path/to/file1', '/path/to/file2']})
features = df[['Text', 'Media_location']]
dataset = tf.data.Dataset.from_tensor_slices((features))
for x in dataset:
print(x)
tf.Tensor([b'some text' b'/path/to/file1'], shape=(2,), dtype=string)
tf.Tensor([b'some more text' b'/path/to/file2'], shape=(2,), dtype=string)
但是,如果兩者都具有不同的資料型別,您將得到錯誤或類似的錯誤,因為張量不能具有混合資料型別。所以嘗試這樣的事情:
df = pd.DataFrame(data={'Text': [0.29, 0.58],
'Media_location': ['/path/to/file1', '/path/to/file2']})
dataset = tf.data.Dataset.from_tensor_slices((df['Text'], df['Media_location']))
for x in dataset:
print(x)
(<tf.Tensor: shape=(), dtype=float64, numpy=0.29>, <tf.Tensor: shape=(), dtype=string, numpy=b'/path/to/file1'>)
(<tf.Tensor: shape=(), dtype=float64, numpy=0.58>, <tf.Tensor: shape=(), dtype=string, numpy=b'/path/to/file2'>)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/429748.html
