python - Tensorflow - 关于 mnist.train.next_batch()

标签 python machine-learning tensorflow mnist

当我搜索 mnist.train.next_batch() 时,我发现了这个 https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/learn/python/learn/datasets/mnist.py

在此代码中

 def next_batch(self, batch_size, fake_data=False, shuffle=True):
  """Return the next `batch_size` examples from this data set."""
  if fake_data:
  fake_image = [1] * 784
  if self.one_hot:
    fake_label = [1] + [0] * 9
  else:
    fake_label = 0
  return [fake_image for _ in xrange(batch_size)], [
      fake_label for _ in xrange(batch_size)
  ]
start = self._index_in_epoch
# Shuffle for the first epoch
if self._epochs_completed == 0 and start == 0 and shuffle:
  perm0 = numpy.arange(self._num_examples)
  numpy.random.shuffle(perm0)
  self._images = self.images[perm0]
  self._labels = self.labels[perm0]
# Go to the next epoch
if start + batch_size > self._num_examples:
  # Finished epoch
  self._epochs_completed += 1
  # Get the rest examples in this epoch
  rest_num_examples = self._num_examples - start
  images_rest_part = self._images[start:self._num_examples]
  labels_rest_part = self._labels[start:self._num_examples]
  # Shuffle the data
  if shuffle:
    perm = numpy.arange(self._num_examples)
    numpy.random.shuffle(perm)
    self._images = self.images[perm]
    self._labels = self.labels[perm]
  # Start next epoch
  start = 0
  self._index_in_epoch = batch_size - rest_num_examples
  end = self._index_in_epoch
  images_new_part = self._images[start:end]
  labels_new_part = self._labels[start:end]
  return numpy.concatenate((images_rest_part, images_new_part), axis=0) , numpy.concatenate((labels_rest_part, labels_new_part), axis=0)
else:
  self._index_in_epoch += batch_size
  end = self._index_in_epoch
  return self._images[start:end], self._labels[start:end]

我知道 mnist.train.next_batch(batch_size=100) 意味着它从 MNIST 数据集中随机选取 100 个数据。现在,这是我的问题

  1. shuffle=true 是什么意思?
  2. 如果我设置 next_batch(batch_size=100,fake_data=False, shuffle=False) 那么它会依次从 MNIST 数据集的开头到结尾选取 100 个数据?不是随机的吗?

最佳答案

Re 1,当shuffle=True时,数据中示例的顺序是随机的。 Re 2,是的,它应该尊重示例在 numpy 数组中的任何顺序。

关于python - Tensorflow - 关于 mnist.train.next_batch(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42896605/

相关文章:

machine-learning - 在嵌入式系统上加载经过训练的模型(无库)

c++ - 如何从 CNTK 导入 tensorflow 模型?

machine-learning - 用于理解上下文单词的自然语言处理技术

python - Tensorflow Bow Encoder 解释

tensorflow - 如何在 TensorFlow 中调整参差不齐的张量中的元素大小

javascript - Python:有没有办法获取由 Javascript 动态创建的 HTML?

python - 以元组中的固定元素为键,查找字典中的最大值

python - pipelinev 安装 django 不用每次都重新下载?

python - pandas value_counts()方法对应的索引

java - weka:自动应用属性选择