[英]Convert list of tuples to tensorflow dataset (tf.data.Dataset)
Data from kaggle Natural Language Processing with Disaster Tweets来自 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),...]
` `
like [(X1, y1),...(X_n, y_n)]像 [(X1, y1),...(X_n, y_n)]
OR dataframe或 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...
I want to convert it to tensorflow datasets.我想将它转换为 tensorflow 数据集。 I tried tf.data.Dataset.from_tensor_slices(ds_train)
but got error我尝试tf.data.Dataset.from_tensor_slices(ds_train)
但出现错误
ValueError: Can't convert Python sequence with mixed types to Tensor. ValueError:无法将混合类型的 Python 序列转换为 Tensor。
One option would be to split the tuple:一种选择是拆分元组:
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)))
With a dataframe:使用 dataframe:
dataset = tf.data.Dataset.from_tensor_slices((df['text'], df['target']))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.