[英]Convert a tensorflow tf.data.Dataset FlatMapDataset to TensorSliceDataset
[英]Convert list of tuples to tensorflow dataset (tf.data.Dataset)
來自 kaggle Natural Language Processing with Disaster Tweets 的數據
ds_train
>>>[("Already expecting to be inundated w/ articles about trad authors' pay plummeting by early next year but if this is true it'll be far worse",
0)
('@blazerfan not everyone can see ignoranceshe is Latinoand that is All she can ever benothing morebut an attack dog 4 a hate group GOP',
0),...]
`
像 [(X1, y1),...(X_n, y_n)]
或 dataframe
0 Just happened a terrible car crash
1 Heard about #earthquake is different cities, s...
2 there is a forest fire at spot pond, geese are...
我想將它轉換為 tensorflow 數據集。 我嘗試tf.data.Dataset.from_tensor_slices(ds_train)
但出現錯誤
ValueError:無法將混合類型的 Python 序列轉換為 Tensor。
一種選擇是拆分元組:
import tensorflow as tf
data = [("Already expecting to be inundated w/ articles about trad authors' pay plummeting by early next year but if this is true it'll be far worse", 0), ('@blazerfan not everyone can see ignoranceshe is Latinoand that is All she can ever benothing morebut an attack dog 4 a hate group GOP', 0)]
x, y = zip(*data)
dataset = tf.data.Dataset.from_tensor_slices((list(x), list(y)))
使用 dataframe:
dataset = tf.data.Dataset.from_tensor_slices((df['text'], df['target']))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.