def __init__(self, ob_space, ac_space, layers=[256], **kwargs):
self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space))
rank = len(ob_space)
if rank == 3: # pixel input
for i in range(4):
x = tf.nn.elu(conv2d(x, 32, "c{}".format(i + 1), [3, 3], [2, 2]))
elif rank == 1: # plain features
#x = tf.nn.elu(linear(x, 256, "l1", normalized_columns_initializer(0.01)))
pass
else:
raise TypeError("observation space must have rank 1 or 3, got %d" % rank)
x = flatten(x)
for i, layer in enumerate(layers):
x = tf.nn.elu(linear(x, layer, "l{}".format(i + 1), tf.contrib.layers.xavier_initializer()))
self.logits = linear(x, ac_space, "action", tf.contrib.layers.xavier_initializer())
self.vf = tf.reshape(linear(x, 1, "value", tf.contrib.layers.xavier_initializer()), [-1])
self.sample = categorical_sample(self.logits, ac_space)[0, :]
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
self.state_in = []
python类get_variable_scope()的实例源码
def __init__(self, ob_space, ac_space, size=256, **kwargs):
self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space))
for i in range(4):
x = tf.nn.elu(conv2d(x, 32, "l{}".format(i + 1), [3, 3], [2, 2]))
# introduce a "fake" batch dimension of 1 after flatten so that we can do GRU over time dim
x = tf.expand_dims(flatten(x), 1)
gru = rnn.GRUCell(size)
h_init = np.zeros((1, size), np.float32)
self.state_init = [h_init]
h_in = tf.placeholder(tf.float32, [1, size])
self.state_in = [h_in]
gru_outputs, gru_state = tf.nn.dynamic_rnn(
gru, x, initial_state=h_in, sequence_length=[size], time_major=True)
x = tf.reshape(gru_outputs, [-1, size])
self.logits = linear(x, ac_space, "action", normalized_columns_initializer(0.01))
self.vf = tf.reshape(linear(x, 1, "value", normalized_columns_initializer(1.0)), [-1])
self.state_out = [gru_state[:1]]
self.sample = categorical_sample(self.logits, ac_space)[0, :]
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def __init__(self, ob_space, ac_space, layers=[256], **kwargs):
self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space))
rank = len(ob_space)
if rank == 3: # pixel input
for i in range(4):
x = tf.nn.elu(conv2d(x, 32, "c{}".format(i + 1), [3, 3], [2, 2]))
elif rank == 1: # plain features
#x = tf.nn.elu(linear(x, 256, "l1", normalized_columns_initializer(0.01)))
pass
else:
raise TypeError("observation space must have rank 1 or 3, got %d" % rank)
x = flatten(x)
for i, layer in enumerate(layers):
x = tf.nn.elu(linear(x, layer, "l{}".format(i + 1), tf.contrib.layers.xavier_initializer()))
self.logits = linear(x, ac_space, "action", tf.contrib.layers.xavier_initializer())
self.vf = tf.reshape(linear(x, 1, "value", tf.contrib.layers.xavier_initializer()), [-1])
self.sample = categorical_sample(self.logits, ac_space)[0, :]
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
self.state_in = []
def __init__(self, ob_space, ac_space, size=256, **kwargs):
self.x = x = tf.placeholder(tf.float32, [None] + list(ob_space))
for i in range(4):
x = tf.nn.elu(conv2d(x, 32, "l{}".format(i + 1), [3, 3], [2, 2]))
# introduce a "fake" batch dimension of 1 after flatten so that we can do GRU over time dim
x = tf.expand_dims(flatten(x), 1)
gru = rnn.GRUCell(size)
h_init = np.zeros((1, size), np.float32)
self.state_init = [h_init]
h_in = tf.placeholder(tf.float32, [1, size])
self.state_in = [h_in]
gru_outputs, gru_state = tf.nn.dynamic_rnn(
gru, x, initial_state=h_in, sequence_length=[size], time_major=True)
x = tf.reshape(gru_outputs, [-1, size])
self.logits = linear(x, ac_space, "action", normalized_columns_initializer(0.01))
self.vf = tf.reshape(linear(x, 1, "value", normalized_columns_initializer(1.0)), [-1])
self.state_out = [gru_state[:1]]
self.sample = categorical_sample(self.logits, ac_space)[0, :]
self.var_list = tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES, tf.get_variable_scope().name)
def __call__(self, *args):
if args in self.cache:
print("(%s) retrieving value from cache"%self.name)
return self.cache[args]
with tf.variable_scope(self.name, reuse=not self.first_time):
scope = tf.get_variable_scope().name
if self.first_time:
self.scope = scope
print("(%s) running function for the first time"%self.name)
else:
assert self.scope == scope, "Tried calling function with a different scope"
print("(%s) running function on new inputs"%self.name)
self.first_time = False
out = self._call(*args)
self.cache[args] = out
return out
def _build(self, initial_state, helper):
if not self.initial_state:
self._setup(initial_state, helper)
scope = tf.get_variable_scope()
scope.set_initializer(tf.random_uniform_initializer(
-self.params["init_scale"],
self.params["init_scale"]))
maximum_iterations = None
if self.mode == tf.contrib.learn.ModeKeys.INFER:
maximum_iterations = self.params["max_decode_length"]
outputs, final_state = dynamic_decode(
decoder=self,
output_time_major=True,
impute_finished=False,
maximum_iterations=maximum_iterations)
return self.finalize(outputs, final_state)
def encode(self, inputs, sequence_length, **kwargs):
scope = tf.get_variable_scope()
scope.set_initializer(tf.random_uniform_initializer(
-self.params["init_scale"],
self.params["init_scale"]))
cell = training_utils.get_rnn_cell(**self.params["rnn_cell"])
outputs, state = tf.nn.dynamic_rnn(
cell=cell,
inputs=inputs,
sequence_length=sequence_length,
dtype=tf.float32,
**kwargs)
return EncoderOutput(
outputs=outputs,
final_state=state,
attention_values=outputs,
attention_values_length=sequence_length)
def encode(self, inputs, sequence_length, **kwargs):
scope = tf.get_variable_scope()
scope.set_initializer(tf.random_uniform_initializer(
-self.params["init_scale"],
self.params["init_scale"]))
cell_fw = training_utils.get_rnn_cell(**self.params["rnn_cell"])
cell_bw = training_utils.get_rnn_cell(**self.params["rnn_cell"])
outputs, states = tf.nn.bidirectional_dynamic_rnn(
cell_fw=cell_fw,
cell_bw=cell_bw,
inputs=inputs,
sequence_length=sequence_length,
dtype=tf.float32,
**kwargs)
# Concatenate outputs and states of the forward and backward RNNs
outputs_concat = tf.concat(outputs, 2)
return EncoderOutput(
outputs=outputs_concat,
final_state=states,
attention_values=outputs_concat,
attention_values_length=sequence_length)
def _variable_with_weight_decay(name, shape, stddev, wd):
"""Helper to create an initialized Variable with weight decay.
Note that the Variable is initialized with a truncated normal distribution.
A weight decay is added only if one is specified.
Args:
name: name of the variable
shape: list of ints
stddev: standard deviation of a truncated Gaussian
wd: add L2Loss weight decay multiplied by this float. If None, weight
decay is not added for this Variable.
Returns:
Variable Tensor
"""
dtype = tf.float16 if FLAGS.use_fp16 else tf.float32
var = _variable_on_cpu(
name,
shape,
tf.truncated_normal_initializer(stddev=stddev, dtype=dtype))
if wd is not None and not tf.get_variable_scope().reuse:
weight_decay = tf.multiply(tf.nn.l2_loss(var), wd, name='weight_loss')
tf.add_to_collection('losses', weight_decay)
return var
def __call__(self, inputs, reuse = True):
with tf.variable_scope(self.name) as vs:
tf.get_variable_scope()
if reuse:
vs.reuse_variables()
x1, down1 = down_block(self.block_fn, 64)(inputs)
x2, down2 = down_block(self.block_fn, 128)(down1)
x3, down3 = down_block(self.block_fn, 256)(down2)
down3 = self.block_fn(512)(down3)
up3 = up_block(self.block_fn, 256)(x3, down3)
up2 = up_block(self.block_fn, 128)(x2, up3)
up1 = up_block(self.block_fn, 64)(x1, up2)
outputs = tcl.conv2d(up1,
num_outputs = self.output_ch,
kernel_size = (1, 1),
stride = (1, 1),
padding = 'SAME')
return outputs
def build(self, inp):
# Divide input equally.
self.lazy_init_var()
inp_list = []
output = []
for ii in xrange(self.num_replica):
with tf.name_scope('%s_%d' % ('replica', ii)) as scope:
device = '/gpu:{}'.format(ii)
with tf.device(device):
tf.get_variable_scope().reuse_variables()
inp_ = {
'x': inp['x_{}'.format(ii)],
'y_gt': inp['y_gt_{}'.format(ii)],
'phase_train': inp['phase_train']
}
output.append(self.sub_models[ii].build(inp_))
inp_list.append(inp_)
self.output_list = output
self.input_list = inp_list
output = tf.concat(0, [oo['y_out'] for oo in output])
self.register_var('y_out', output)
output2 = tf.concat(0, [mm.get_var('score_out')
for mm in self.sub_models])
self.register_var('score_out', output2)
return {'y_out': output}
def encoder(self, images, is_training, reuse=False):
with tf.variable_scope("generator"):
if reuse:
tf.get_variable_scope().reuse_variables()
encode_layers = dict()
def encode_layer(x, output_filters, layer):
act = lrelu(x)
conv = conv2d(act, output_filters=output_filters, scope="g_e%d_conv" % layer)
enc = batch_norm(conv, is_training, scope="g_e%d_bn" % layer)
encode_layers["e%d" % layer] = enc
return enc
e1 = conv2d(images, self.generator_dim, scope="g_e1_conv")
encode_layers["e1"] = e1
e2 = encode_layer(e1, self.generator_dim * 2, 2)
e3 = encode_layer(e2, self.generator_dim * 4, 3)
e4 = encode_layer(e3, self.generator_dim * 8, 4)
e5 = encode_layer(e4, self.generator_dim * 8, 5)
e6 = encode_layer(e5, self.generator_dim * 8, 6)
e7 = encode_layer(e6, self.generator_dim * 8, 7)
e8 = encode_layer(e7, self.generator_dim * 8, 8)
return e8, encode_layers
def discriminator(self, image, is_training, reuse=False):
with tf.variable_scope("discriminator"):
if reuse:
tf.get_variable_scope().reuse_variables()
h0 = lrelu(conv2d(image, self.discriminator_dim, scope="d_h0_conv"))
h1 = lrelu(batch_norm(conv2d(h0, self.discriminator_dim * 2, scope="d_h1_conv"),
is_training, scope="d_bn_1"))
h2 = lrelu(batch_norm(conv2d(h1, self.discriminator_dim * 4, scope="d_h2_conv"),
is_training, scope="d_bn_2"))
h3 = lrelu(batch_norm(conv2d(h2, self.discriminator_dim * 8, sh=1, sw=1, scope="d_h3_conv"),
is_training, scope="d_bn_3"))
# real or fake binary loss
fc1 = fc(tf.reshape(h3, [self.batch_size, -1]), 1, scope="d_fc1")
# category loss
fc2 = fc(tf.reshape(h3, [self.batch_size, -1]), self.embedding_num, scope="d_fc2")
return tf.nn.sigmoid(fc1), fc1, fc2
def build(self):
"""Build the widget.
The main purpose of this function is to create the trainable variables (parameters) for the widget.
:return: None.
"""
if self._built:
return self
else:
if self._name is None:
#
# Build WITHOUT scope.
self._build()
self._built = True
return self
else:
#
# Build WITH scope.
self._scope = tf.get_variable_scope().name
with tf.variable_scope(self._name):
self._build()
self._built = True
return self
switchable_dropout_wrapper.py 文件源码
项目:paraphrase-id-tensorflow
作者: nelson-liu
项目源码
文件源码
阅读 32
收藏 0
点赞 0
评论 0
def __call__(self, inputs, state, scope=None):
# Get the dropped-out outputs and state
outputs_do, new_state_do = super(SwitchableDropoutWrapper,
self).__call__(
inputs, state, scope=scope)
tf.get_variable_scope().reuse_variables()
# Get the un-dropped-out outputs and state
outputs, new_state = self._cell(inputs, state, scope)
# Set the outputs and state to be the dropped out version if we are
# training, and no dropout if we are not training.
outputs = tf.cond(self.is_train, lambda: outputs_do,
lambda: outputs * (self._output_keep_prob))
if isinstance(state, tuple):
new_state = state.__class__(
*[tf.cond(self.is_train, lambda: new_state_do_i,
lambda: new_state_i)
for new_state_do_i, new_state_i in
zip(new_state_do, new_state)])
else:
new_state = tf.cond(self.is_train, lambda: new_state_do,
lambda: new_state)
return outputs, new_state
def decode(self, cell, init_state, loop_function=None):
outputs = []
prev = None
state = init_state
for i, inp in enumerate(self.decoder_inputs_emb):
if loop_function is not None and prev is not None:
with tf.variable_scope("loop_function", reuse=True):
inp = loop_function(prev, i)
if i > 0:
tf.get_variable_scope().reuse_variables()
output, state = cell(inp, state)
# print output.eval()
outputs.append(output)
if loop_function is not None:
prev = output
return outputs
def __make_net(self, input_images, input_measure, input_actions, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
fc_val_params = copy.deepcopy(self.__fc_joint_params)
fc_val_params[-1]['out_dims'] = self.__target_dim
fc_adv_params = copy.deepcopy(self.__fc_joint_params)
fc_adv_params[-1]['out_dims'] = len(self.__net_discrete_actions) * self.__target_dim
if self.verbose:
print 'fc_val_params:', fc_val_params
print 'fc_adv_params:', fc_adv_params
p_img_conv = ly.conv_encoder(input_images, self.__conv_params, 'p_img_conv', msra_coeff=0.9)
p_img_fc = ly.fc_net(ly.flatten(p_img_conv), self.__fc_img_params, 'p_img_fc', msra_coeff=0.9)
p_meas_fc = ly.fc_net(input_measure, self.__fc_measure_params, 'p_meas_fc', msra_coeff=0.9)
p_val_fc = ly.fc_net(tf.concat([p_img_fc, p_meas_fc], 1),
fc_val_params, 'p_val_fc', last_linear=True, msra_coeff=0.9)
p_adv_fc = ly.fc_net(tf.concat([p_img_fc, p_meas_fc], 1),
fc_adv_params, 'p_adv_fc', last_linear=True, msra_coeff=0.9)
p_adv_fc_nomean = p_adv_fc - tf.reduce_mean(p_adv_fc, reduction_indices=1, keep_dims=True)
self.__pred_all_nomean = tf.reshape(p_adv_fc_nomean, [-1, len(self.__net_discrete_actions), self.__target_dim])
self.__pred_all = self.__pred_all_nomean + tf.reshape(p_val_fc, [-1, 1, self.__target_dim])
self.__pred_relevant = tf.boolean_mask(self.__pred_all, tf.cast(input_actions, tf.bool))
def testTrainEvalWithReuse(self):
train_batch_size = 2
eval_batch_size = 1
train_height, train_width = 224, 224
eval_height, eval_width = 256, 256
num_classes = 1000
with self.test_session():
train_inputs = tf.random_uniform(
(train_batch_size, train_height, train_width, 3))
logits, _ = vgg.vgg_a(train_inputs)
self.assertListEqual(logits.get_shape().as_list(),
[train_batch_size, num_classes])
tf.get_variable_scope().reuse_variables()
eval_inputs = tf.random_uniform(
(eval_batch_size, eval_height, eval_width, 3))
logits, _ = vgg.vgg_a(eval_inputs, is_training=False,
spatial_squeeze=False)
self.assertListEqual(logits.get_shape().as_list(),
[eval_batch_size, 2, 2, num_classes])
logits = tf.reduce_mean(logits, [1, 2])
predictions = tf.argmax(logits, 1)
self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def testTrainEvalWithReuse(self):
train_batch_size = 2
eval_batch_size = 1
train_height, train_width = 224, 224
eval_height, eval_width = 256, 256
num_classes = 1000
with self.test_session():
train_inputs = tf.random_uniform(
(train_batch_size, train_height, train_width, 3))
logits, _ = vgg.vgg_16(train_inputs)
self.assertListEqual(logits.get_shape().as_list(),
[train_batch_size, num_classes])
tf.get_variable_scope().reuse_variables()
eval_inputs = tf.random_uniform(
(eval_batch_size, eval_height, eval_width, 3))
logits, _ = vgg.vgg_16(eval_inputs, is_training=False,
spatial_squeeze=False)
self.assertListEqual(logits.get_shape().as_list(),
[eval_batch_size, 2, 2, num_classes])
logits = tf.reduce_mean(logits, [1, 2])
predictions = tf.argmax(logits, 1)
self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def testTrainEvalWithReuse(self):
train_batch_size = 2
eval_batch_size = 1
train_height, train_width = 224, 224
eval_height, eval_width = 256, 256
num_classes = 1000
with self.test_session():
train_inputs = tf.random_uniform(
(train_batch_size, train_height, train_width, 3))
logits, _ = vgg.vgg_19(train_inputs)
self.assertListEqual(logits.get_shape().as_list(),
[train_batch_size, num_classes])
tf.get_variable_scope().reuse_variables()
eval_inputs = tf.random_uniform(
(eval_batch_size, eval_height, eval_width, 3))
logits, _ = vgg.vgg_19(eval_inputs, is_training=False,
spatial_squeeze=False)
self.assertListEqual(logits.get_shape().as_list(),
[eval_batch_size, 2, 2, num_classes])
logits = tf.reduce_mean(logits, [1, 2])
predictions = tf.argmax(logits, 1)
self.assertEquals(predictions.get_shape().as_list(), [eval_batch_size])
def build_lstm_inner(H, lstm_input):
'''
build lstm decoder
'''
lstm_cell = rnn_cell.BasicLSTMCell(H['lstm_size'], forget_bias=0.0, state_is_tuple=False)
if H['num_lstm_layers'] > 1:
lstm = rnn_cell.MultiRNNCell([lstm_cell] * H['num_lstm_layers'], state_is_tuple=False)
else:
lstm = lstm_cell
batch_size = H['batch_size'] * H['grid_height'] * H['grid_width']
state = tf.zeros([batch_size, lstm.state_size])
outputs = []
with tf.variable_scope('RNN', initializer=tf.random_uniform_initializer(-0.1, 0.1)):
for time_step in range(H['rnn_len']):
if time_step > 0: tf.get_variable_scope().reuse_variables()
output, state = lstm(lstm_input, state)
outputs.append(output)
return outputs
def get_conv_filter(self, params):
if params["name"]+"/weights" in self.modelDict:
init = tf.constant_initializer(value=self.modelDict[params["name"]+"/weights"], dtype=tf.float32)
var = tf.get_variable(name="weights", initializer=init, shape=params["shape"])
print "loaded " + params["name"]+"/weights"
else:
if params["std"]:
stddev = params["std"]
else:
fanIn = params["shape"][0]*params["shape"][1]*params["shape"][2]
stddev = (2/float(fanIn))**0.5
init = tf.truncated_normal(shape=params["shape"], stddev=stddev, seed=0)
var = tf.get_variable(name="weights", initializer=init)
print "generated " + params["name"] + "/weights"
if not tf.get_variable_scope().reuse:
weightDecay = tf.mul(tf.nn.l2_loss(var), self._wd,
name='weight_loss')
tf.add_to_collection('losses', weightDecay)
return var
def get_conv_filter(self, params):
if params["name"]+"/weights" in self.modelDict:
init = tf.constant_initializer(value=self.modelDict[params["name"]+"/weights"], dtype=tf.float32)
var = tf.get_variable(name="weights", initializer=init, shape=params["shape"])
print "loaded " + params["name"]+"/weights"
else:
if params["std"]:
stddev = params["std"]
else:
fanIn = params["shape"][0]*params["shape"][1]*params["shape"][2]
stddev = (2/float(fanIn))**0.5
init = tf.truncated_normal(shape=params["shape"], stddev=stddev, seed=0)
var = tf.get_variable(name="weights", initializer=init)
print "generated " + params["name"] + "/weights"
if not tf.get_variable_scope().reuse:
weightDecay = tf.mul(tf.nn.l2_loss(var), self._wd,
name='weight_loss')
tf.add_to_collection('losses', weightDecay)
return var
def __call__(self, shape, dtype=None, partition_info=None):
# Creating different RestoreV2 ops when a single one could
# output several tensors seems inefficient, but that's actually
# what tf.Saver.restore_op (via tf.BaseSaverBuilder) does too.
if self._scope is None:
scope_name = tf.get_variable_scope().name
elif callable(self._scope):
scope_name = self._scope(tf.get_variable_scope().name)
else:
scope_name = self._scope
tensor_name = self._var_name
if scope_name:
tensor_name = '{}/{}'.format(scope_name, tensor_name)
tensor = io_ops.restore_v2(
self._filename,
[tensor_name],
[self._partition_spec(shape, partition_info)],
[dtype])[0]
tensor.set_shape(shape)
return tensor
# pylint: disable=invalid-name
def _build(self, initial_state, helper):
if not self.initial_state:
self._setup(initial_state, helper)
scope = tf.get_variable_scope()
scope.set_initializer(tf.random_uniform_initializer(
-self.params["init_scale"],
self.params["init_scale"]))
maximum_iterations = None
if self.mode == tf.contrib.learn.ModeKeys.INFER:
maximum_iterations = self.params["max_decode_length"]
outputs, final_state = dynamic_decode(
decoder=self,
output_time_major=True,
impute_finished=False,
maximum_iterations=maximum_iterations)
return self.finalize(outputs, final_state)
def linear_mapping_stupid(inputs, out_dim, in_dim=None, dropout=1.0, var_scope_name="linear_mapping"):
with tf.variable_scope(var_scope_name):
print('name', tf.get_variable_scope().name)
input_shape_tensor = tf.shape(inputs) # dynamic shape, no None
input_shape = inputs.get_shape().as_list() # static shape. may has None
print('input_shape', input_shape)
assert len(input_shape) == 3
inputs = tf.reshape(inputs, [-1, input_shape_tensor[-1]])
linear_mapping_w = tf.get_variable("linear_mapping_w", [input_shape[-1], out_dim], initializer=tf.random_normal_initializer(mean=0, stddev=tf.sqrt(dropout*1.0/input_shape[-1])))
linear_mapping_b = tf.get_variable("linear_mapping_b", [out_dim], initializer=tf.zeros_initializer())
output = tf.matmul(inputs, linear_mapping_w) + linear_mapping_b
print('xxxxx_params', input_shape, out_dim)
#output = tf.reshape(output, [input_shape[0], -1, out_dim])
output = tf.reshape(output, [input_shape_tensor[0], -1, out_dim])
return output
def encode(self, inputs, sequence_length, **kwargs):
scope = tf.get_variable_scope()
scope.set_initializer(tf.random_uniform_initializer(
-self.params["init_scale"],
self.params["init_scale"]))
cell = training_utils.get_rnn_cell(**self.params["rnn_cell"])
outputs, state = tf.nn.dynamic_rnn(
cell=cell,
inputs=inputs,
sequence_length=sequence_length,
dtype=tf.float32,
**kwargs)
return EncoderOutput(
outputs=outputs,
final_state=state,
attention_values=outputs,
attention_values_length=sequence_length)
future_predictor_agent_basic.py 文件源码
项目:DirectFuturePrediction
作者: IntelVCL
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def make_net(self, input_images, input_measurements, input_actions, input_objectives, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
self.fc_joint_params['out_dims'][-1] = len(self.net_discrete_actions) * self.target_dim
p_img_conv = my_ops.conv_encoder(input_images, self.conv_params, 'p_img_conv', msra_coeff=0.9)
p_img_fc = my_ops.fc_net(my_ops.flatten(p_img_conv), self.fc_img_params, 'p_img_fc', msra_coeff=0.9)
p_meas_fc = my_ops.fc_net(input_measurements, self.fc_meas_params, 'p_meas_fc', msra_coeff=0.9)
if isinstance(self.fc_obj_params, np.ndarray):
p_obj_fc = my_ops.fc_net(input_objectives, self.fc_obj_params, 'p_obj_fc', msra_coeff=0.9)
p_concat_fc = tf.concat([p_img_fc,p_meas_fc,p_obj_fc], 1)
else:
p_concat_fc = tf.concat([p_img_fc,p_meas_fc], 1)
if self.random_objective_coeffs:
raise Exception('Need fc_obj_params with randomized objectives')
p_joint_fc = my_ops.fc_net(p_concat_fc, self.fc_joint_params, 'p_joint_fc', last_linear=True, msra_coeff=0.9)
pred_all = tf.reshape(p_joint_fc, [-1, len(self.net_discrete_actions), self.target_dim])
pred_relevant = tf.boolean_mask(pred_all, tf.cast(input_actions, tf.bool))
return pred_all, pred_relevant
def domain_classifier(self, images, name="G", reuse=False):
random_uniform_init = tf.random_uniform_initializer(minval=-0.1, maxval=0.1)
with tf.variable_scope(name):
tf.get_variable_scope().reuse_variables()
with tf.variable_scope("images"):
# "generator/images"
images_W = tf.get_variable("images_W", [self.img_dims, self.G_hidden_size], "float32", random_uniform_init)
images_emb = tf.matmul(images, images_W) # B,H
l2_loss = tf.constant(0.0)
with tf.variable_scope("domain"):
if reuse:
tf.get_variable_scope().reuse_variables()
with tf.variable_scope("output"):
output_W = tf.get_variable("output_W", [self.G_hidden_size, self.num_domains],
"float32", random_uniform_init)
output_b = tf.get_variable("output_b", [self.num_domains], "float32", random_uniform_init)
l2_loss += tf.nn.l2_loss(output_W)
l2_loss += tf.nn.l2_loss(output_b)
logits = tf.nn.xw_plus_b(images_emb, output_W, output_b, name="logits")
predictions = tf.argmax(logits, 1, name="predictions")
return predictions, logits, l2_loss
def build_graph(self, x, batch_size=1, n_units=256):
self.phs = [graph.Placeholder(np.float32, [batch_size, n_units]) for _ in range(2)]
self.ph_state = graph.TfNode(tuple(ph.node for ph in self.phs))
self.ph_state.checked = tuple(ph.checked for ph in self.phs)
self.zero_state = tuple(np.zeros([batch_size, n_units]) for _ in range(2))
state = tf.contrib.rnn.LSTMStateTuple(*self.ph_state.checked)
lstm = tf.contrib.rnn.BasicLSTMCell(n_units, state_is_tuple=True)
outputs, self.state = tf.nn.dynamic_rnn(lstm, x.node, initial_state=state,
sequence_length=tf.shape(x.node)[1:2], time_major=False)
self.state = graph.TfNode(self.state)
self.weight = graph.TfNode(tf.get_collection(tf.GraphKeys.TRAINABLE_VARIABLES,
tf.get_variable_scope().name))
return outputs