tf.nn.dropout
确实不是施加了任何标准限制。我相信你要找的是“process the gradients before applying them”,使用tf.clip_by_norm
。
例如,而不是简单地:
# Create an optimizer + implicitly call compute_gradients() and apply_gradients()
optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(loss)
,你可以:
# Create an optimizer.
optimizer = tf.train.GradientDescentOptimizer(learning_rate)
# Compute the gradients for a list of variables.
grads_and_vars = optimizer.compute_gradients(loss, [weights1, weights2, ...])
# grads_and_vars is a list of tuples (gradient, variable).
# Do whatever you need to the 'gradient' part, for example cap them, etc.
capped_grads_and_vars = [(tf.clip_by_norm(gv[0], clip_norm=123.0, axes=0), gv[1])
for gv in grads_and_vars]
# Ask the optimizer to apply the capped gradients
optimizer = optimizer.apply_gradients(capped_grads_and_vars)
我希望这有助于。关于最后说明tf.clip_by_norm
的axes
参数:
- 如果你计算
tf.nn.xw_plus_b(x, weights, biases)
,或等价matmul(x, weights) + biases
,当x
和weights
尺寸分别为(batch, in_units)
和(in_units, out_units)
,那么你可能要设置axes == [0]
(因为在这个每个列的使用情况列详细说明对特定单位的所有传入权重)。
- 请注意上面变量的形状/尺寸以及是否/你想要
clip_by_norm
每个人!例如。如果某些[weights1, weights2, ...]
是矩阵,有些不是,并且您在grads_and_vars
上调用了clip_by_norm()
,其值与上述列表理解中的axes
值相同,但这并不意味着所有变量都是相同的!事实上,如果你幸运的话,这会导致一个奇怪的错误,如ValueError: Invalid reduction dimension 1 for input with 1 dimensions
,但否则它是一个非常鬼鬼祟祟的错误。
那不是'tf.clip_by_norm'而不是'tf.clip_by_value'吗? – MiniQuark