【发布时间】:2020-12-10 08:07:46
【问题描述】:
问题
数据集可以是不同类型的元组的集合。我可以从元组创建数据集。
tf.data.Dataset.from_tensors(
([1, 2, 3], 'A')
)
-----
<TensorDataset shapes: ((3,), ()), types: (tf.int32, tf.string)>
如何从元组数组创建数据集?
tf.data.Dataset.from_tensors(
[
([1, 2, 3], 'A'),
([4, 5, 6], 'B')
]
)
----
ValueError: Can't convert non-rectangular Python sequence to Tensor.
Tensorflow Dataset IMDB review dataset是一个不同类型元组数组的例子,所以应该有办法。
import numpy as np
import tensorflow as tf
import tensorflow_datasets as tfds
imdb, info = tfds.load("imdb_reviews", with_info=True, as_supervised=True)
train_data, test_data = imdb['train'], imdb['test']
print(train_data.take(2))
for text, label in train_data.take(2).as_numpy_iterator():
print("{}, {}".format(text[0:64], label))
----
<TakeDataset shapes: ((), ()), types: (tf.string, tf.int64)>
b'Being a fan of silent films, I looked forward to seeing this pic', 0
b"I haven't seen this film in years so my knowledge is a little ru", 1
【问题讨论】:
-
我想你在找
from_tensor_slices。
标签: python tensorflow machine-learning keras tensorflow-datasets