【问题标题】:select random images from MNIST dataset从 MNIST 数据集中选择随机图像
【发布时间】:2018-09-11 03:46:11
【问题描述】:

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

我想要的是以下内容:

  1. 从 tensorflow.examples.tutorials.mnist 导入 MNIST 数据集
  2. 将从 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).

谁可以支持?

提前致谢

【问题讨论】:

    标签: tensorflow mnist


    【解决方案1】:

    使用 python 时的一个好习惯是使用python -i program.py 运行程序,这样您就可以使用 shell 以交互方式检查每个变量所包含的内容。如果您打印data.test.images,您会看到它是一个形状为 (10000, 784) 的 n-darray(n 维 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 上下文中处理的内容。

    【讨论】:

      猜你喜欢
      • 2022-01-01
      • 2013-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-05
      • 1970-01-01
      相关资源
      最近更新 更多