首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >TensorFlow从numpy数组创建数据集

TensorFlow从numpy数组创建数据集
EN

Stack Overflow用户
提问于 2015-12-19 01:35:08
回答 3查看 21.5K关注 0票数 21

TensorFlow构建了一种很好的存储数据的方式。例如,它用于存储示例中的MNIST数据:

代码语言:javascript
运行
复制
>>> mnist
<tensorflow.examples.tutorials.mnist.input_data.read_data_sets.<locals>.DataSets object at 0x10f930630>

假设有一个输入和输出numpy数组。

代码语言:javascript
运行
复制
>>> x = np.random.normal(0,1, (100, 10))
>>> y = np.random.randint(0, 2, 100)

如何在tf数据集中转换它们?

我想使用像next_batch这样的函数

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2015-12-19 01:47:13

Dataset对象只是MNIST教程的一部分,而不是主TensorFlow库。

您可以在此处查看它的定义位置:

GitHub Link

该构造函数接受一个图像和标签参数,因此您可以在其中传递您自己的值。

票数 9
EN

Stack Overflow用户

发布于 2018-04-13 05:52:05

最近,Tensorflow在其数据集api中添加了一个使用numpy数组的功能。详情请参见here

下面是我从那里复制的代码片段:

代码语言:javascript
运行
复制
# 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})
票数 2
EN

Stack Overflow用户

发布于 2017-11-04 22:42:18

作为另一种选择,您可以使用函数tf.train.batch()创建一批数据,同时消除对tf.placeholder的使用。有关更多详细信息,请参阅文档。

代码语言:javascript
运行
复制
>>> 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)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34361030

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档