def __init__(self, input_space, output_space, trainable=True):
if isinstance(input_space, Tuple) or isinstance(output_space, Tuple):
raise ValueError('For tuple action and observation spaces '
'consider implementing custom network architecture.')
self._input_ph = tf.placeholder('float32', shape=[None] + list(input_space.shape),
name='inputs')
net, end_points = make_dqn_body(self.input_ph, trainable)
end_points['fc1'] = layers.fully_connected(net, num_outputs=256, activation_fn=tf.nn.relu,
scope='fc1', trainable=trainable)
gaussian = tf.random_normal_initializer
v = layers.fully_connected(end_points['fc1'], num_outputs=1,
activation_fn=None,
weights_initializer=gaussian(0.0, 0.1),
biases_initializer=gaussian(0.05, 0.1),
scope='out_value',
trainable=trainable)
end_points['out_value'] = tf.squeeze(v)
header_endpoints = make_a3c_header(net, input_space, output_space, trainable)
end_points.update(header_endpoints)
self.end_points = end_points
self.output_policy = self.output
python类fully_connected()的实例源码
def __init__(self, input_space, output_space, layer_sizes=(512, 512, 512), trainable=True):
if isinstance(input_space, Tuple) or isinstance(output_space, Tuple):
raise ValueError('For tuple action and observation spaces '
'consider implementing custom network architecture.')
self._input_ph = tf.placeholder('float32', shape=[None] + list(input_space.shape),
name='inputs')
end_points = {}
net = layers.flatten(self._input_ph)
for i, units in enumerate(layer_sizes):
name = 'fc%d' % i
net = layers.fully_connected(net, num_outputs=units, activation_fn=tf.nn.relu,
trainable=trainable, scope=name)
end_points[name] = net
gaussian = tf.random_normal_initializer
v = layers.fully_connected(net, num_outputs=1,
activation_fn=None,
weights_initializer=gaussian(0.0, 0.1),
biases_initializer=gaussian(0.05, 0.1),
scope='out_value',
trainable=trainable)
end_points['out_value'] = tf.squeeze(v)
header_endpoints = make_a3c_header(net, input_space, output_space, trainable)
end_points.update(header_endpoints)
self.end_points = end_points
self.output_policy = self.output
def __init__(self, input_space, output_space, layer_sizes=(512, 512, 512),
output_activation=None, trainable=True):
if isinstance(input_space, Tuple) or isinstance(output_space, Tuple):
raise ValueError('For tuple action and observation spaces '
'consider implementing custom network architecture.')
end_points = {}
self._input_ph = tf.placeholder('float32', shape=[None] + list(input_space.shape),
name='inputs')
net = self._input_ph
for i, units in enumerate(layer_sizes):
name = 'fc%d' % i
net = layers.fully_connected(net, num_outputs=units, activation_fn=tf.nn.relu,
trainable=trainable, scope=name)
end_points[name] = net
end_points['out'] = layers.fully_connected(net, num_outputs=output_space.shape[0],
activation_fn=output_activation,
trainable=trainable, scope='out')
self.end_points = end_points
def __init__(self, input_space, output_space, layer_sizes=(512, 512), dueling_type='mean',
advantage_layers=(256,), value_layers=(256,), trainable=True):
if isinstance(input_space, Tuple) or isinstance(output_space, Tuple):
raise ValueError('For tuple action and observation spaces '
'consider implementing custom network architecture.')
self._input_ph = tf.placeholder('float32', shape=[None] + list(input_space.shape),
name='inputs')
end_points = {}
net = layers.flatten(self.input_ph)
for i, units in enumerate(layer_sizes):
name = 'fc%d' % i
net = layers.fully_connected(net, num_outputs=units, activation_fn=tf.nn.relu,
trainable=trainable, scope=name)
end_points[name] = net
net, dueling_endpoints = make_dueling_header(input_layer=net,
output_size=output_space.shape[0],
dueling_type=dueling_type,
advantage_layers=advantage_layers,
value_layers=value_layers,
trainable=trainable)
end_points.update(dueling_endpoints)
self._output = net
self.end_points = end_points
def __init__(self, input_space, output_space, trainable=True, use_nature=True):
if isinstance(input_space, Tuple) or isinstance(output_space, Tuple):
raise ValueError('For tuple action and observation spaces '
'consider implementing custom network architecture.')
self._input_ph = tf.placeholder('float32', shape=[None] + list(input_space.shape),
name='inputs')
if use_nature:
net, end_points = make_dqn_body_nature(self.input_ph, trainable)
else:
net, end_points = make_dqn_body(self.input_ph, trainable)
net = layers.fully_connected(net, num_outputs=512, activation_fn=tf.nn.relu,
scope='fc1', trainable=trainable)
end_points['fc1'] = net
end_points['out'] = layers.fully_connected(net,
num_outputs=output_space.shape[0],
activation_fn=None, scope='out',
trainable=trainable)
self.end_points = end_points
def __init__(self,
params,
device_assigner=None,
optimizer_class=adagrad.AdagradOptimizer,
**kwargs):
super(HardDecisionsToDataThenNN, self).__init__(
params,
device_assigner=device_assigner,
optimizer_class=optimizer_class,
**kwargs)
self.layers = [decisions_to_data.HardDecisionsToDataLayer(
params, 0, device_assigner),
fully_connected.FullyConnectedLayer(
params, 1, device_assigner=device_assigner)]
def inference_graph(self, data):
with ops.device(self.device_assigner.get_device(self.layer_num)):
# Compute activations for the neural network.
nn_activations = [layers.fully_connected(data, self.params.layer_size)]
for _ in range(1, self.params.num_layers):
# pylint: disable=W0106
nn_activations.append(
layers.fully_connected(
nn_activations[-1],
self.params.layer_size))
nn_activations_tensor = array_ops.concat(
1, nn_activations, name="flattened_nn_activations")
return nn_activations_tensor
def __init__(self,
params,
device_assigner=None,
optimizer_class=adagrad.AdagradOptimizer,
**kwargs):
super(HardDecisionsToDataThenNN, self).__init__(
params,
device_assigner=device_assigner,
optimizer_class=optimizer_class,
**kwargs)
self.layers = [decisions_to_data.HardDecisionsToDataLayer(
params, 0, device_assigner),
fully_connected.FullyConnectedLayer(
params, 1, device_assigner=device_assigner)]
def inference_graph(self, data):
with ops.device(self.device_assigner.get_device(self.layer_num)):
# Compute activations for the neural network.
nn_activations = [layers.fully_connected(data, self.params.layer_size)]
for _ in range(1, self.params.num_layers):
# pylint: disable=W0106
nn_activations.append(
layers.fully_connected(
nn_activations[-1],
self.params.layer_size))
nn_activations_tensor = array_ops.concat(
1, nn_activations, name="flattened_nn_activations")
return nn_activations_tensor
def discriminator_stego_nn(self, img, batch_size, name):
eve_input = self.image_processing_layer(img)
eve_conv1 = convolution2d(eve_input, 64, kernel_size = [5, 5], stride = [2,2],
activation_fn= tf.nn.relu, normalizer_fn = BatchNorm, scope = 'eve/' + name + '/conv1')
eve_conv2 = convolution2d(eve_conv1, 64 * 2, kernel_size = [5, 5], stride = [2,2],
activation_fn= tf.nn.relu, normalizer_fn = BatchNorm, scope = 'eve/' + name + '/conv2')
eve_conv3 = convolution2d(eve_conv2, 64 * 4,kernel_size = [5, 5], stride = [2,2],
activation_fn= tf.nn.relu, normalizer_fn = BatchNorm, scope = 'eve/' + name + '/conv3')
eve_conv4 = convolution2d(eve_conv3, 64* 8, kernel_size = [5, 5], stride = [2,2],
activation_fn= tf.nn.relu, normalizer_fn = BatchNorm, scope = 'eve/' + name + '/conv4')
eve_conv4 = tf.reshape(eve_conv4, [batch_size, -1])
#eve_fc = fully_connected(eve_conv4, 1, activation_fn = tf.nn.sigmoid, normalizer_fn = BatchNorm,
#weights_initializer=tf.random_normal_initializer(stddev=1.0))
eve_fc = fully_connected(eve_conv4, 1, normalizer_fn = BatchNorm,
weights_initializer=tf.random_normal_initializer(stddev=1.0), scope = 'eve' + name + '/final_fc')
return eve_fc
def discriminator_forward(img,
network_description,
is_training,
reuse=None,
name="discriminator",
use_batch_norm=True,
debug=False):
with tf.variable_scope(name, reuse=reuse):
out = run_network(img,
network_description,
is_training=is_training,
use_batch_norm=use_batch_norm,
debug=debug)
out = layers.flatten(out)
prob = layers.fully_connected(
out,
num_outputs=1,
activation_fn=tf.nn.sigmoid,
scope="prob_projection"
)
return {"prob":prob, "hidden":out}
def _critic(self, input_, name='critic', reuse=False):
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
co_0 = tf.nn.relu(conv2d(input_, o_dim=64, \
k_size=[3,1], st=[2,1], name='co_0'))
co_1 = tf.nn.relu(conv2d(co_0, o_dim=128, \
k_size=[3,2], st=[2,1], name='co_1'))
co_2 = tf.nn.relu(conv2d(co_1, o_dim=256, \
k_size=[3,2], st=[2,2], name='co_2'))
co_3 = tf.nn.relu(conv2d(co_2, o_dim=512, \
k_size=[3,2], st=[3,3], name='co_3'))
co_4 = tf.nn.relu(conv2d(co_3, o_dim=1024, \
k_size=[2,2], st=[3,3], name='co_4'))
co_5 = tf.nn.relu(conv2d(co_4, o_dim=self.o_size[-2]*self.o_size[-1],\
k_size=[3,1], st=[4,2], name='co_5'))
em_distance = ly.fully_connected(tf.reshape(co_5, [-1, np.prod(ten_sh(co_5)[1:])]), \
1, activation_fn=None, scope='em_dis')
return em_distance
def _critic(self, input_, name='critic', reuse=False):
with tf.variable_scope(name) as scope:
if reuse:
scope.reuse_variables()
co_0 = tf.nn.relu(conv2d(input_, o_dim=64, \
k_size=[3,1], st=[2,1], name='co_0'))
co_1 = tf.nn.relu(conv2d(co_0, o_dim=128, \
k_size=[3,2], st=[2,1], name='co_1'))
co_2 = tf.nn.relu(conv2d(co_1, o_dim=256, \
k_size=[3,2], st=[2,2], name='co_2'))
co_3 = tf.nn.relu(conv2d(co_2, o_dim=512, \
k_size=[3,2], st=[3,3], name='co_3'))
co_4 = tf.nn.relu(conv2d(co_3, o_dim=1024, \
k_size=[2,2], st=[3,3], name='co_4'))
co_5 = tf.nn.relu(conv2d(co_4, o_dim=self.o_size[-2]*self.o_size[-1],\
k_size=[3,1], st=[4,2], name='co_5'))
em_distance = ly.fully_connected(tf.reshape(co_5, [-1, np.prod(ten_sh(co_5)[1:])]), \
1, activation_fn=None, scope='em_dis')
return em_distance
def apply(self, is_train, context_embed, answer, context_mask=None):
init_fn = get_keras_initialization(self.init)
with tf.variable_scope("bounds_encoding"):
m1, m2 = self.predictor.apply(is_train, context_embed, context_mask)
with tf.variable_scope("start_pred"):
logits1 = fully_connected(m1, 1, activation_fn=None,
weights_initializer=init_fn)
logits1 = tf.squeeze(logits1, squeeze_dims=[2])
with tf.variable_scope("end_pred"):
logits2 = fully_connected(m2, 1, activation_fn=None, weights_initializer=init_fn)
logits2 = tf.squeeze(logits2, squeeze_dims=[2])
with tf.variable_scope("predict_span"):
return self.span_predictor.predict(answer, logits1, logits2, context_mask)
def apply(self, is_train, x, memories, answer: List[Tensor], x_mask=None, memory_mask=None):
with tf.variable_scope("map_context"):
memories = self.context_mapper.apply(is_train, memories, memory_mask)
with tf.variable_scope("encode_context"):
encoded = self.context_encoder.apply(is_train, memories, memory_mask)
with tf.variable_scope("merge"):
x = self.merge.apply(is_train, x, encoded, x_mask)
with tf.variable_scope("predict"):
m1, m2 = self.bounds_predictor.apply(is_train, x, x_mask)
init = get_keras_initialization(self.init)
with tf.variable_scope("logits1"):
l1 = fully_connected(m1, 1, activation_fn=None, weights_initializer=init)
l1 = tf.squeeze(l1, squeeze_dims=[2])
with tf.variable_scope("logits2"):
l2 = fully_connected(m2, 1, activation_fn=None, weights_initializer=init)
l2 = tf.squeeze(l2, squeeze_dims=[2])
with tf.variable_scope("predict_span"):
return self.span_predictor.predict(answer, l1, l2, x_mask)
def __call__(self, x, reuse=False):
with tf.variable_scope(self.name) as scope:
if reuse:
scope.reuse_variables()
size = 64
d = tcl.conv2d(x, num_outputs=size, kernel_size=3, # bzx64x64x3 -> bzx32x32x64
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.conv2d(d, num_outputs=size * 2, kernel_size=3, # 16x16x128
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.conv2d(d, num_outputs=size * 4, kernel_size=3, # 8x8x256
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.conv2d(d, num_outputs=size * 8, kernel_size=3, # 4x4x512
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.fully_connected(tcl.flatten(d), 256, activation_fn=lrelu, weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.fully_connected(d, 1, activation_fn=None, weights_initializer=tf.random_normal_initializer(0, 0.02))
return d
def __call__(self, x, reuse=False):
with tf.variable_scope(self.name) as scope:
if reuse:
scope.reuse_variables()
size = 64
d = tcl.conv2d(x, num_outputs=size, kernel_size=3, # bzx64x64x3 -> bzx32x32x64
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.conv2d(d, num_outputs=size * 2, kernel_size=3, # 16x16x128
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.conv2d(d, num_outputs=size * 4, kernel_size=3, # 8x8x256
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.conv2d(d, num_outputs=size * 8, kernel_size=3, # 4x4x512
stride=2, activation_fn=lrelu, normalizer_fn=tcl.batch_norm, padding='SAME', weights_initializer=tf.random_normal_initializer(0, 0.02))
d = tcl.fully_connected(tcl.flatten(d), 256, activation_fn=lrelu, weights_initializer=tf.random_normal_initializer(0, 0.02))
mu = tcl.fully_connected(d, 100, activation_fn=None, weights_initializer=tf.random_normal_initializer(0, 0.02))
sigma = tcl.fully_connected(d, 100, activation_fn=None, weights_initializer=tf.random_normal_initializer(0, 0.02))
return mu, sigma
def model(img_in, num_actions, scope, noisy=False, reuse=False):
"""As described in https://storage.googleapis.com/deepmind-data/assets/papers/DeepMindNature14236Paper.pdf"""
with tf.variable_scope(scope, reuse=reuse):
out = img_in
with tf.variable_scope("convnet"):
# original architecture
out = layers.convolution2d(out, num_outputs=32, kernel_size=8, stride=4, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=4, stride=2, activation_fn=tf.nn.relu)
out = layers.convolution2d(out, num_outputs=64, kernel_size=3, stride=1, activation_fn=tf.nn.relu)
out = layers.flatten(out)
with tf.variable_scope("action_value"):
if noisy:
# Apply noisy network on fully connected layers
# ref: https://arxiv.org/abs/1706.10295
out = noisy_dense(out, name='noisy_fc1', size=512, activation_fn=tf.nn.relu)
out = noisy_dense(out, name='noisy_fc2', size=num_actions)
else:
out = layers.fully_connected(out, num_outputs=512, activation_fn=tf.nn.relu)
out = layers.fully_connected(out, num_outputs=num_actions, activation_fn=None)
return out
def aux_logit_layer( inputs, num_classes, is_training ):
with tf.variable_scope("pool2d"):
pooled = layers.avg_pool2d(inputs, [ 5, 5 ], stride = 3 )
with tf.variable_scope("conv11"):
conv11 = layers.conv2d( pooled, 128, [1, 1] )
with tf.variable_scope("flatten"):
flat = tf.reshape( conv11, [-1, 2048] )
with tf.variable_scope("fc"):
fc = layers.fully_connected( flat, 1024, activation_fn=None )
with tf.variable_scope("drop"):
drop = layers.dropout( fc, 0.3, is_training = is_training )
with tf.variable_scope( "linear" ):
linear = layers.fully_connected( drop, num_classes, activation_fn=None )
with tf.variable_scope("soft"):
soft = tf.nn.softmax( linear )
return soft
def create_architecture(self, **specs):
self.vars.sequence_length = tf.placeholder(tf.int64, [1], name="sequence_length")
fc_input = self.get_input_layers()
fc1 = fully_connected(fc_input, num_outputs=self.fc_units_num,
scope=self._name_scope + "/fc1")
fc1_reshaped = tf.reshape(fc1, [1, -1, self.fc_units_num])
self.recurrent_cells = self.ru_class(self._recurrent_units_num)
state_c = tf.placeholder(tf.float32, [1, self.recurrent_cells.state_size.c], name="initial_lstm_state_c")
state_h = tf.placeholder(tf.float32, [1, self.recurrent_cells.state_size.h], name="initial_lstm_state_h")
self.vars.initial_network_state = LSTMStateTuple(state_c, state_h)
rnn_outputs, self.ops.network_state = tf.nn.dynamic_rnn(self.recurrent_cells,
fc1_reshaped,
initial_state=self.vars.initial_network_state,
sequence_length=self.vars.sequence_length,
time_major=False,
scope=self._name_scope)
reshaped_rnn_outputs = tf.reshape(rnn_outputs, [-1, self._recurrent_units_num])
self.reset_state()
self.ops.pi, self.ops.v = self.policy_value_layer(reshaped_rnn_outputs)