TensorFlow构建了一种很好的存储数据的方式。例如,它用于存储示例中的MNIST数据:
>>> mnist
<tensorflow.examples.tutorials.mnist.input_data.read_data_sets.<locals>.DataSets object at 0x10f930630>
假设有一个输入和输出numpy数组。
>>> x = np.random.normal(0,1, (100, 10))
>>> y = np.random.randint(0, 2, 100)
如何在tf
数据集中转换它们?
我想使用像next_batch
这样的函数
发布于 2015-12-19 01:47:13
发布于 2018-04-13 05:52:05
最近,Tensorflow在其数据集api中添加了一个使用numpy数组的功能。详情请参见here。
下面是我从那里复制的代码片段:
# Load the training data into two NumPy arrays, for example using `np.load()`.
with np.load("/var/data/training_data.npy") as data:
features = data["features"]
labels = data["labels"]
# Assume that each row of `features` corresponds to the same row as `labels`.
assert features.shape[0] == labels.shape[0]
features_placeholder = tf.placeholder(features.dtype, features.shape)
labels_placeholder = tf.placeholder(labels.dtype, labels.shape)
dataset = tf.data.Dataset.from_tensor_slices((features_placeholder, labels_placeholder))
# [Other transformations on `dataset`...]
dataset = ...
iterator = dataset.make_initializable_iterator()
sess.run(iterator.initializer, feed_dict={features_placeholder: features,
labels_placeholder: labels})
发布于 2017-11-04 22:42:18
作为另一种选择,您可以使用函数tf.train.batch()
创建一批数据,同时消除对tf.placeholder
的使用。有关更多详细信息,请参阅文档。
>>> images = tf.constant(X, dtype=tf.float32) # X is a np.array
>>> labels = tf.constant(y, dtype=tf.int32) # y is a np.array
>>> batch_images, batch_labels = tf.train.batch([images, labels], batch_size=32, capacity=300, enqueue_many=True)
https://stackoverflow.com/questions/34361030
复制相似问题