本文整理汇总了Python中tensorflow.contrib.framework.arg_scope方法的典型用法代码示例。如果您正苦于以下问题:Python framework.arg_scope方法的具体用法?Python framework.arg_scope怎么用?Python framework.arg_scope使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow.contrib.framework
的用法示例。
在下文中一共展示了framework.arg_scope方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: set_args
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def set_args(largs, conv_bias=True, weights_regularizer=None):
from .layers import conv2d
from .layers import fc
from .layers import sconv2d
def real_set_args(func):
def wrapper(*args, **kwargs):
is_training = kwargs.get('is_training', False)
layers = sum([x for (x, y) in largs(is_training)], [])
layers_args = [arg_scope(x, **y) for (x, y) in largs(is_training)]
if not conv_bias:
layers_args += [arg_scope([conv2d], biases_initializer=None)]
if weights_regularizer is not None:
layers_args += [arg_scope(
[conv2d, fc, sconv2d],
weights_regularizer=weights_regularizer)]
with arg_scope(layers, outputs_collections=__outputs__):
with arg_scopes(layers_args):
x = func(*args, **kwargs)
x.model_name = func.__name__
return x
return wrapper
return real_set_args
示例2: step_fn
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def step_fn(wrapped_fn):
""" Wrap an RNN class method's step function to implement basic expected behavior """
@functools.wraps(wrapped_fn)
def wrapper(self, *args, **kwargs):
""" Determine scope reuse and keep track of states and outputs """
with framework.arg_scope(
[self.collect_named_outputs],
outputs_collections=kwargs.get('outputs_collections')):
with tf.variable_scope(self.variable_scope, reuse=self.reuse):
output, state = wrapped_fn(self, *args, **kwargs)
output = tf.identity(output, name='rnn_output')
self.outputs.append(output)
self.states.append(state)
return self.collect_named_outputs(output)
return wrapper
示例3: __call__
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def __call__(self, outputs_collections=None):
""" Execute the next time step of the GLAS model """
# Get the current output and decoded output (or zeros if they do not exist yet)
output = self.output if self.step > 0 else 0.0
decoded = self.decoder.output if self.step > 0 else tf.zeros((1, self.decoder.output_size))
with framework.arg_scope(
[self.decoder.next, self.sampler.random_sample],
outputs_collections=outputs_collections):
# sample from the approximate posterior
sample = self.sampler.random_sample()
# decode from the latent space
decoded = self.decoder(sample)
written = self.attention.write(decoded)
return output + written, None
示例4: testShareParams
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testShareParams(self):
# Tests reuse option.
first_outputs = 2
alternate_num_outputs = 12
parameterization = {'first/Conv2D': first_outputs}
decorator = ops.ConfigurableOps(parameterization=parameterization)
explicit = layers.conv2d(
self.inputs, first_outputs, 3, scope='first')
with arg_scope([layers.conv2d], reuse=True):
decorated = decorator.conv2d(
self.inputs,
num_outputs=alternate_num_outputs,
kernel_size=3,
scope='first')
with self.cached_session():
tf.global_variables_initializer().run()
# verifies that parameters are shared.
self.assertAllClose(explicit.eval(), decorated.eval())
conv_ops = sorted([
op.name
for op in tf.get_default_graph().get_operations()
if op.type == 'Conv2D'
])
self.assertAllEqual(['first/Conv2D', 'first_1/Conv2D'], conv_ops)
示例5: testConcatOpGetRegularizer
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testConcatOpGetRegularizer(self, use_batch_norm, use_partitioner):
sc = self._batch_norm_scope() if use_batch_norm else []
partitioner = tf.fixed_size_partitioner(2) if use_partitioner else None
model_stub = add_concat_model_stub
with arg_scope(sc):
with tf.variable_scope(tf.get_variable_scope(), partitioner=partitioner):
final_op = add_concat_model_stub.build_model()
# Instantiate OpRegularizerManager.
op_handler_dict = self._default_op_handler_dict
op_handler_dict['FusedBatchNormV3'] = StubBatchNormSourceOpHandler(
model_stub)
if not use_batch_norm:
op_handler_dict['Conv2D'] = StubConvSourceOpHandler(model_stub)
op_reg_manager = orm.OpRegularizerManager([final_op], op_handler_dict)
expected_alive = model_stub.expected_alive()
expected = np.logical_or(expected_alive['conv4'], expected_alive['concat'])
conv_reg = op_reg_manager.get_regularizer(_get_op('conv4/Conv2D'))
self.assertAllEqual(expected, conv_reg.alive_vector)
relu_reg = op_reg_manager.get_regularizer(_get_op('conv4/Relu'))
self.assertAllEqual(expected, relu_reg.alive_vector)
示例6: testGroupConcatOpGetRegularizerValues
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testGroupConcatOpGetRegularizerValues(self, op_name, short_name):
model_stub = grouping_concat_model_stub
with arg_scope(self._batch_norm_scope()):
with tf.variable_scope(tf.get_variable_scope()):
final_op = model_stub.build_model()
# Instantiate OpRegularizerManager.
op_handler_dict = self._default_op_handler_dict
op_handler_dict['FusedBatchNormV3'] = StubBatchNormSourceOpHandler(
model_stub)
op_reg_manager = orm.OpRegularizerManager([final_op], op_handler_dict)
expected_alive = model_stub.expected_alive()
expected_reg = model_stub.expected_regularization()
reg = op_reg_manager.get_regularizer(_get_op(op_name))
self.assertAllEqual(expected_alive[short_name], reg.alive_vector)
self.assertAllClose(expected_reg[short_name], reg.regularization_vector)
示例7: testGroupConcatOpGetRegularizerObjects
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testGroupConcatOpGetRegularizerObjects(self):
model_stub = grouping_concat_model_stub
with arg_scope(self._batch_norm_scope()):
with tf.variable_scope(tf.get_variable_scope()):
final_op = model_stub.build_model()
# Instantiate OpRegularizerManager.
op_handler_dict = self._default_op_handler_dict
op_handler_dict['FusedBatchNormV3'] = StubBatchNormSourceOpHandler(
model_stub)
op_reg_manager = orm.OpRegularizerManager([final_op], op_handler_dict)
self.assertEqual(
op_reg_manager.get_regularizer(_get_op('conv1/Conv2D')),
op_reg_manager.get_regularizer(_get_op('conv2/Conv2D')))
self.assertEqual(
op_reg_manager.get_regularizer(_get_op('conv3/Conv2D')),
op_reg_manager.get_regularizer(_get_op('conv4/Conv2D')))
示例8: testGather
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testGather(self):
gather_index = [5, 6, 7, 8, 9, 0, 1, 2, 3, 4]
with arg_scope(self._batch_norm_scope()):
inputs = tf.zeros([2, 4, 4, 3])
c1 = layers.conv2d(inputs, num_outputs=10, kernel_size=3, scope='conv1')
gather = tf.gather(c1, gather_index, axis=3)
manager = orm.OpRegularizerManager(
[gather.op], self._default_op_handler_dict)
c1_reg = manager.get_regularizer(_get_op('conv1/Conv2D'))
gather_reg = manager.get_regularizer(_get_op('GatherV2'))
# Check regularizer indices.
self.assertAllEqual(list(range(10)), c1_reg.regularization_vector)
# This fails due to gather not being supported. Once gather is supported,
# this test can be enabled to verify that the regularization vector is
# gathered in the same ordering as the tensor.
# self.assertAllEqual(
# gather_index, gather_reg.regularization_vector)
# This test shows that gather is not supported. The regularization vector
# has the same initial ordering after the gather op scrambled the
# channels. Remove this once gather is supported.
self.assertAllEqual(list(range(10)), gather_reg.regularization_vector)
示例9: testInit_AddConcat_AllOps
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testInit_AddConcat_AllOps(self):
with arg_scope(self._batch_norm_scope()):
inputs = tf.zeros([2, 4, 4, 3])
c1 = layers.conv2d(inputs, num_outputs=10, kernel_size=3, scope='conv1')
c2 = layers.conv2d(inputs, num_outputs=10, kernel_size=3, scope='conv2')
add = c1 + c2
c3 = layers.conv2d(add, num_outputs=10, kernel_size=3, scope='conv3')
out = tf.identity(c3)
concat = tf.concat([c1, c2], axis=3)
c4 = layers.conv2d(concat, num_outputs=10, kernel_size=3, scope='conv4')
manager = orm.OpRegularizerManager(
[out.op], self._default_op_handler_dict, SumGroupingRegularizer)
# Op c4 is not in the DFS path of out. Verify that OpRegularizerManager
# does not process c4.
self.assertNotIn(c4.op, manager.ops)
self.assertNotIn(concat.op, manager.ops)
示例10: testInit_Blacklist
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testInit_Blacklist(self):
with arg_scope(self._batch_norm_scope()):
inputs = tf.zeros([2, 4, 4, 3])
c1 = layers.conv2d(inputs, num_outputs=3, kernel_size=3, scope='conv1')
c2 = layers.conv2d(c1, num_outputs=4, kernel_size=3, scope='conv2')
c3 = layers.conv2d(c2, num_outputs=5, kernel_size=3, scope='conv3')
# Verify c2 has a regularizer.
manager = orm.OpRegularizerManager(
[c3.op], self._default_op_handler_dict, SumGroupingRegularizer)
self.assertIsNotNone(manager.get_regularizer(c2.op))
# Verify c2 has None regularizer after blacklisting.
manager = orm.OpRegularizerManager(
[c3.op], self._default_op_handler_dict, SumGroupingRegularizer,
regularizer_blacklist=['conv2'])
self.assertIsNone(manager.get_regularizer(c2.op))
示例11: testInit_BlacklistGroup
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def testInit_BlacklistGroup(self):
with arg_scope(self._batch_norm_scope()):
inputs = tf.zeros([2, 4, 4, 3])
c1 = layers.conv2d(inputs, num_outputs=10, kernel_size=3, scope='conv1')
c2 = layers.conv2d(inputs, num_outputs=10, kernel_size=3, scope='conv2')
add = c1 + c2
c3 = layers.conv2d(add, num_outputs=10, kernel_size=3, scope='conv3')
# Verify c2 has a regularizer.
manager = orm.OpRegularizerManager(
[c3.op], self._default_op_handler_dict, SumGroupingRegularizer)
self.assertIsNotNone(manager.get_regularizer(c2.op))
# Verify c2 has None regularizer after blacklisting c1 which is grouped.
manager = orm.OpRegularizerManager(
[c3.op], self._default_op_handler_dict, SumGroupingRegularizer,
regularizer_blacklist=['conv1'])
self.assertIsNone(manager.get_regularizer(c2.op))
示例12: _build_model
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def _build_model(self):
with arg_scope([resnet_block, conv2d, max_pool2d, tanh],
layer_dict=self.layer_dict):
self.R_x = self._build_refiner(self.normalized_x)
self.denormalized_R_x = denormalize(self.R_x)
self.D_y, self.D_y_logits = \
self._build_discrim(self.normalized_y, name="D_y")
self.D_R_x, self.D_R_x_logits = \
self._build_discrim(self.R_x, name="D_R_x", reuse=True)
self.D_R_x_history, self.D_R_x_history_logits = \
self._build_discrim(self.R_x_history,
name="D_R_x_history", reuse=True)
#self.estimate_outputs = self._build_estimation_network()
self._build_loss()
示例13: _localize
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def _localize(self, preprocessed_images):
k = self._num_control_points
conv_output = self._convnet.extract_features(preprocessed_images)[-1]
batch_size = shape_utils.combined_static_and_dynamic_shape(conv_output)[0]
conv_output = tf.reshape(conv_output, [batch_size, -1])
with arg_scope(self._fc_hyperparams):
fc1 = fully_connected(conv_output, 512)
fc2_weights_initializer = tf.zeros_initializer()
fc2_biases_initializer = tf.constant_initializer(self._init_bias)
fc2 = fully_connected(0.1 * fc1, 2 * k,
weights_initializer=fc2_weights_initializer,
biases_initializer=fc2_biases_initializer,
activation_fn=None,
normalizer_fn=None)
if self._summarize_activations:
tf.summary.histogram('fc1', fc1)
tf.summary.histogram('fc2', fc2)
if self._activation == 'sigmoid':
ctrl_pts = tf.sigmoid(fc2)
elif self._activation == 'none':
ctrl_pts = fc2
else:
raise ValueError('Unknown activation: {}'.format(self._activation))
ctrl_pts = tf.reshape(ctrl_pts, [batch_size, k, 2])
return ctrl_pts
示例14: predict
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def predict(self, inputs, scope=None):
with tf.variable_scope(scope, 'BidirectionalRnn', [inputs]) as scope:
(output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn(
self._fw_cell, self._bw_cell, inputs, time_major=False, dtype=tf.float32)
rnn_outputs = tf.concat([output_fw, output_bw], axis=2)
filter_weights = lambda vars : [x for x in vars if x.op.name.endswith('kernel')]
tf.contrib.layers.apply_regularization(
self._rnn_regularizer,
filter_weights(self._fw_cell.trainable_weights))
tf.contrib.layers.apply_regularization(
self._rnn_regularizer,
filter_weights(self._bw_cell.trainable_weights))
if self._num_output_units > 0:
with arg_scope(self._fc_hyperparams):
rnn_outputs = fully_connected(rnn_outputs, self._num_output_units, activation_fn=tf.nn.relu)
if self._summarize_activations:
max_time = rnn_outputs.get_shape()[1].value
for t in range(max_time):
activation_t = rnn_outputs[:,t,:]
tf.summary.histogram('Activations/{}/Step_{}'.format(scope.name, t), activation_t)
return rnn_outputs
示例15: Batch_Normalization
# 需要导入模块: from tensorflow.contrib import framework [as 别名]
# 或者: from tensorflow.contrib.framework import arg_scope [as 别名]
def Batch_Normalization(x, training, scope):
with arg_scope([batch_norm],
scope=scope,
updates_collections=None,
decay=0.9,
center=True,
scale=True,
zero_debias_moving_mean=True) :
return tf.cond(training,
lambda : batch_norm(inputs=x, is_training=training, reuse=None),
lambda : batch_norm(inputs=x, is_training=training, reuse=True))