Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
with tf.variable_scope('pool3'):
pool3 = tf.nn.max_pool(
conv7, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
with tf.variable_scope('512'):
with tf.variable_scope('conv8'):
conv8 = tf.nn.relu(
utils.conv_layer(
pool3, [3, 3, 256, 512], 1, 'SAME', wd=l2_penalty))
if train_phase:
#conv8 = tf.nn.dropout(conv8, keep_prob)
conv8 = tf.nn.dropout(conv8, 1 - 0.4)
with tf.variable_scope('conv9'):
conv9 = tf.nn.relu(
utils.conv_layer(
conv8, [3, 3, 512, 512], 1, 'SAME', wd=l2_penalty))
if train_phase:
#conv9 = tf.nn.dropout(conv9, keep_prob)
conv9 = tf.nn.dropout(conv9, 1 - 0.4)
with tf.variable_scope('conv10'):
conv10 = tf.nn.relu(
utils.conv_layer(
conv9, [3, 3, 512, 512], 1, 'SAME', wd=l2_penalty))
with tf.variable_scope('pool4'):
pool4 = tf.nn.max_pool(
conv10, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='VALID')
embedded_full_input = tf.nn.embedding_lookup(self.embedding, self.full_input)
# recurrent operations
with tf.variable_scope("recurrent"):
self.cell = tf.nn.rnn_cell.BasicLSTMCell(hidden_size)
batch_size, max_time_step = tf.unstack(tf.shape(self.full_input))
outputs, _ = tf.nn.dynamic_rnn(self.cell,
embedded_full_input,
input_length,
dtype=tf.float32) # outputs: [batch, time, hidden], bw_outputs: [batch, time, hidden]
outputs = tf.reshape(outputs, (-1, hidden_size)) # output: [batch*time, hidden]
# output with rnn memories
with tf.variable_scope("output", reuse=tf.AUTO_REUSE):
self.W = tf.Variable(tf.truncated_normal(shape=(hidden_size, self.vocab_size)))
self.b = tf.Variable(tf.constant(0.1, shape=(self.vocab_size,)))
logits = tf.add(tf.matmul(outputs, self.W), self.b) # logits: [batch*time, vocab_size]
logits = tf.reshape(logits, (batch_size, max_time_step, -1)) # logits: [batch, time, vocab_size]
# loss calculation
with tf.variable_scope("loss"):
self.loss = tf.reduce_mean(tf.contrib.seq2seq.sequence_loss(logits=logits,
targets=self.target_input,
weights=tf.sequence_mask(target_length,
max_time_step,
dtype=tf.float32)))
# train with clipped gradient
with tf.variable_scope("train", reuse=tf.AUTO_REUSE):
global_step = tf.Variable(0, trainable=False)
num_anchors = feat_height * feat_width * num_predictions_per_location
class_predictions_with_background: A float tensor of shape
[batch_size, num_anchors, num_classes + 1] representing the class
predictions for the proposals.
"""
box_encodings_list = []
class_predictions_list = []
# TODO: Come up with a better way to generate scope names
# in box predictor once we have time to retrain all models in the zoo.
# The following lines create scope names to be backwards compatible with the
# existing checkpoints.
box_predictor_scopes = [_NoopVariableScope()]
if len(image_features) > 1:
box_predictor_scopes = [
tf.variable_scope('BoxPredictor_{}'.format(i))
for i in range(len(image_features))
]
for (image_feature,
num_predictions_per_location, box_predictor_scope) in zip(
image_features, num_predictions_per_location_list,
box_predictor_scopes):
with box_predictor_scope:
# Add a slot for the background class.
num_class_slots = self.num_classes + 1
net = image_feature
with slim.arg_scope(self._conv_hyperparams), \
slim.arg_scope([slim.dropout], is_training=self._is_training):
# Add additional conv layers before the class predictor.
features_depth = static_shape.get_depth(image_feature.get_shape())
depth = max(min(features_depth, self._max_depth), self._min_depth)
def _inject(self, model_vars, k, alpha):
""" Inject the required update ops with all trainable variables.
[Args]
model_vars: list of trainable tf.Variables. [list]
"""
with tf.variable_scope(self.name) as scope:
counter = tf.Variable(0, trainable=False, dtype=tf.int32, name='counter')
self.k = tf.constant(k, dtype=tf.int32, name='reset_step_k')
self.alpha = tf.constant(alpha, dtype=tf.float32, name='alpha')
self.counter = tf.cond(
tf.equal(counter, self.k),
lambda: tf.assign(counter, 0),
lambda: tf.assign(counter, counter+1))
with tf.variable_scope('weights') as scope:
for weight in model_vars:
slow_weight = tf.Variable(weight.eval(), trainable=False, dtype=tf.float32, name='slow_weight')
update_weight = tf.identity(slow_weight + (weight - slow_weight) * self.alpha, name='update_weight')
self.variables.append(slow_weight)
self.update_ops.append(update_weight)
self.slow_weights_op = tf.cond(
tf.equal(self.counter, self.k),
lambda: self._assign_list(self.variables, self.update_ops),
lambda: self._assign_list(self.variables, self.variables),
name='cond_update_slow_weight')
self.fast_weights_op = tf.cond(
tf.equal(self.counter, self.k),
lambda: self._assign_list(model_vars, self.slow_weights_op),
def __init__(self,a_size,scope,trainer, width, height):
with tf.variable_scope(scope):
#Input and visual encoding layers
self.state = tf.placeholder(shape=[None, height, width, 3], dtype=tf.float32)
#Added a conv layer as described here: https://github.com/awjuliani/Meta-RL/blob/master/A3C-Meta-Grid.ipynb
#parameters: inputs, num_outputs, activation_fn
self.cnn_first_layer = tf.layers.conv2d(self.state, 16, (8, 8), strides=(4, 4))
self.cnn_second_layer = tf.layers.conv2d(self.cnn_first_layer, 32, (4, 4), strides=(2, 2))
self.conv = tf.contrib.layers.fully_connected(slim.flatten(self.cnn_second_layer), 256, activation_fn=tf.nn.relu)
self.prev_rewards = tf.placeholder(shape=[None, 1], dtype=tf.float32)
self.prev_actions = tf.placeholder(shape=[None], dtype=tf.int32)
self.timestep = tf.placeholder(shape=[None, 1], dtype=tf.float32)
self.prev_actions_onehot = tf.one_hot(self.prev_actions,a_size, dtype=tf.float32)
#Recurrent network for temporal dependencies
lstm_cell = tf.nn.rnn_cell.LSTMCell(256, state_is_tuple=True)
wd=l2_penalty)
conv3 = direct_drop(conv3, 0.6)
with tf.variable_scope('conv4'):
conv4 = utils.conv_layer(
conv3, [3, 3, 128, 128],
1,
'SAME',
activation=tf.nn.relu,
train_phase=train_phase,
wd=l2_penalty)
conv4 = direct_drop(conv4, 0.6)
with tf.variable_scope('pool2'):
pool2 = tf.nn.max_pool(
conv4,
ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1],
padding='VALID')
with tf.variable_scope('256'):
with tf.variable_scope('conv5'):
conv5 = utils.conv_layer(
pool2, [3, 3, 128, 256],
1,
'SAME',
activation=tf.nn.relu,
train_phase=train_phase,
wd=l2_penalty)
def _call(self, decoder_inputs, encoder_outputs, decoder_self_attention_bias,
attention_bias, cache=None):
for n, layer in enumerate(self.layers):
self_attention_layer = layer[0]
enc_dec_attention_layer = layer[1]
feed_forward_network = layer[2]
# Run inputs through the sublayers.
layer_name = "layer_%d" % n
layer_cache = cache[layer_name] if cache is not None else None
with tf.variable_scope(layer_name):
with tf.variable_scope("self_attention"):
# TODO: Figure out why this is needed
# decoder_self_attention_bias = tf.cast(x=decoder_self_attention_bias,
# dtype=decoder_inputs.dtype)
decoder_inputs = self_attention_layer(
decoder_inputs, decoder_self_attention_bias, cache=layer_cache,
)
with tf.variable_scope("encdec_attention"):
decoder_inputs = enc_dec_attention_layer(
decoder_inputs, encoder_outputs, attention_bias,
)
with tf.variable_scope("ffn"):
decoder_inputs = feed_forward_network(decoder_inputs)
return self.output_normalization(decoder_inputs)
def add_word_embeddings_op(self):
assert self.config.use_word_embeddings or self.config.use_char_embeddings is True
word_embeddings, char_embeddings, analysis_embeddings = None, None, None
with tf.variable_scope("words"):
if self.config.use_word_embeddings is True:
if self.config.embeddings is None:
self.logger.info("WARNING: randomly initializing word vectors")
_word_embeddings = tf.get_variable(
name="_word_embeddings",
dtype=tf.float32,
shape=[self.config.nwords, self.config.dim_word])
else:
_word_embeddings = tf.Variable(
self.config.embeddings,
name="_word_embeddings",
dtype=tf.float32,
trainable=self.config.train_embeddings)
word_embeddings = tf.nn.embedding_lookup(_word_embeddings, self.word_ids, name="word_embeddings")
with tf.variable_scope("chars"):
def _build_posterior(self,inputsize,hiddendict,outputsize,scope='posterior',sigma_rho=None):
qW,qb = {},{}
qWmu,qbmu = {},{}
qWrho,qbrho = {},{}
with tf.variable_scope(scope):
layerdict = [inputsize] + hiddendict + [outputsize]
i = 0
for h1,h2 in zip(layerdict[:-1],layerdict[1:]):
with tf.variable_scope('qW{0}'.format(i)):
if sigma_rho is None:
sigma_rho = np.log(np.exp(0.017)-1.0)
#sigma_rho = -10
qWmu[i] = tf.Variable(tf.random_uniform([h1,h2],-np.sqrt(3/h1),np.sqrt(3/h1)),name='loc')
qWrho[i] = tf.Variable(tf.random_uniform([h1,h2],sigma_rho,sigma_rho),name='scale',trainable=True)
#qWmu[i] = tf.Variable(tf.random_normal([h1,h2],stddev=0.5/np.sqrt(h1+h2)),name='loc')
#qWrho[i] = tf.Variable(tf.random_normal([h1,h2],stddev=0.5/np.sqrt(h1)),name='scale')
#qWrho[i] = tf.Variable(tf.random_normal([h1,h2],mean=np.log(np.exp(0.5/np.sqrt(h1+h2))-1),stddev=0.0001),name='scale')
qW[i] = Normal(loc=qWmu[i],scale=tf.nn.softplus(qWrho[i]))
with tf.variable_scope('qb{0}'.format(i)):
qbmu[i] = tf.Variable(tf.random_uniform([h2],0,0),name='loc')
qbrho[i] = tf.Variable(tf.random_uniform([h2],sigma_rho,sigma_rho),name='scale',trainable=True)
#qbmu[i] = tf.Variable(tf.random_normal([h2],stddev=0.5/np.sqrt(h1+h2)),name='loc')
#qbrho[i] = tf.Variable(tf.random_normal([h2]),name='scale')
#qbrho[i] = tf.Variable(tf.random_normal([h2],mean=np.log(np.exp(0.5/np.sqrt(h1+h2))-1),stddev=0.0001),name='scale')
qb[i] = Normal(loc=qbmu[i],scale=tf.nn.softplus(qbrho[i]))
with tf.variable_scope("decoder_%d" % depth):
output = tf.concat([up, down], axis=3)
output = tf.nn.relu(output)
output = deconv(output, ngf[depth-1])
output = batchnorm(output)
if depth > 5:
output = tf.nn.dropout(output, keep_prob=0.5)
return output
with tf.variable_scope("encoder_1"): # [batch, 256, 256, in_channels] => [batch, 128, 128, ngf]
down = conv(generator_inputs, ngf[1], stride=2)
up = encoder_decoder(down, 2)
with tf.variable_scope("decoder_1"): # [batch, 128, 128, ngf * 2] => [batch, 256, 256, generator_outputs_channels]
output = tf.concat([up, down], axis=3)
output = tf.nn.relu(output)
output = deconv(output, generator_outputs_channels)
output = tf.tanh(output)
return output