向Tensorflow CIFAR10添加辍学示例

向Tensorflow CIFAR10添加辍学示例,tensorflow,Tensorflow,我希望在tensorflow CIFAR10教程示例代码中添加中途退出,但遇到了一些困难 Deep MNIST tensorflow教程包含一个辍学示例,但是它使用了一个交互式图形,这与CIFAR10教程使用的方法不同。此外,CIFAR10教程没有使用占位符,也没有使用提要将变量传递给优化器,MNIST模型使用该优化器传递训练的退出概率 我正在尝试的是: 在cifar10_train.train()中,我在默认图形下定义了辍学概率占位符;即: def train(): """Train CI

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

Deep MNIST tensorflow教程包含一个辍学示例,但是它使用了一个交互式图形,这与CIFAR10教程使用的方法不同。此外,CIFAR10教程没有使用占位符,也没有使用提要将变量传递给优化器,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\u drop\u prob占位符,如下所示:

"""Build a Graph that computes the logits predictions from the
inference model."""
logits = cifar10.inference(images, keep_drop_prob)
drop1 = tf.nn.dropout(norm1, keep_drop_prob)
"""Calculate loss."""
loss = cifar10.loss(logits, labels, keep_drop_prob = 0.5)
"""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')
在cifar10.inference()模块中,我现在获取传递的keep\u drop\u prob占位符,并使用它定义我的退出层,如下所示:

"""Build a Graph that computes the logits predictions from the
inference model."""
logits = cifar10.inference(images, keep_drop_prob)
drop1 = tf.nn.dropout(norm1, keep_drop_prob)
"""Calculate loss."""
loss = cifar10.loss(logits, labels, keep_drop_prob = 0.5)
"""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')
现在,在计算损耗时,我定义并传递keep_drop_prob的值,仍然在train()模块中,如下所示:

"""Build a Graph that computes the logits predictions from the
inference model."""
logits = cifar10.inference(images, keep_drop_prob)
drop1 = tf.nn.dropout(norm1, keep_drop_prob)
"""Calculate loss."""
loss = cifar10.loss(logits, labels, keep_drop_prob = 0.5)
"""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')
然后在cifar10.loss()模块中,我在计算交叉熵时使用传递的keep\u drop\u prob值,如下所示:

"""Build a Graph that computes the logits predictions from the
inference model."""
logits = cifar10.inference(images, keep_drop_prob)
drop1 = tf.nn.dropout(norm1, keep_drop_prob)
"""Calculate loss."""
loss = cifar10.loss(logits, labels, keep_drop_prob = 0.5)
"""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')
现在,我不确定我到目前为止所做的是否正确,以及下一步需要做什么


任何帮助都将不胜感激

我相信我已经找到了解决办法

看起来我的思路是对的,但在传递keep_drop_prob占位符时有点过火了

为了增加辍学率,我做了以下工作:

我在cifar10\u train.train()模块中添加了keep\u drop\u 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占位符来定义我的退出层,并将其传递给激活摘要以登录tensorboard:

drop1 = tf.nn.dropout(norm1, keep_drop_prob)
_activation_summary(drop1) 
当我查看张力板图时,我看到了我的辍学op。我还可以在dropout op中查询keep_prob变量,并通过在构建logits图时更改传递的值来影响其value属性

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

我不确定这是否是实施辍学的最有效的方法,但我相当肯定它是有效的

注意,我只有一个keep\u drop\u prob占位符,我将它传递给许多层的drop out(每个卷积atm之后一个)。我认为tensorflow为每个辍学op使用唯一的分布,而不需要唯一的占位符

编辑:别忘了对eval模块进行必要的更改,但是为dropout传递一个值1