select 来自 MNIST 数据集的随机图像

select random images from MNIST dataset

首先,我必须承认我对 python 和 TensorFlow 的经验有限。我正在寻找有关处理从 TensorFlow 示例导入的 MNIST 图像的一些支持。

我想要的是:

  1. 从 tensorflow.examples.tutorials.mnist
  2. 导入 MNIST 数据集
  3. 将从 MNIST 中随机选择的一半图片存储在一个数组中,以便我可以对其进行操作

我写的代码如下

import tensorflow as tf
import numpy as np
from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets('data/MNIST/',one_hot=True)
import random

rndm_imgs = random.sample(data.test.images, len(data.test.images)/2)

我收到以下错误

    Traceback (most recent call last):
  File "<string>", line 7, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/random.py", line 320, in sample
    raise TypeError("Population must be a sequence or set.  For dicts, use list(d).")
TypeError: Population must be a sequence or set.  For dicts, use list(d).

有谁可以支持吗?

提前致谢

使用 python 的一个好习惯是 运行 使用 python -i program.py 你的程序,这样你就可以使用 shell 交互地检查你的每个变量抓住。如果您打印 data.test.images,您会看到它是一个形状为 (10000, 784) 的 n-darray(n-dimensional numpy 数组)。所以基本上它是一个矩阵,其中每一行都是一个图像(mnist 是 28x28,因此是 784)。

如果要使用python的内置random.sample函数进行采样,将数据矩阵转换为列表,使每个元素都是一个图像(784维或元素的向量) .

data_test_list = list(data.test.images)
test_samples = random.samples(data_test_list, len(data.test.images)/2)
test_samples = np.array(test_samples)

请注意,在获取样本后,我们将结果转换回 numpy 数组,因为这通常是您希望在 tensorflow 上下文中处理的内容。