本文整理匯總了Python中layers.conv2d方法的典型用法代碼示例。如果您正苦於以下問題:Python layers.conv2d方法的具體用法?Python layers.conv2d怎麽用?Python layers.conv2d使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類layers
的用法示例。
在下文中一共展示了layers.conv2d方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_LeNet
# 需要導入模塊: import layers [as 別名]
# 或者: from layers import conv2d [as 別名]
def get_LeNet(batch_size=batch_size, img_shape=(1, 28, 28), dtype="float32"):
data_shape = (batch_size,) + img_shape
data = relay.var("data", shape=data_shape, dtype=dtype)
conv1_bias = relay.var("conv1_bias")
conv1 = layers.conv2d(data, kernel_size=(3, 3), strides=(1, 1), padding=(1, 1), channels=6, name="conv1")
conv1 = relay.nn.bias_add(conv1, conv1_bias)
maxpool1 = relay.nn.max_pool2d(conv1, (2, 2), (2, 2))
conv2_bias = relay.var("conv2_bias")
conv2 = layers.conv2d(maxpool1, kernel_size=(5, 5), strides=(1, 1), padding=(0, 0), channels=16, name="conv2")
conv2 = relay.nn.bias_add(conv2, conv2_bias)
maxpool2 = relay.nn.max_pool2d(conv2, (2, 2), (2, 2))
bf1 = relay.nn.batch_flatten(maxpool2)
dense1 = layers.dense_without_bias(bf1, units=120, name="dense1")
dense2 = layers.dense_without_bias(dense1, units=84, name="dense2")
dense3 = layers.dense_without_bias(dense2, units=10, name="dense3")
softmax = relay.nn.softmax(dense3)
#label is from input
label = relay.var("data2", shape=(batch_size, 10), dtype=dtype)
loss = relay.nn.cross_entropy(softmax, label)
args = relay.analysis.free_vars(loss)
return relay.Function(args, loss)
示例2: __init__
# 需要導入模塊: import layers [as 別名]
# 或者: from layers import conv2d [as 別名]
def __init__(self, sess, input_shape, num_actions, reuse=False, is_training=True, name='train'):
super().__init__(sess, reuse)
self.initial_state = []
with tf.name_scope(name + "policy_input"):
self.X_input = tf.placeholder(tf.uint8, input_shape)
with tf.variable_scope("policy", reuse=reuse):
conv1 = conv2d('conv1', tf.cast(self.X_input, tf.float32) / 255., num_filters=32, kernel_size=(8, 8),
padding='VALID', stride=(4, 4),
initializer=orthogonal_initializer(np.sqrt(2)), activation=tf.nn.relu,
is_training=is_training)
conv2 = conv2d('conv2', conv1, num_filters=64, kernel_size=(4, 4), padding='VALID', stride=(2, 2),
initializer=orthogonal_initializer(np.sqrt(2)), activation=tf.nn.relu,
is_training=is_training)
conv3 = conv2d('conv3', conv2, num_filters=64, kernel_size=(3, 3), padding='VALID', stride=(1, 1),
initializer=orthogonal_initializer(np.sqrt(2)), activation=tf.nn.relu,
is_training=is_training)
conv3_flattened = flatten(conv3)
fc4 = dense('fc4', conv3_flattened, output_dim=512, initializer=orthogonal_initializer(np.sqrt(2)),
activation=tf.nn.relu, is_training=is_training)
self.policy_logits = dense('policy_logits', fc4, output_dim=num_actions,
initializer=orthogonal_initializer(np.sqrt(1.0)), is_training=is_training)
self.value_function = dense('value_function', fc4, output_dim=1,
initializer=orthogonal_initializer(np.sqrt(1.0)), is_training=is_training)
with tf.name_scope('value'):
self.value_s = self.value_function[:, 0]
with tf.name_scope('action'):
self.action_s = noise_and_argmax(self.policy_logits)
示例3: __build
# 需要導入模塊: import layers [as 別名]
# 或者: from layers import conv2d [as 別名]
def __build(self):
self.__init_global_epoch()
self.__init_global_step()
self.__init_input()
with tf.name_scope('Preprocessing'):
red, green, blue = tf.split(self.X, num_or_size_splits=3, axis=3)
preprocessed_input = tf.concat([
tf.subtract(blue, ShuffleNet.MEAN[0]) * ShuffleNet.NORMALIZER,
tf.subtract(green, ShuffleNet.MEAN[1]) * ShuffleNet.NORMALIZER,
tf.subtract(red, ShuffleNet.MEAN[2]) * ShuffleNet.NORMALIZER,
], 3)
x_padded = tf.pad(preprocessed_input, [[0, 0], [1, 1], [1, 1], [0, 0]], "CONSTANT")
conv1 = conv2d('conv1', x=x_padded, w=None, num_filters=self.output_channels['conv1'], kernel_size=(3, 3),
stride=(2, 2), l2_strength=self.args.l2_strength, bias=self.args.bias,
batchnorm_enabled=self.args.batchnorm_enabled, is_training=self.is_training,
activation=tf.nn.relu, padding='VALID')
padded = tf.pad(conv1, [[0, 0], [0, 1], [0, 1], [0, 0]], "CONSTANT")
max_pool = max_pool_2d(padded, size=(3, 3), stride=(2, 2), name='max_pool')
stage2 = self.__stage(max_pool, stage=2, repeat=3)
stage3 = self.__stage(stage2, stage=3, repeat=7)
stage4 = self.__stage(stage3, stage=4, repeat=3)
global_pool = avg_pool_2d(stage4, size=(7, 7), stride=(1, 1), name='global_pool', padding='VALID')
logits_unflattened = conv2d('fc', global_pool, w=None, num_filters=self.args.num_classes,
kernel_size=(1, 1),
l2_strength=self.args.l2_strength,
bias=self.args.bias,
is_training=self.is_training)
self.logits = flatten(logits_unflattened)
self.__init_output()
示例4: build
# 需要導入模塊: import layers [as 別名]
# 或者: from layers import conv2d [as 別名]
def build(self):
self.net = {}
self.net['input'] = tf.transpose(self.x, perm=(0, 2, 3, 1))
if self.network_type == 'cnn':
self.net['conv1'] = conv2d(self.net['input'], 32, kernel=(8, 8), stride=(4, 4),
init_b=tf.constant_initializer(0.01), name='conv1')
self.net['conv2'] = conv2d(self.net['input'], 64, kernel=(4, 4), stride=(2, 2),
init_b=tf.constant_initializer(0.01), name='conv2')
self.net['conv3'] = conv2d(self.net['input'], 64, kernel=(3, 3), stride=(1, 1),
init_b=tf.constant_initializer(0.01), name='conv3')
self.net['feature'] = dense(self.net['conv2'], 512,
init_b=tf.constant_initializer(0.01), name='fc1')
elif self.network_type == 'cnn_nips':
self.net['conv1'] = conv2d(self.net['input'], 16, kernel=(8, 8), stride=(4, 4),
init_b=tf.constant_initializer(0.01), name='conv1')
self.net['conv2'] = conv2d(self.net['conv1'], 32, kernel=(4, 4), stride=(2, 2),
init_b=tf.constant_initializer(0.01), name='conv2')
self.net['feature'] = dense(self.net['conv2'], 256,
init_b=tf.constant_initializer(0.01), name='fc1')
elif self.network_type == 'mlp':
self.net['fc1'] = dense(self.net['input'], 50,
init_b=tf.constant_initializer(0.0), name='fc1')
self.net['feature'] = dense(self.net['fc1'], 50,
init_b=tf.constant_initializer(0.0), name='fc2')
else:
raise NotImplementedError('Unknown network type: {}'.format(self.network_type))
self.net['values'] = dense(self.net['feature'], self.n_outputs, activation=None,
init_b=tf.constant_initializer(0.0), name='values')
self.net['q_value'] = tf.reduce_max(self.net['values'], axis=1, name='q_value')
self.net['q_action'] = tf.argmax(self.net['values'], axis=1,
name='q_action', output_type=tf.int32)
self.vars = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
tf.get_variable_scope().name)
示例5: __call__
# 需要導入模塊: import layers [as 別名]
# 或者: from layers import conv2d [as 別名]
def __call__(self, x, reuse=False, output_name=None):
with tf.variable_scope(self.name) as scope:
if reuse:
scope.reuse_variables()
# Initial dense multiplication
x = layers.linear(x, "G_FC1", self.nb_filters * 8 * 8)
batch_size = tf.shape(x)[0]
if FLAGS.data_format == "NHWC":
target_shape = (batch_size, 8, 8, self.nb_filters)
elif FLAGS.data_format == "NCHW":
target_shape = (batch_size, self.nb_filters, 8, 8)
x = layers.reshape(x, target_shape)
# x = tf.contrib.layers.batch_norm(x, fused=True, data_format=FLAGS.data_format)
x = tf.nn.elu(x)
x = layers.dec_conv2d_block(x, "G_conv2D1", self.nb_filters, 3, data_format=FLAGS.data_format)
x = layers.upsampleNN(x, "G_up1", 2, data_format=FLAGS.data_format)
x = layers.dec_conv2d_block(x, "G_conv2D2", self.nb_filters, 3, data_format=FLAGS.data_format)
x = layers.upsampleNN(x, "G_up2", 2, data_format=FLAGS.data_format)
x = layers.dec_conv2d_block(x, "G_conv2D3", self.nb_filters, 3, data_format=FLAGS.data_format)
x = layers.upsampleNN(x, "G_up3", 2, data_format=FLAGS.data_format)
x = layers.dec_conv2d_block(x, "G_conv2D4", self.nb_filters, 3, data_format=FLAGS.data_format)
# Last conv
x = layers.conv2d(x, "G_conv2D5", self.nb_filters, FLAGS.channels, 3, 1, "SAME", data_format=FLAGS.data_format)
x = tf.nn.tanh(x, name=output_name)
return x
示例6: _build_encoder
# 需要導入模塊: import layers [as 別名]
# 或者: from layers import conv2d [as 別名]
def _build_encoder(self):
with tf.variable_scope(self.name):
if self.arch == 'FC':
layer_i = layers.flatten(self.input_ph)
for i, layer_size in enumerate(self.fc_layer_sizes):
layer_i = layers.fc('fc{}'.format(i+1), layer_i, layer_size, activation=self.activation)[-1]
self.ox = layer_i
elif self.arch == 'ATARI-TRPO':
self.w1, self.b1, self.o1 = layers.conv2d('conv1', self.input_ph, 16, 4, self.input_channels, 2, activation=self.activation)
self.w2, self.b2, self.o2 = layers.conv2d('conv2', self.o1, 16, 4, 16, 2, activation=self.activation)
self.w3, self.b3, self.o3 = layers.fc('fc3', layers.flatten(self.o2), 20, activation=self.activation)
self.ox = self.o3
elif self.arch == 'NIPS':
self.w1, self.b1, self.o1 = layers.conv2d('conv1', self.input_ph, 16, 8, self.input_channels, 4, activation=self.activation)
self.w2, self.b2, self.o2 = layers.conv2d('conv2', self.o1, 32, 4, 16, 2, activation=self.activation)
self.w3, self.b3, self.o3 = layers.fc('fc3', layers.flatten(self.o2), 256, activation=self.activation)
self.ox = self.o3
elif self.arch == 'NATURE':
self.w1, self.b1, self.o1 = layers.conv2d('conv1', self.input_ph, 32, 8, self.input_channels, 4, activation=self.activation)
self.w2, self.b2, self.o2 = layers.conv2d('conv2', self.o1, 64, 4, 32, 2, activation=self.activation)
self.w3, self.b3, self.o3 = layers.conv2d('conv3', self.o2, 64, 3, 64, 1, activation=self.activation)
self.w4, self.b4, self.o4 = layers.fc('fc4', layers.flatten(self.o3), 512, activation=self.activation)
self.ox = self.o4
else:
raise Exception('Invalid architecture `{}`'.format(self.arch))
if self.use_recurrent:
with tf.variable_scope('lstm_layer') as vs:
self.lstm_cell = tf.contrib.rnn.BasicLSTMCell(
self.hidden_state_size, state_is_tuple=True, forget_bias=1.0)
batch_size = tf.shape(self.step_size)[0]
self.ox_reshaped = tf.reshape(self.ox,
[batch_size, -1, self.ox.get_shape().as_list()[-1]])
state_tuple = tf.contrib.rnn.LSTMStateTuple(
*tf.split(self.initial_lstm_state, 2, 1))
self.lstm_outputs, self.lstm_state = tf.nn.dynamic_rnn(
self.lstm_cell,
self.ox_reshaped,
initial_state=state_tuple,
sequence_length=self.step_size,
time_major=False)
self.lstm_state = tf.concat(self.lstm_state, 1)
self.ox = tf.reshape(self.lstm_outputs, [-1,self.hidden_state_size], name='reshaped_lstm_outputs')
# Get all LSTM trainable params
self.lstm_trainable_variables = [v for v in
tf.trainable_variables() if v.name.startswith(vs.name)]
return self.ox