【问题标题】:Adding DROPOUT to Tensorflow CIFAR10 Deep CNN Example将 DROPOUT 添加到 Tensorflow CIFAR10 Deep CNN 示例
【发布时间】:2016-10-18 03:12:55
【问题描述】:

我希望在 tensorflow CIFAR10 教程示例代码中添加 dropout,但遇到了一些困难。

Deep MNIST tensorflow 教程包含一个 dropout 示例,但它使用交互式图表,这与 CIFAR10 教程中使用的方法不同。此外,CIFAR10 教程没有使用占位符,也没有使用 feed_dict 将变量传递给优化器,MNIST 模型使用它来传递训练的丢失概率。

我正在尝试什么:

在 cifar10_train.train() 中,我在默认图表下定义了辍学概率占位符;那就是:

def train():
  """Train CIFAR-10 for a number of steps."""
  with tf.Graph().as_default():
    global_step = tf.Variable(0, trainable=False)
    keep_drop_prob = = tf.placeholder(tf.float32)

在下面,仍然在 train() 模块中,当我通过调用 cifar10.inference() 构建计算图时,我还传递了 keep_drop_prob 占位符,如下所示:

"""Build a Graph that computes the logits predictions from the
inference model."""
logits = cifar10.inference(images, keep_drop_prob)

在 cifar10.inference() 模块中,我现在使用传递的 keep_drop_prob 占位符并使用它来定义我的 dropout 层,如下所示:

drop1 = tf.nn.dropout(norm1, keep_drop_prob)

现在我在计算损失时定义并传递 keep_drop_prob 的值,仍在 train() 模块中,如下所示:

"""Calculate loss."""
loss = cifar10.loss(logits, labels, keep_drop_prob = 0.5)

然后在 cifar10.loss() 模块中,我在计算交叉熵时使用传递的 keep_drop_prob 值,如下所示:

"""Calculate the average cross entropy loss across the batch."""
labels = tf.cast(labels, tf.int64)
cross_entropy = tf.nn.sparse_softmax_cross_entropy_with_logits(
    logits, labels, keep_drop_prob, name='cross_entropy_per_example')

现在,我不确定到目前为止我所做的是否正确,以及我接下来需要做什么。

任何帮助将不胜感激!

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    我相信我已经找到了解决方案。

    看来我走在了正确的轨道上,但是在传递 keep_drop_prob 占位符时有点过火了。

    要添加 dropout,我做了以下操作:

    我在 cifar10_train.train() 模块中添加了 keep_drop_prob 占位符,如下所示:

    def train():
      """Train CIFAR-10 for a number of steps."""
      with tf.Graph().as_default():
        global_step = tf.Variable(0, trainable=False)
        keep_drop_prob = = tf.placeholder(tf.float32)
    

    在 cifar10_train.train() 模块中构建图形时,我将占位符传递给它,但也定义它的值

    """Build a Graph that computes the logits predictions from the
    inference model."""
    logits = cifar10.inference(images, keep_drop_prob=0.5)
    

    在 cifar10.inference() 模块中,我现在获取传递的 keep_drop_prob 占位符,并使用它来定义我的 dropout 层,并将其传递给用于登录 tensorboard 的激活摘要:

    drop1 = tf.nn.dropout(norm1, keep_drop_prob)
    _activation_summary(drop1) 
    

    当我查看张量板图表时,我看到了我的 dropout 操作。我还可以询问 dropout 操作中的 keep_prob 变量,并通过更改构建 logits 图时传递的值来影响其 value 属性。

    我的下一个测试是将 keep_drop_prob 设置为 1 和 0,并确保我从我的网络中获得预期的结果。

    我不确定这是实现 dropout 的最有效方式,但我相当肯定它有效。

    注意,我只有一个 keep_drop_prob 占位符,我将它传递给许多层 dropout(每个卷积 atm 之后一层)。我认为 tensorflow 对每个 dropout 操作使用唯一的分布,而不是需要一个唯一的占位符。

    编辑:不要忘记对 eval 模块进行必要的更改,但将值 1 传递给 dropout。

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 1970-01-01
      • 2019-12-22
      • 1970-01-01
      • 2019-10-19
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 2016-02-20
      相关资源
      最近更新 更多