Tensorflow 从MNIST数据集中选择随机图像

Tensorflow 从MNIST数据集中选择随机图像,tensorflow,mnist,Tensorflow,Mnist,首先,我必须承认我在python和TensorFlow方面的经验有限。我正在寻找一些关于从TensorFlow示例导入的MNIST图像操作的支持 我想说的是: 从tensorflow.examples.tutorials.MNIST导入MNIST数据集 将从MNIST中随机选择的一半图片存储在一个数组中,以便我可以对其进行操作 我正在编写的代码如下 import tensorflow as tf import numpy as np from tensorflow.examples.tutori

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

我想说的是:

从tensorflow.examples.tutorials.MNIST导入MNIST数据集 将从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,您将看到它是一个n-darray n维numpy数组,形状为10000、784。所以基本上它是一个矩阵,其中每一行是一个图像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上下文中要处理的内容