[英]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.