本文整理汇总了Python中tensorflow.python.ops.nn.dropout函数的典型用法代码示例。如果您正苦于以下问题:Python dropout函数的具体用法?Python dropout怎么用?Python dropout使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了dropout函数的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testInvalidKeepProb
def testInvalidKeepProb(self):
x_dim = 40
y_dim = 30
t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
with self.assertRaises(ValueError):
nn.dropout(t, -1.0)
with self.assertRaises(ValueError):
nn.dropout(t, 1.1)
with self.assertRaises(ValueError):
nn.dropout(t, [0.0, 1.0])
with self.assertRaises(ValueError):
nn.dropout(t, array_ops.placeholder(dtypes.float64))
with self.assertRaises(ValueError):
nn.dropout(t, array_ops.placeholder(dtypes.float32, shape=[2]))
示例2: dropout
def dropout(inputs,
keep_prob=0.5,
noise_shape=None,
is_training=True,
outputs_collections=None,
scope=None):
"""Returns a dropout op applied to the input.
With probability `keep_prob`, outputs the input element scaled up by
`1 / keep_prob`, otherwise outputs `0`. The scaling is so that the expected
sum is unchanged.
Args:
inputs: the tensor to pass to the nn.dropout op.
keep_prob: A scalar `Tensor` with the same type as x. The probability
that each element is kept.
noise_shape: A 1-D `Tensor` of type `int32`, representing the
shape for randomly generated keep/drop flags.
is_training: A bool `Tensor` indicating whether or not the model
is in training mode. If so, dropout is applied and values scaled.
Otherwise, inputs is returned.
outputs_collections: collection to add the outputs.
scope: Optional scope for op_scope.
Returns:
a tensor representing the output of the operation.
"""
with ops.op_scope([inputs], scope, 'Dropout') as sc:
is_training = ops.convert_to_tensor(is_training)
outputs = control_flow_ops.cond(
is_training,
lambda: nn.dropout(inputs, keep_prob, noise_shape),
lambda: inputs)
return utils.collect_named_outputs(outputs_collections, sc, outputs)
示例3: testShapedDropout
def testShapedDropout(self):
# Runs dropout with 0-1 tensor 10 times, sum the number of ones and validate
# that it is producing approximately the right number of ones over a large
# number of samples, based on the keep probability. This time with shaped
# noise.
x_dim = 40 * 30
y_dim = 3
num_iter = 10
for keep_prob in [0.1, 0.5, 0.8]:
with self.test_session():
t = constant_op.constant(1.0,
shape=[x_dim, y_dim],
dtype=types.float32)
dropout = nn.dropout(t, keep_prob, noise_shape=[x_dim, 1])
self.assertEqual([x_dim, y_dim], dropout.get_shape())
final_count = 0
for _ in xrange(0, num_iter):
value = dropout.eval()
final_count += np.count_nonzero(value)
# Verifies that there are only two values: 0 and 1/keep_prob.
sorted_value = np.unique(np.sort(value))
self.assertEqual(0, sorted_value[0])
self.assertAllClose(1 / keep_prob, sorted_value[1])
# Check that we are in the 15% error range
expected_count = x_dim * y_dim * keep_prob * num_iter
rel_error = math.fabs(final_count - expected_count) / expected_count
print rel_error
self.assertTrue(rel_error < 0.15)
示例4: testShapedDropoutUnknownShape
def testShapedDropoutUnknownShape(self):
x_dim = 40
y_dim = 30
keep_prob = 0.5
x = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
dropout_x = nn.dropout(x, keep_prob, noise_shape=array_ops.placeholder(dtypes.int32))
self.assertEqual(x.get_shape(), dropout_x.get_shape())
示例5: call
def call(self, inputs, training=False):
if isinstance(training, bool):
training_bool = training
else:
training_bool = tensor_util.constant_value(training)
if training_bool is False:
return array_ops.identity(inputs)
dropped_inputs = nn.dropout(inputs, 1 - self.rate, noise_shape=self.noise_shape, seed=self.seed)
if training_bool is True:
return dropped_inputs
return control_flow_ops.cond(training, lambda: dropped_inputs, lambda: inputs)
示例6: testShapedDropoutCorrelation
def testShapedDropoutCorrelation(self):
# Runs a shaped dropout and tests that the correlations are correct.
x_dim = 40
y_dim = 30
num_iter = 10
for keep_prob in [0.1, 0.5, 0.8]:
with self.test_session():
t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
dropout = nn.dropout(t, keep_prob, noise_shape=[x_dim, 1])
self.assertEqual([x_dim, y_dim], dropout.get_shape())
for _ in xrange(0, num_iter):
value = dropout.eval()
# Verifies that each y column as only one type of activation.
for i in xrange(x_dim):
sorted_value = np.unique(np.sort(value[i, :]))
self.assertEqual(sorted_value.size, 1)
示例7: testShapedDropoutShapeError
def testShapedDropoutShapeError(self):
# Runs shaped dropout and verifies an error is thrown on misshapen noise.
x_dim = 40
y_dim = 30
keep_prob = 0.5
t = constant_op.constant(1.0, shape=[x_dim, y_dim], dtype=dtypes.float32)
with self.assertRaises(ValueError):
_ = nn.dropout(t, keep_prob, noise_shape=[x_dim, y_dim + 10])
with self.assertRaises(ValueError):
_ = nn.dropout(t, keep_prob, noise_shape=[x_dim, y_dim, 5])
with self.assertRaises(ValueError):
_ = nn.dropout(t, keep_prob, noise_shape=[x_dim + 3])
with self.assertRaises(ValueError):
_ = nn.dropout(t, keep_prob, noise_shape=[x_dim])
# test that broadcasting proceeds
_ = nn.dropout(t, keep_prob, noise_shape=[y_dim])
_ = nn.dropout(t, keep_prob, noise_shape=[1, y_dim])
_ = nn.dropout(t, keep_prob, noise_shape=[x_dim, 1])
_ = nn.dropout(t, keep_prob, noise_shape=[1, 1])
示例8: dropout
def dropout(tensor_in, prob, name=None):
"""Adds dropout node and stores probability tensor into graph collection.
Args:
tensor_in: Input tensor.
prob: Float or Tensor.
Returns:
Tensor of the same shape of `tensor_in`.
Raises:
ValueError: If `keep_prob` is not in `(0, 1]`.
"""
with ops.op_scope([tensor_in], name, "dropout") as name:
if isinstance(prob, float):
prob = vs.get_variable("prob", [],
initializer=init_ops.constant_initializer(prob),
trainable=False)
ops.add_to_collection(DROPOUTS, prob)
return nn.dropout(tensor_in, prob)
示例9: test_conv_bn_dropout
def test_conv_bn_dropout(self):
"""Test dropout precision of convolution batch norm graph."""
if test.is_gpu_available(cuda_only=True):
random_seed.set_random_seed(0)
x = _input([2, 8, 8, 1])
y = _conv_bn(x)
y = nn.dropout(y, rate=0.5)
y = _conv_bn(y)
y = array_ops.identity(y)
optimizer = gradient_descent.GradientDescentOptimizer(learning_rate=0.01)
g = optimizer.compute_gradients(y, [x])
output = (y, g)
output_val_ref, output_val, cost_graph = self._run(output)
node_map = _build_node_map(cost_graph.node)
self._assert_output_fp16(node_map, 'Conv2D')
self._assert_output_fp16(node_map, 'FusedBatchNorm')
self._assert_output_fp16(node_map, 'dropout/mul')
self._assert_output_fp16(node_map, 'Conv2D_1')
output_val_ref, output_val, cost_graph = self._run(output)
self.assertAllClose(output_val_ref, output_val, atol=1e-3, rtol=1e-3)
示例10: dropped_inputs
def dropped_inputs():
return nn.dropout(inputs, 1 - self.rate,
noise_shape=self._get_noise_shape(inputs),
seed=self.seed)
示例11: _dropout
def _dropout():
return nn.dropout(inputs, keep_prob, noise_shape)