def create_model(self, model_input, num_classes=2, l2_penalty=1e-8, **unused_params):
"""Creates a logistic model.
Args:
model_input: 'batch' x 'num_features' matrix of input features.
vocab_size: The number of classes in the dataset.
Returns:
A dictionary with a tensor containing the probability predictions of the
model in the 'predictions' key. The dimensions of the tensor are
batch_size x num_classes."""
net = slim.flatten(model_input)
output = slim.fully_connected(
net, num_classes - 1, activation_fn=tf.nn.sigmoid,
weights_regularizer=slim.l2_regularizer(l2_penalty))
return {"predictions": output}
python类flatten()的实例源码
def create_model(self, model_input, num_classes=10, l2_penalty=1e-8, **unused_params):
"""Creates a logistic model.
Args:
model_input: 'batch' x 'num_features' matrix of input features.
num_classes: The number of classes in the dataset.
Returns:
A dictionary with a tensor containing the probability predictions of the
model in the 'predictions' key. The dimensions of the tensor are
batch_size x num_classes."""
net = slim.flatten(model_input)
output = slim.fully_connected(
net, num_classes, activation_fn=None,
weights_regularizer=slim.l2_regularizer(l2_penalty))
return {"predictions": output}
def encoder(self, images, is_training):
activation_fn = leaky_relu # tf.nn.relu
weight_decay = 0.0
with tf.variable_scope('encoder'):
with slim.arg_scope([slim.batch_norm],
is_training=is_training):
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
weights_regularizer=slim.l2_regularizer(weight_decay),
normalizer_fn=slim.batch_norm,
normalizer_params=self.batch_norm_params):
net = images
net = slim.conv2d(net, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1a')
net = slim.repeat(net, 3, conv2d_block, 0.1, 32, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_1b')
net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2a')
net = slim.repeat(net, 3, conv2d_block, 0.1, 64, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_2b')
net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3a')
net = slim.repeat(net, 3, conv2d_block, 0.1, 128, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_3b')
net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4a')
net = slim.repeat(net, 3, conv2d_block, 0.1, 256, [4, 4], 1, activation_fn=activation_fn, scope='Conv2d_4b')
net = slim.flatten(net)
fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1')
fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2')
return fc1, fc2
def encoder(self, images, is_training):
activation_fn = leaky_relu # tf.nn.relu
weight_decay = 0.0
with tf.variable_scope('encoder'):
with slim.arg_scope([slim.batch_norm],
is_training=is_training):
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
weights_regularizer=slim.l2_regularizer(weight_decay),
normalizer_fn=slim.batch_norm,
normalizer_params=self.batch_norm_params):
net = slim.conv2d(images, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1')
net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2')
net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3')
net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4')
net = slim.flatten(net)
fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1')
fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2')
return fc1, fc2
def encoder(self, images, is_training):
activation_fn = leaky_relu # tf.nn.relu
weight_decay = 0.0
with tf.variable_scope('encoder'):
with slim.arg_scope([slim.batch_norm],
is_training=is_training):
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
weights_regularizer=slim.l2_regularizer(weight_decay),
normalizer_fn=slim.batch_norm,
normalizer_params=self.batch_norm_params):
net = slim.conv2d(images, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1')
net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2')
net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3')
net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4')
net = slim.conv2d(net, 512, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_5')
net = slim.flatten(net)
fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1')
fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2')
return fc1, fc2
def __create_network(self, scope, img_shape=(80, 80)):
with tf.variable_scope(self.task_name):
with tf.variable_scope(scope):
with tf.variable_scope('input_data'):
self.inputs = tf.placeholder(shape=[None, *img_shape, cfg.HIST_LEN], dtype=tf.float32)
with tf.variable_scope('networks'):
with tf.variable_scope('conv_1'):
self.conv_1 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.inputs, num_outputs=32,
kernel_size=[8, 8], stride=4, padding='SAME', trainable=self.is_train)
with tf.variable_scope('conv_2'):
self.conv_2 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_1, num_outputs=64,
kernel_size=[4, 4], stride=2, padding='SAME', trainable=self.is_train)
with tf.variable_scope('conv_3'):
self.conv_3 = slim.conv2d(activation_fn=tf.nn.relu, inputs=self.conv_2, num_outputs=64,
kernel_size=[3, 3], stride=1, padding='SAME', trainable=self.is_train)
with tf.variable_scope('f_c'):
self.fc = slim.fully_connected(slim.flatten(self.conv_3), 512,
activation_fn=tf.nn.elu, trainable=self.is_train)
def build_model(self, inputs, learner_type=commons.LearnerType.Classifier):
_, endpoints = self.incep4_model.build_model(inputs)
# get feature output.
basenet_output = endpoints[self.incep4_model.net_params.output_layer_name]
if len(basenet_output.get_shape()) > 2:
basenet_output_flat = slim.flatten(
basenet_output, scope="baseoutput_flatten")
else:
basenet_output_flat = basenet_output
# add ft layer.
new_logits = slim.fully_connected(
basenet_output_flat,
self.net_params.cls_num,
activation_fn=None,
scope="ft/logits")
# monitor ft layer output.
base_model.add_tensor_summary(
new_logits.name, new_logits, use_histogram=True, use_sparsity=True)
return new_logits, endpoints
def create_network(self, input, trainable):
if trainable:
wr = slim.l2_regularizer(self.regularization)
else:
wr = None
# the input is stack of black and white frames.
# put the stack in the place of channel (last in tf)
input_t = tf.transpose(input, [0, 2, 3, 1])
net = slim.conv2d(input_t, 8, (7, 7), data_format="NHWC",
activation_fn=tf.nn.relu, stride=3, weights_regularizer=wr, trainable=trainable)
net = slim.max_pool2d(net, 2, 2)
net = slim.conv2d(net, 16, (3, 3), data_format="NHWC",
activation_fn=tf.nn.relu, weights_regularizer=wr, trainable=trainable)
net = slim.max_pool2d(net, 2, 2)
net = slim.flatten(net)
net = slim.fully_connected(net, 256, activation_fn=tf.nn.relu,
weights_regularizer=wr, trainable=trainable)
q_state_action_values = slim.fully_connected(net, self.dim_actions,
activation_fn=None, weights_regularizer=wr, trainable=trainable)
return q_state_action_values
def __call__(self, inputs, state, scope=None):
batch_size = tf.shape(inputs)[0]
if self._apply_to == 'input':
inputs = slim.flatten(inputs) if self._shape == -1 else tf.reshape(inputs, [batch_size] + self._shape)
return self._cell(inputs, state)
elif self._apply_to == 'output':
output, res_state = self._cell(inputs, state)
output = slim.flatten(output) if self._shape == -1 else tf.reshape(output, [batch_size] + self._shape)
return output, res_state
elif self._apply_to == 'state':
output, res_state = self._cell(inputs, state)
res_state = slim.flatten(res_state) if self._shape == -1 else tf.reshape(res_state, [batch_size] + self._shape)
return output, res_state
else:
raise ValueError('Unknown apply_to: "{}"'.format(self._apply_to))
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True):
with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d],
outputs_collections=[end_points_collection]):
net = slim.conv2d(inputs, 48, [5, 5], scope='conv1')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool1')
net = slim.conv2d(net, 64, [5, 5], scope='conv2')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool2')
net = slim.conv2d(net, 128, [5, 5], scope='conv3')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool3')
net = slim.conv2d(net, 160, [5, 5], scope='conv4')
net = slim.conv2d(net, 192, [5, 5], scope='conv5')
net = slim.conv2d(net, 192, [5, 5], scope='conv6')
net = slim.conv2d(net, 192, [5, 5], scope='conv7')
net = slim.flatten(net)
# By removing the fc layer, we'll get much smaller model with almost the same performance
# net = slim.fully_connected(net, 3072, scope='fc8')
return net, end_points_collection
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True):
with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d],
outputs_collections=[end_points_collection]):
net = slim.conv2d(inputs, 32, [5, 5], scope='conv1')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool1')
net = slim.conv2d(net, 64, [5, 5], scope='conv2')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool2')
net = slim.conv2d(net, 64, [5, 5], scope='conv3')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool3')
net = slim.conv2d(net, 64, [5, 5], scope='conv4')
net = slim.conv2d(net, 64, [5, 5], scope='conv5')
net = slim.conv2d(net, 64, [5, 5], scope='conv6')
net = slim.conv2d(net, 64, [5, 5], scope='conv7')
net = slim.flatten(net)
net = slim.fully_connected(net, 256, scope='fc3')
return net, end_points_collection
def cnn_layers(inputs, scope, end_points_collection, dropout_keep_prob=0.8, is_training=True):
with slim.arg_scope([slim.conv2d, slim.fully_connected, slim.max_pool2d],
outputs_collections=[end_points_collection]):
net = slim.conv2d(inputs, 32, [5, 5], scope='conv1')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool1')
net = slim.conv2d(net, 64, [5, 5], scope='conv2')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool2')
net = slim.conv2d(net, 64, [5, 5], scope='conv3')
net = slim.max_pool2d(net, [2, 2], 2, scope='pool3')
net = slim.conv2d(net, 64, [5, 5], scope='conv4')
net = slim.conv2d(net, 64, [5, 5], scope='conv5')
net = slim.conv2d(net, 64, [5, 5], scope='conv6')
net = slim.conv2d(net, 64, [5, 5], scope='conv7')
net = slim.flatten(net)
net = slim.fully_connected(net, 128, scope='fc3')
return net, end_points_collection
def encoder(self, images, is_training):
activation_fn = leaky_relu # tf.nn.relu
weight_decay = 0.0
with tf.variable_scope('encoder'):
with slim.arg_scope([slim.batch_norm],
is_training=is_training):
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
weights_regularizer=slim.l2_regularizer(weight_decay),
normalizer_fn=slim.batch_norm,
normalizer_params=self.batch_norm_params):
net = slim.conv2d(images, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1')
net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2')
net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3')
net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4')
net = slim.flatten(net)
fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1')
fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2')
return fc1, fc2
def encoder(self, images, is_training):
activation_fn = leaky_relu # tf.nn.relu
weight_decay = 0.0
with tf.variable_scope('encoder'):
with slim.arg_scope([slim.batch_norm],
is_training=is_training):
with slim.arg_scope([slim.conv2d, slim.fully_connected],
weights_initializer=tf.truncated_normal_initializer(stddev=0.1),
weights_regularizer=slim.l2_regularizer(weight_decay),
normalizer_fn=slim.batch_norm,
normalizer_params=self.batch_norm_params):
net = slim.conv2d(images, 32, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_1')
net = slim.conv2d(net, 64, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_2')
net = slim.conv2d(net, 128, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_3')
net = slim.conv2d(net, 256, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_4')
net = slim.conv2d(net, 512, [4, 4], 2, activation_fn=activation_fn, scope='Conv2d_5')
net = slim.flatten(net)
fc1 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_1')
fc2 = slim.fully_connected(net, self.latent_variable_dim, activation_fn=None, normalizer_fn=None, scope='Fc_2')
return fc1, fc2
def create_model(self, model_input, num_classes=10, l2_penalty=1e-8, **unused_params):
"""Creates a logistic model.
Args:
model_input: 'batch' x 'num_features' matrix of input features.
num_classes: The number of classes in the dataset.
Returns:
A dictionary with a tensor containing the probability predictions of the
model in the 'predictions' key. The dimensions of the tensor are
batch_size x num_classes."""
net = slim.flatten(model_input)
output = slim.fully_connected(
net, num_classes, activation_fn=None,
weights_regularizer=slim.l2_regularizer(l2_penalty))
return {"predictions": output}
def create_model(self, model_input, num_classes=2, l2_penalty=1e-8, **unused_params):
net = slim.conv2d(model_input, 64, [3, 3], scope='conv1_1')
# net = slim.conv2d(net, 64, [3, 3], scope='conv1_2')
net = slim.max_pool2d(net, [2, 2], scope='pool1')
# net = slim.conv2d(net, 128, [3, 3], scope='conv2_1')
# net = slim.conv2d(net, 128, [3, 3], scope='conv2_2')
# net = slim.max_pool2d(net, [2, 2], scope='pool2')
# net = slim.conv2d(net, 258, [3, 3], scope='conv3_1')
# net = slim.conv2d(net, 258, [3, 3], scope='conv3_2')
# net = slim.max_pool2d(net, [2, 2], scope='pool3')
net = slim.flatten(net)
output = slim.fully_connected(
net, num_classes - 1, activation_fn=tf.nn.sigmoid,
weights_regularizer=slim.l2_regularizer(l2_penalty))
return {"predictions": output}
def create_model(self, model_input, num_classes=2, l2_penalty=1e-8, **unused_params):
"""Creates a logistic model.
Args:
model_input: 'batch' x 'num_features' matrix of input features.
vocab_size: The number of classes in the dataset.
Returns:
A dictionary with a tensor containing the probability predictions of the
model in the 'predictions' key. The dimensions of the tensor are
batch_size x num_classes."""
net = slim.flatten(model_input)
output = slim.fully_connected(
net, num_classes - 1, activation_fn=tf.nn.sigmoid,
weights_regularizer=slim.l2_regularizer(l2_penalty))
return {"predictions": output}
def create_base(self, inputs, is_training):
params = self._config.cnn_params
print("input dimension = {}".format(inputs.get_shape()))
with tf.name_scope('Model'):
with slim.arg_scope([slim.conv2d, slim.fully_connected], activation_fn=tf.nn.relu,
# normalizer_fn=slim.batch_norm,
# normalizer_params={'is_training': is_training}
# weights_initializer=initializer = tf.contrib.layers.xavier_initializer(seed = 10)
):
# inputs is 2D with dimension (3 x feature_len)
net = slim.conv2d(inputs, params['num_filters'][0], [3,5], scope='conv1')
net = slim.conv2d(net, params['num_filters'][1], [3, 5], scope='conv2')
net = slim.conv2d(net, params['num_filters'][2], [3, 5], scope='conv3')
net = slim.flatten(net, scope='flatten1')
net = slim.fully_connected(net, params['num_fc_1'], scope='fc1')
net = slim.dropout(net, self._config.keep_prob, is_training=is_training, scope='dropout1')
logits = slim.fully_connected(net, self._config.num_classes, activation_fn=None, scope='fc2')
with tf.name_scope('output'):
predicted_classes = tf.to_int32(tf.argmax(logits, dimension=1), name='y')
return logits, predicted_classes
def discriminator_from_params(x, params):
with tf.variable_scope('Discriminator'):
c1 = conv2d(x, [5, 5], [1, 2, 2, 1], 16, scope='conv1', params=params[0:2])
c2 = conv2d(c1, [5, 5], [1, 2, 2, 1], 64, scope='conv2', params=params[2:4])
f0 = slim.flatten(c2)
f1 = dense(f0, 100, scope='dense1', params=params[4:6])
f2 = dense(f1, 10, scope='dense2', params=params[6:8])
return f2
# hid = dense(x, n_hid, scope='l1', params=params[:2], normalized=True)
# hid = tf.nn.relu(hid)
# #hid = tf.tanh(hid)
# hid = dense(hid, n_hid, scope='l2', params=params[2:4], normalized=True)
# hid = tf.nn.relu(hid)
# #hid = tf.tanh(hid)
# out = tf.nn.sigmoid(dense(hid, 1, scope='d_out', params=params[4:]))
# #
def generative_network(z, zdim):
"""Generative network to parameterize generative model. It takes
latent variables as input and outputs the likelihood parameters.
logits = neural_network(z)
Args:
z = tensor input
d = latent variable dimension
"""
with slim.arg_scope([slim.conv2d_transpose],
activation_fn=tf.nn.elu,
normalizer_fn=slim.batch_norm,
normalizer_params={'scale': True}):
net = tf.reshape(z, [N_MINIBATCH, 1, 1, zdim])
net = slim.conv2d_transpose(net, 128, 3, padding='VALID')
net = slim.conv2d_transpose(net, 64, 5, padding='VALID')
net = slim.conv2d_transpose(net, 32, 5, stride=2)
net = slim.conv2d_transpose(net, 1, 5, stride=2, activation_fn=None)
net = slim.flatten(net)
#net = slim.nn.sigmoid(net)
return net
def content_extractor(self, images, reuse=False):
# images: (batch, 32, 32, 3) or (batch, 32, 32, 1)
if images.get_shape()[3] == 1:
# For mnist dataset, replicate the gray scale image 3 times.
images = tf.image.grayscale_to_rgb(images)
with tf.variable_scope('content_extractor', reuse=reuse):
with slim.arg_scope([slim.conv2d], padding='SAME', activation_fn=None,
stride=2, weights_initializer=tf.contrib.layers.xavier_initializer()):
with slim.arg_scope([slim.batch_norm], decay=0.95, center=True, scale=True,
activation_fn=tf.nn.relu, is_training=(self.mode=='train' or self.mode=='pretrain')):
net = slim.conv2d(images, 64, [3, 3], scope='conv1') # (batch_size, 16, 16, 64)
net = slim.batch_norm(net, scope='bn1')
net = slim.conv2d(net, 128, [3, 3], scope='conv2') # (batch_size, 8, 8, 128)
net = slim.batch_norm(net, scope='bn2')
net = slim.conv2d(net, 256, [3, 3], scope='conv3') # (batch_size, 4, 4, 256)
net = slim.batch_norm(net, scope='bn3')
net = slim.conv2d(net, 128, [4, 4], padding='VALID', scope='conv4') # (batch_size, 1, 1, 128)
net = slim.batch_norm(net, activation_fn=tf.nn.tanh, scope='bn4')
if self.mode == 'pretrain':
net = slim.conv2d(net, 10, [1, 1], padding='VALID', scope='out')
net = slim.flatten(net)
return net
def discriminator(self, images, reuse=False):
# images: (batch, 32, 32, 1)
with tf.variable_scope('discriminator', reuse=reuse):
with slim.arg_scope([slim.conv2d], padding='SAME', activation_fn=None,
stride=2, weights_initializer=tf.contrib.layers.xavier_initializer()):
with slim.arg_scope([slim.batch_norm], decay=0.95, center=True, scale=True,
activation_fn=tf.nn.relu, is_training=(self.mode=='train')):
net = slim.conv2d(images, 128, [3, 3], activation_fn=tf.nn.relu, scope='conv1') # (batch_size, 16, 16, 128)
net = slim.batch_norm(net, scope='bn1')
net = slim.conv2d(net, 256, [3, 3], scope='conv2') # (batch_size, 8, 8, 256)
net = slim.batch_norm(net, scope='bn2')
net = slim.conv2d(net, 512, [3, 3], scope='conv3') # (batch_size, 4, 4, 512)
net = slim.batch_norm(net, scope='bn3')
net = slim.conv2d(net, 1, [4, 4], padding='VALID', scope='conv4') # (batch_size, 1, 1, 1)
net = slim.flatten(net)
return net
def __init__(self,
name,
hidden_layers,
input_dims,
output_dims):
# tf
self.sess = tf.get_default_session()
with tf.variable_scope(name):
self.obs = tf.placeholder(shape=[None, input_dims], dtype=tf.float32)
flat_input_state = slim.flatten(self.obs, scope='flat')
if hidden_layers == "":
self.logits = slim.fully_connected(
inputs=flat_input_state,
num_outputs=output_dims,
activation_fn=None,
weights_initializer=tf.zeros_initializer)
else:
final_hidden = self.hidden_layers_starting_at(flat_input_state, hidden_layers)
self.logits = slim.fully_connected(
inputs=final_hidden,
num_outputs=output_dims,
activation_fn=None)
def reparameterize(encoded, num_discrete, tau, hard=False,
rnd_sample=None, eps=1e-20):
eshp = encoded.get_shape().as_list()
print("encoded = ", eshp)
num_normal = eshp[1] - num_discrete
print 'num_normal = ', num_normal
logits_normal = encoded[:, 0:num_normal]
logits_gumbel = encoded[:, num_normal:eshp[1]]
# we reparameterize using both the N(0, I) and the gumbel(0, 1)
z_discrete, kl_discrete = gumbel_reparmeterization(logits_gumbel,
tau,
rnd_sample,
hard)
z_n, kl_n = gaussian_reparmeterization(logits_normal)
# merge and pad appropriately
z = tf.concat([z_n, z_discrete], axis=1)
return [slim.flatten(z),
slim.flatten(z_n),
slim.flatten(z_discrete),
slim.flatten(tf.nn.softmax(logits_gumbel)),
kl_n,
kl_discrete]
def __init__(self, nb_actions):
self.state = tf.placeholder(tf.float32, [None, 84, 84, 4])
cnn_1 = slim.conv2d(self.state, 16, [8,8], stride=4, scope='cnn_1', activation_fn=nn.relu)
cnn_2 = slim.conv2d(cnn_1, 32, [4,4], stride=2, scope='cnn_2', activation_fn=nn.relu)
flatten = slim.flatten(cnn_2)
fcc_1 = slim.fully_connected(flatten, 256, scope='fcc_1', activation_fn=nn.relu)
self.advantage = slim.fully_connected(fcc_1, nb_actions, scope='advantage', activation_fn=None)
self.value_state = slim.fully_connected(fcc_1, 1, scope='value_state', activation_fn=None)
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def _encoder(self, x, is_training=None):
net = self.arch['encoder']
for i, (o, k, s) in enumerate(zip(net['output'], net['kernel'], net['stride'])):
x = conv2d_nchw_layernorm(
x, o, k, s, lrelu,
name='Conv2d-{}'.format(i)
)
x = slim.flatten(x)
z_mu = tf.layers.dense(x, self.arch['z_dim'])
z_lv = tf.layers.dense(x, self.arch['z_dim'])
return z_mu, z_lv
def loss(self, x, y):
with tf.name_scope('loss'):
z_mu, z_lv = self._encode(x)
z = GaussianSampleLayer(z_mu, z_lv)
xh = self._generate(z, y)
D_KL = tf.reduce_mean(
GaussianKLD(
slim.flatten(z_mu),
slim.flatten(z_lv),
slim.flatten(tf.zeros_like(z_mu)),
slim.flatten(tf.zeros_like(z_lv)),
)
)
logPx = tf.reduce_mean(
GaussianLogDensity(
slim.flatten(x),
slim.flatten(xh),
tf.zeros_like(slim.flatten(xh))),
)
loss = dict()
loss['G'] = - logPx + D_KL
loss['D_KL'] = D_KL
loss['logP'] = logPx
tf.summary.scalar('KL-div', D_KL)
tf.summary.scalar('logPx', logPx)
tf.summary.histogram('xh', xh)
tf.summary.histogram('x', x)
return loss
def _head_to_tail(self, pool5, is_training, reuse=False):
with tf.variable_scope(self._scope, self._scope, reuse=reuse):
pool5_flat = slim.flatten(pool5, scope='flatten')
fc6 = slim.fully_connected(pool5_flat, 4096, scope='fc6')
if is_training:
fc6 = slim.dropout(fc6, keep_prob=0.5, is_training=True,
scope='dropout6')
fc7 = slim.fully_connected(fc6, 4096, scope='fc7')
if is_training:
fc7 = slim.dropout(fc7, keep_prob=0.5, is_training=True,
scope='dropout7')
return fc7
def forward(self):
temp = tf.transpose(
self.inp.out, [0,3,1,2])
self.out = slim.flatten(
temp, scope = self.scope)
def __init__(self, namespace, actor):
super(CriticNetwork, self).__init__(namespace)
# input state to the critic is the _same_ state given to the actor.
# input action to the critic is simply the output action of the actor.
# even though when training we explicitly provide a new value for the
# input action (via the input_action placeholder) we need to be stop the gradient
# flowing to the actor since there is a path through the actor to the input_state
# too, hence we need to be explicit about cutting it (otherwise training the
# critic will attempt to train the actor too.
self.input_state = actor.input_state
self.input_action = tf.stop_gradient(actor.output_action)
with tf.variable_scope(namespace):
if opts.use_raw_pixels:
conv_net = self.simple_conv_net_on(self.input_state, opts)
# TODO: use base_network helper
hidden1 = slim.fully_connected(conv_net, 200, scope='hidden1')
hidden2 = slim.fully_connected(hidden1, 50, scope='hidden2')
concat_inputs = tf.concat(1, [hidden2, self.input_action])
final_hidden = slim.fully_connected(concat_inputs, 50, scope="hidden3")
else:
# stack of hidden layers on flattened input; (batch,2,2,7) -> (batch,28)
flat_input_state = slim.flatten(self.input_state, scope='flat')
concat_inputs = tf.concat(1, [flat_input_state, self.input_action])
final_hidden = self.hidden_layers_starting_at(concat_inputs,
opts.critic_hidden_layers)
# output from critic is a single q-value
self.q_value = slim.fully_connected(scope='q_value',
inputs=final_hidden,
num_outputs=1,
weights_regularizer=tf.contrib.layers.l2_regularizer(0.01),
activation_fn=None)