TensorFlow 从 numpy 数组创建数据集
TensorFlow create dataset from numpy array
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
这样的函数
Dataset 对象只是 MNIST 教程的一部分,不是主要的 TensorFlow 库。
你可以在这里看到它的定义:
构造函数接受 images 和 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 在其数据集 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})
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
Dataset 对象只是 MNIST 教程的一部分,不是主要的 TensorFlow 库。
你可以在这里看到它的定义:
构造函数接受 images 和 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 在其数据集 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})