本文整理汇总了Python中sonnet.Linear方法的典型用法代码示例。如果您正苦于以下问题:Python sonnet.Linear方法的具体用法?Python sonnet.Linear怎么用?Python sonnet.Linear使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sonnet
的用法示例。
在下文中一共展示了sonnet.Linear方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _head
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _head(self, policy_input, heading, xy, target_xy):
"""Build the head of the agent: linear policy and value function, and pass
the auxiliary outputs through.
"""
# Linear policy and value function.
policy_logits = snt.Linear(
self._num_actions, name='policy_logits')(policy_input)
baseline = tf.squeeze(snt.Linear(1, name='baseline')(policy_input), axis=-1)
# Sample an action from the policy.
new_action = tf.multinomial(
policy_logits, num_samples=1, output_dtype=tf.int32)
new_action = tf.squeeze(new_action, 1, name='new_action')
return AgentOutput(
new_action, policy_logits, baseline, heading, xy, target_xy)
示例2: __init__
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def __init__(self,
output_sizes,
regularizers=None,
initializers=None,
custom_getter=None,
activation=_NONLINEARITY,
activate_final=False,
name='MLP'):
super(MLPManualReg, self).__init__(custom_getter=custom_getter, name=name)
self._output_sizes = output_sizes
self._activation = activation
self._activate_final = activate_final
with self._enter_variable_scope():
self._layers = [snt.Linear(self._output_sizes[i],
name='linear_{}'.format(i),
initializers=initializers,
regularizers=regularizers,
custom_getter=custom_getter,
use_bias=True)
for i in range(len(self._output_sizes))]
示例3: decoder
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def decoder(self, inputs):
with tf.variable_scope("decoder"):
l2_regularizer = tf.contrib.layers.l2_regularizer(self._l2_penalty_weight)
orthogonality_reg = get_orthogonality_regularizer(
self._orthogonality_penalty_weight)
initializer = tf.initializers.glorot_uniform(dtype=self._float_dtype)
# 2 * embedding_dim, because we are returning means and variances
decoder_module = snt.Linear(
2 * self.embedding_dim,
use_bias=False,
regularizers={"w": l2_regularizer},
initializers={"w": initializer},
)
outputs = snt.BatchApply(decoder_module)(inputs)
self._orthogonality_reg = orthogonality_reg(decoder_module.w)
return outputs
示例4: testVerifiableModelWrapperResnet
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def testVerifiableModelWrapperResnet(self):
def _build(z0, is_training=False): # pylint: disable=unused-argument
input_size = np.prod(z0.shape[1:])
# We make a resnet-like structure.
z = snt.Linear(input_size)(z0)
z_left = tf.nn.relu(z)
z_left = snt.Linear(input_size)(z_left)
z = z_left + z0
return snt.Linear(2)(z)
z = tf.constant([[1, 2, 3, 4]], dtype=tf.float32)
wrapper = ibp.VerifiableModelWrapper(_build)
logits = wrapper(z)
self.assertLen(wrapper.input_wrappers, 1)
self.assertLen(wrapper.modules, 5)
# Check input has fanout 2, as it is the start of the resnet block.
self.assertEqual(wrapper.fanout_of(wrapper.input_wrappers[0]), 2)
for module in wrapper.modules:
self.assertEqual(wrapper.fanout_of(module), 1)
# Check propagation.
self._propagation_test(wrapper, z, logits)
示例5: testFCIntervalBounds
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def testFCIntervalBounds(self):
m = snt.Linear(1, initializers={
'w': tf.constant_initializer(1.),
'b': tf.constant_initializer(2.),
})
z = tf.constant([[1, 2, 3]], dtype=tf.float32)
m(z) # Connect to create weights.
m = ibp.LinearFCWrapper(m)
input_bounds = ibp.IntervalBounds(z - 1., z + 1.)
output_bounds = m.propagate_bounds(input_bounds)
with self.test_session() as sess:
sess.run(tf.global_variables_initializer())
l, u = sess.run([output_bounds.lower, output_bounds.upper])
l = l.item()
u = u.item()
self.assertAlmostEqual(5., l)
self.assertAlmostEqual(11., u)
示例6: custom_build
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def custom_build(inputs, is_training, keep_prob):
x_inputs = tf.reshape(inputs, [-1, 28, 28, 1])
"""A custom build method to wrap into a sonnet Module."""
outputs = snt.Conv2D(output_channels=32, kernel_shape=4, stride=2)(x_inputs)
outputs = snt.BatchNorm()(outputs, is_training=is_training)
outputs = tf.nn.relu(outputs)
outputs = tf.nn.max_pool(outputs, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
outputs = snt.Conv2D(output_channels=64, kernel_shape=4, stride=2)(outputs)
outputs = snt.BatchNorm()(outputs, is_training=is_training)
outputs = tf.nn.relu(outputs)
outputs = tf.nn.max_pool(outputs, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
outputs = snt.Conv2D(output_channels=1024, kernel_shape=1, stride=1)(outputs)
outputs = snt.BatchNorm()(outputs, is_training=is_training)
outputs = tf.nn.relu(outputs)
outputs = snt.BatchFlatten()(outputs)
outputs = tf.nn.dropout(outputs, keep_prob=keep_prob)
outputs = snt.Linear(output_size=10)(outputs)
# _activation_summary(outputs)
return outputs
示例7: custom_build
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def custom_build(self, inputs):
"""A custom build method to wrap into a sonnet Module."""
outputs = snt.Conv2D(output_channels=16, kernel_shape=[7, 7], stride=[1, 1])(inputs)
outputs = tf.nn.relu(outputs)
outputs = snt.Conv2D(output_channels=16, kernel_shape=[5, 5], stride=[1, 2])(outputs)
outputs = tf.nn.relu(outputs)
outputs = snt.Conv2D(output_channels=16, kernel_shape=[5, 5], stride=[1, 2])(outputs)
outputs = tf.nn.relu(outputs)
outputs = snt.Conv2D(output_channels=16, kernel_shape=[5, 5], stride=[2, 2])(outputs)
outputs = tf.nn.relu(outputs)
outputs = tf.nn.dropout(outputs, self.placeholders['keep_prob'])
outputs = snt.BatchFlatten()(outputs)
outputs = snt.Linear(128)(outputs)
outputs = tf.nn.relu(outputs)
return outputs
示例8: _build
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _build(self, inputs):
"""
Perform dense/fully connected layer with a activation function
"""
self._layer = snt.Linear(self._output_size, self._add_bias, self._initializers,
self._partitioners, self._regularizers, name='LinearWx')
output = self._layer(inputs)
# Add GraphKeys
if self._add_bias:
tf.add_to_collection(GraphKeys.BIASES, self._layer.b)
tf.add_to_collection(GraphKeys.WEIGHTS, self._layer.w)
tf.add_to_collection(GraphKeys.PRE_ACTIVATIONS, output)
if self._activation_fn is None or self._activation_fn == tf.identity:
return output
output = self._activation_fn(output)
# Add to GraphKeys for activation output
tf.add_to_collection(GraphKeys.ACTIVATIONS, output)
return output
# Below are just convenience to access properties from the underlying layer
示例9: _torso
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _torso(self, input_):
"""Processing of all the visual and language inputs to the LSTM core."""
# Extract the inputs
last_action, env_output = input_
last_reward, _, _, observation = env_output
frame = observation[self._idx_frame]
goal = observation[self._idx_goal]
goal = tf.to_float(goal)
# Convert to image to floats and normalise.
frame = tf.to_float(frame)
frame = snt.FlattenTrailingDimensions(dim_from=3)(frame)
frame /= 255.0
# Feed image through convnet.
with tf.variable_scope('convnet'):
# Convolutional layers.
conv_out = self._convnet(frame)
# Fully connected layer.
conv_out = snt.BatchFlatten()(conv_out)
conv_out = snt.Linear(256)(conv_out)
conv_out = tf.nn.relu(conv_out)
# Concatenate outputs of the visual and instruction pathways.
if self._feed_action_and_reward:
# Append clipped last reward and one hot last action.
tf.logging.info('Append last reward clipped to: %f', self._max_reward)
clipped_last_reward = tf.expand_dims(
tf.clip_by_value(last_reward, -self._max_reward, self._max_reward),
-1)
tf.logging.info('Append last action (one-hot of %d)', self._num_actions)
one_hot_last_action = tf.one_hot(last_action, self._num_actions)
tf.logging.info('Append goal:')
tf.logging.info(goal)
action_and_reward = tf.concat([clipped_last_reward, one_hot_last_action],
axis=1)
else:
action_and_reward = tf.constant([0], dtype=tf.float32)
return conv_out, action_and_reward, goal
示例10: _head
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _head(self, core_output):
"""Build the head of the agent: linear policy and value function."""
policy_logits = snt.Linear(
self._num_actions, name='policy_logits')(
core_output)
baseline = tf.squeeze(snt.Linear(1, name='baseline')(core_output), axis=-1)
# Sample an action from the policy.
new_action = tf.multinomial(
policy_logits, num_samples=1, output_dtype=tf.int32)
new_action = tf.squeeze(new_action, 1, name='new_action')
return AgentOutput(new_action, policy_logits, baseline)
示例11: _build
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _build(self, inputs):
if FLAGS.l2_reg:
regularizers = {'w': lambda w: FLAGS.l2_reg*tf.nn.l2_loss(w),
'b': lambda w: FLAGS.l2_reg*tf.nn.l2_loss(w),}
else:
regularizers = None
reshape = snt.BatchReshape([28, 28, 1])
conv = snt.Conv2D(2, 5, padding=snt.SAME, regularizers=regularizers)
act = _NONLINEARITY(conv(reshape(inputs)))
pool = tf.nn.pool(act, window_shape=(2, 2), pooling_type=_POOL,
padding=snt.SAME, strides=(2, 2))
conv = snt.Conv2D(4, 5, padding=snt.SAME, regularizers=regularizers)
act = _NONLINEARITY(conv(pool))
pool = tf.nn.pool(act, window_shape=(2, 2), pooling_type=_POOL,
padding=snt.SAME, strides=(2, 2))
flatten = snt.BatchFlatten()(pool)
linear = snt.Linear(32, regularizers=regularizers)(flatten)
return snt.Linear(10, regularizers=regularizers)(linear)
示例12: _build
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _build(self, x):
x = tf.to_float(x)
initializers={"w": tf.truncated_normal_initializer(stddev=0.01)}
lin = snt.Linear(self.size, use_bias=False, initializers=initializers)
z = lin(x)
scale = tf.constant(1., dtype=tf.float32)
offset = tf.get_variable(
"b",
shape=[1, z.shape.as_list()[1]],
initializer=tf.truncated_normal_initializer(stddev=0.1),
dtype=tf.float32
)
mean, var = tf.nn.moments(z, [0], keep_dims=True)
z = ((z - mean) * tf.rsqrt(var + 1e-6)) * scale + offset
x_p = self.activation_fn(z)
return z, x_p
# This needs to work by string name sadly due to how the variable replace
# works and would also work even if the custom getter approuch was used.
# This is verbose, but it should atleast be clear as to what is going on.
# TODO(lmetz) a better way to do this (the next 3 functions:
# _raw_name, w(), b() )
示例13: __init__
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def __init__(self, size, use_bias=True, init_const_mag=True):
self.size = size
self.use_bias = use_bias
self.init_const_mag = init_const_mag
super(Linear, self).__init__(name="commonLinear")
示例14: _build
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def _build(self, h):
with tf.device(self.device):
mod = snt.Linear(self.num_grad_channels)
ret = snt.BatchApply(mod)(h)
# return as [num_grad_channels] x [bs] x [num units]
return tf.transpose(ret, perm=self.perm)
示例15: bias_readout
# 需要导入模块: import sonnet [as 别名]
# 或者: from sonnet import Linear [as 别名]
def bias_readout(self, h):
with tf.device(self.remote_device):
mod = snt.Linear(1, name='bias_readout')
ret = snt.BatchApply(mod)(h)
return tf.squeeze(ret, 2)