Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python TensorFlow从numpy数组创建数据集_Python_Machine Learning_Tensorflow_Mnist - Fatal编程技术网

Python TensorFlow从numpy数组创建数据集

Python TensorFlow从numpy数组创建数据集,python,machine-learning,tensorflow,mnist,Python,Machine Learning,Tensorflow,Mnist,TensorFlow是一种存储数据的好方法。例如,用于存储示例中的MNIST数据: >>> mnist <tensorflow.examples.tutorials.mnist.input_data.read_data_sets.<locals>.DataSets object at 0x10f930630> 如何在tf数据集中转换它们 我想使用像next\u batch这样的函数Dataset对象只是MNIST教程的一部分,而不是主TensorFlo

TensorFlow是一种存储数据的好方法。例如,用于存储示例中的MNIST数据:

>>> mnist
<tensorflow.examples.tutorials.mnist.input_data.read_data_sets.<locals>.DataSets object at 0x10f930630>
如何在
tf
数据集中转换它们


我想使用像
next\u batch

这样的函数Dataset对象只是MNIST教程的一部分,而不是主TensorFlow库

您可以在此处看到其定义位置:


构造函数接受一个images and labels参数,因此您可以在其中传递自己的值。

作为替代,您可以使用函数
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)

最近,Tensorflow在其dataset api中添加了一个功能,以使用numpy数组。有关详细信息,请参阅

以下是我从那里复制的片段:

# 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})

好的,谢谢,我有这个嫌疑犯。我认为作为主库的一部分,它将是一个有用的工具。AFAIK numpy阵列上的任何批处理操作都需要执行数据的副本。这可能会导致较慢的算法原理是TensorFlow应该只是一个核心数学库,但其他开源库可以提供用于机器学习的额外抽象。类似于Theano,它在顶部构建了类似Pylearn2的库。如果要避免复制操作,可以使用基于队列的数据访问功能,而不是提供占位符。在tf2中,这不再有效。您知道tf2中的建议方式吗?对于tf2,请检查@MajidL。您知道如果您的所有数据集都不适合内存,如何执行此操作吗?您的意思是您的数据集采用NumPy格式,但无法加载到内存中?如果是这种情况,可能会有所帮助。
# 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})