本文整理汇总了Python中tensorflow.get_variable_scope函数的典型用法代码示例。如果您正苦于以下问题:Python get_variable_scope函数的具体用法?Python get_variable_scope怎么用?Python get_variable_scope使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_variable_scope函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: sequence_rnn_pad
def sequence_rnn_pad(rnn_cell, input_dim, length=50, first_train=True):
state = rnn_cell.zero_state(1, tf.float32)
outputs = []
inputs = []
states = []
flags = []
for i in range(length):
if i > 0 or first_train == False:
tf.get_variable_scope().reuse_variables()
input = tf.placeholder('float', (1, input_dim))
inputs.append(input)
output_state = rnn_cell(input, state)
(output, state) = output_state
flag = tf.placeholder('float', (1, rnn_cell.state_size))
state = flag * state
flags.append(flag)
# flag = tf.placeholder(tf.types.float32)
# flags.append(flag)
# state = flag * state
states.append(state)
outputs.append(output)
# for i in range(length):
# flag = tf.Variable(0)
# flags.append(flag)
# states[i] = flag * states[i]
return inputs, outputs, states, flags
示例2: inference
def inference(x, n_batch, maxlen=None, n_hidden=None, n_out=None):
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial)
def bias_variable(shape):
initial = tf.zeros(shape, dtype=tf.float32)
return tf.Variable(initial)
cell = tf.contrib.rnn.GRUCell(n_hidden)
initial_state = cell.zero_state(n_batch, tf.float32)
state = initial_state
outputs = [] # 과거의 은닉층에서 나온 출력을 저장한다
with tf.variable_scope('GRU'):
for t in range(maxlen):
if t > 0:
tf.get_variable_scope().reuse_variables()
(cell_output, state) = cell(x[:, t, :], state)
outputs.append(cell_output)
output = outputs[-1]
V = weight_variable([n_hidden, n_out])
c = bias_variable([n_out])
y = tf.matmul(output, V) + c # 선형활성
return y
开发者ID:wooramkang,项目名称:deep-learning-with-tensorflow,代码行数:28,代码来源:05_adding_problem_gru_tensorflow.py
示例3: make_net
def make_net(self, input_images, input_measurements, input_actions, input_objectives, reuse=False):
if reuse:
tf.get_variable_scope().reuse_variables()
self.fc_val_params = np.copy(self.fc_joint_params)
self.fc_val_params['out_dims'][-1] = self.target_dim
self.fc_adv_params = np.copy(self.fc_joint_params)
self.fc_adv_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_val_fc = my_ops.fc_net(p_concat_fc, self.fc_val_params, 'p_val_fc', last_linear=True, msra_coeff=0.9)
p_adv_fc = my_ops.fc_net(p_concat_fc, self.fc_adv_params, 'p_adv_fc', last_linear=True, msra_coeff=0.9)
adv_reshape = tf.reshape(p_adv_fc, [-1, len(self.net_discrete_actions), self.target_dim])
pred_all_nomean = adv_reshape - tf.reduce_mean(adv_reshape, reduction_indices=1, keep_dims=True)
pred_all = pred_all_nomean + tf.reshape(p_val_fc, [-1, 1, self.target_dim])
pred_relevant = tf.boolean_mask(pred_all, tf.cast(input_actions, tf.bool))
return pred_all, pred_relevant
示例4: rnn_decoder
def rnn_decoder(decoder_inputs, initial_state, cell, scope=None):
"""RNN Decoder that creates training and sampling sub-graphs.
Args:
decoder_inputs: Inputs for decoder, list of tensors.
This is used only in trianing sub-graph.
initial_state: Initial state for the decoder.
cell: RNN cell to use for decoder.
scope: Scope to use, if None new will be produced.
Returns:
List of tensors for outputs and states for training and sampling sub-graphs.
"""
with tf.variable_scope(scope or "dnn_decoder"):
states, sampling_states = [initial_state], [initial_state]
outputs, sampling_outputs = [], []
with tf.op_scope([decoder_inputs, initial_state], "training"):
for i, inp in enumerate(decoder_inputs):
if i > 0:
tf.get_variable_scope().reuse_variables()
output, new_state = cell(inp, states[-1])
outputs.append(output)
states.append(new_state)
with tf.op_scope([initial_state], "sampling"):
for i, _ in enumerate(decoder_inputs):
if i == 0:
sampling_outputs.append(outputs[i])
sampling_states.append(states[i])
else:
sampling_output, sampling_state = cell(
sampling_outputs[-1], sampling_states[-1])
sampling_outputs.append(sampling_output)
sampling_states.append(sampling_state)
return outputs, states, sampling_outputs, sampling_states
示例5: testModelWithBucketsScopeAndLoss
def testModelWithBucketsScopeAndLoss(self):
"""Test that variable scope reuse is not reset after model_with_buckets."""
classes = 10
buckets = [(4, 4), (8, 8)]
with self.test_session():
# Here comes a sample Seq2Seq model using GRU cells.
def SampleGRUSeq2Seq(enc_inp, dec_inp, weights, per_example_loss):
"""Example sequence-to-sequence model that uses GRU cells."""
def GRUSeq2Seq(enc_inp, dec_inp):
cell = tf.nn.rnn_cell.MultiRNNCell([tf.nn.rnn_cell.GRUCell(24)] * 2)
return tf.nn.seq2seq.embedding_attention_seq2seq(
enc_inp, dec_inp, cell, num_encoder_symbols=classes,
num_decoder_symbols=classes, embedding_size=24)
targets = [dec_inp[i+1] for i in range(len(dec_inp) - 1)] + [0]
return tf.nn.seq2seq.model_with_buckets(
enc_inp, dec_inp, targets, weights, buckets, GRUSeq2Seq,
per_example_loss=per_example_loss)
# Now we construct the copy model.
inp = [tf.placeholder(tf.int32, shape=[None]) for _ in range(8)]
out = [tf.placeholder(tf.int32, shape=[None]) for _ in range(8)]
weights = [tf.ones_like(inp[0], dtype=tf.float32) for _ in range(8)]
with tf.variable_scope("root"):
_, losses1 = SampleGRUSeq2Seq(inp, out, weights, per_example_loss=False)
# Now check that we did not accidentally set reuse.
self.assertEqual(False, tf.get_variable_scope().reuse)
# Construct one more model with per-example loss.
tf.get_variable_scope().reuse_variables()
_, losses2 = SampleGRUSeq2Seq(inp, out, weights, per_example_loss=True)
# First loss is scalar, the second one is a 1-dimensinal tensor.
self.assertEqual([], losses1[0].get_shape().as_list())
self.assertEqual([None], losses2[0].get_shape().as_list())
示例6: discriminator_z
def discriminator_z(self, z, is_training=True, reuse_variables=False, num_hidden_layer_channels=(64, 32, 16), enable_bn=True):
if reuse_variables:
tf.get_variable_scope().reuse_variables()
current = z
for i in range(len(num_hidden_layer_channels)):
name = 'D_z_fc' + str(i)
current = fc(
input_vector=current,
num_output_length=num_hidden_layer_channels[i],
name=name
)
if enable_bn:
name = 'D_z_bn' + str(i)
current = tf.contrib.layers.batch_norm(
current,
scale=False,
is_training=is_training,
scope=name,
reuse=reuse_variables
)
current = tf.nn.relu(current)
name = 'D_z_fc' + str(i+1)
current = fc(
input_vector=current,
num_output_length=1,
name=name
)
return tf.nn.sigmoid(current), current
示例7: lstm_fn
def lstm_fn(height):
if height == FLAGS.num_lstm_layer-1:
return tf.contrib.rnn.BasicLSTMCell(FLAGS.lstm_unit, state_is_tuple=True,
reuse = tf.get_variable_scope().reuse)
else:
return tf.contrib.rnn.DropoutWrapper(tf.contrib.rnn.BasicLSTMCell(FLAGS.lstm_unit, state_is_tuple=True,
reuse = tf.get_variable_scope().reuse), output_keep_prob=0.5)
示例8: full_model
def full_model(data):
latent_mean, latent_log_std = encoder(data)
#latent_sample = lm_ae.reparam_normal_sample(latent_mean, latent_log_std, 'sample')
latent_sample = latent_mean
output_mean, output_log_std = decoder(latent_sample)
disc_neg_logit = discriminator(latent_sample)
tf.get_variable_scope().reuse_variables()
latent_prior_sample = tf.random_normal(tf.shape(latent_mean))
latent_prior_sample.set_shape(latent_mean.get_shape().as_list())
disc_pos_logit = discriminator(latent_prior_sample)
reconstruction_error = tf.reduce_sum(
-0.5 * numpy.log(2 * numpy.pi) - output_log_std - 0.5 * tf.square(output_mean - data) / tf.exp(
2.0 * output_log_std), reduction_indices=[1])
disc_cross_entropy = 0.5*tf.nn.sigmoid_cross_entropy_with_logits(disc_neg_logit, tf.zeros(tf.shape(disc_neg_logit))) \
+ 0.5*tf.nn.sigmoid_cross_entropy_with_logits(disc_pos_logit, tf.ones( tf.shape(disc_pos_logit)))
num_copies = 85
image = tf.reshape(
tf.tile(tf.expand_dims(tf.transpose(tf.pack([data, output_mean, data - output_mean]), perm=[1, 0, 2]), 2),
[1, 1, num_copies, 1]), [-1, 3 * num_copies, SIG_LEN])
lm_ae.summaries.image_summary('posterior_sample', tf.expand_dims(image, -1), 5)
rough_error = tf.reduce_mean(tf.square(tf.reduce_mean(tf.square(output_mean), reduction_indices=[1]) - tf.reduce_mean(tf.square(data), reduction_indices=[1])))
return output_mean, tf.reduce_mean(reconstruction_error), tf.reduce_mean(disc_cross_entropy), rough_error
示例9: update_target_network
def update_target_network(sess, network_name_train, network_name_target):
'''
This helper method copies all the trainable weights and biases from
one DeepQNetwork to another. This method is used for synchronisation
of the train and target Q-networks
'''
tf.get_variable_scope().reuse_variables()
vars_source = tf.get_collection(
tf.GraphKeys.TRAINABLE_VARIABLES, scope=network_name_train
)
copy_ops = []
check_ops = []
for v in vars_source:
# Note the [0:-2] to cut of the device placement
v_source = tf.get_variable(v.name[0:-2])
# Remove variable prefix (network name)
var_name = v.name[v.name.find("/"):]
v_target = tf.get_variable(network_name_target + var_name[0:-2])
# print("Copying variable:")
#print(" Source: " + v_source.name)
#print(" Target: " + v_target.name)
copy_ops.append(v_target.assign(v_source))
check_ops.append(tf.equal(v_target, v_source))
# Actual copying all the variables, check if the values are equal
sess.run(copy_ops)
check_res = sess.run(check_ops)
for res in check_res:
if not np.all(res):
raise ValueError("Verification of tf.equal(var_train, var_target) failed.")
示例10: build_train_graph
def build_train_graph(self):
init_c = tf.zeros([self.batch_size, self.lstm_cell.state_size[0]])
init_h = tf.zeros([self.batch_size, self.lstm_cell.state_size[1]])
initial_state = (init_c, init_h)
image_emb = tf.matmul(self.inp_dict["features"], self.image_embedding[
'weights']) + self.image_embedding['biases']
with tf.variable_scope("LSTM"):
output, state = self.lstm_cell(image_emb, initial_state)
loss = 0.0
for i in range(1, self.num_timesteps):
batch_embed = tf.nn.embedding_lookup(
self.word_embedding['weights'], self.inp_dict['captions'][
:, i - 1]) + self.word_embedding['biases']
tf.get_variable_scope().reuse_variables()
output, state = self.lstm_cell(batch_embed, state)
words = tf.reshape(self.inp_dict['captions'][
:, i], shape=[self.batch_size, 1])
onehot_encoded = tf.one_hot(indices=words, depth=len(
self.wtoidx), on_value=1, off_value=0, axis=-1)
onehot_encoded = tf.reshape(onehot_encoded, shape=[
self.batch_size, self.max_words])
target_logit = tf.matmul(
output, self.target_word['weights']) + self.target_word['biases']
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
logits=target_logit, labels=onehot_encoded)
cross_entropy = cross_entropy * self.inp_dict["mask"][:, i]
current_loss = tf.reduce_sum(cross_entropy)
loss = loss + current_loss
loss = loss / tf.reduce_sum(self.inp_dict["mask"][:, 1:])
# introducing L2 regularization in Loss/Cost Function
# self.beta=0
#l2_loss = self.beta * sum([tf.nn.l2_loss(tf_var) for tf_var in tf.trainable_variables() if not "Bias" in tf_var.name])
#loss = tf.reduce_mean(loss+l2_loss)
return loss, self.inp_dict
示例11: build_decode_graph
def build_decode_graph(self):
image_features = tf.placeholder(
tf.float32, [1, self.dim_imgft], name='Input_Features')
image_emb = tf.matmul(image_features, self.image_embedding[
'weights']) + self.image_embedding['biases']
init_c = tf.zeros([1, self.lstm_cell.state_size[0]])
init_h = tf.zeros([1, self.lstm_cell.state_size[1]])
initial_state = (init_c, init_h)
IDs = []
with tf.variable_scope("LSTM"):
output, state = self.lstm_cell(image_emb, initial_state)
pred_ID = tf.nn.embedding_lookup(
self.word_embedding['weights'], [
self.wtoidx["<S>"]]) + self.word_embedding['biases']
for i in range(self.num_timesteps):
tf.get_variable_scope().reuse_variables()
output, state = self.lstm_cell(pred_ID, state)
logits = tf.matmul(output, self.target_word[
"weights"]) + self.target_word["biases"]
predicted_next_idx = tf.argmax(logits, axis=1)
pred_ID = tf.nn.embedding_lookup(
self.word_embedding['weights'], predicted_next_idx)
pred_ID = pred_ID + self.word_embedding['biases']
predicted_next_idx = tf.cast(predicted_next_idx, tf.int32, name="word_"+str(i))
IDs.append(predicted_next_idx)
with open("model/Decoder/DecoderOutputs.txt", 'w') as f:
for name in IDs:
f.write(name.name.split(":0")[0] + "\n")
return image_features, IDs
示例12: generator
def generator(self, gen_x_dim = 30, gen_y_dim = 30, reuse = False):
if reuse:
tf.get_variable_scope().reuse_variables()
n_network = self.net_size_g
gen_n_points = gen_x_dim * gen_y_dim
z_scaled = tf.reshape(self.z, [self.batch_size, 1, self.z_dim]) * \
tf.ones([gen_n_points, 1], dtype=tf.float32) * self.scale
z_unroll = tf.reshape(z_scaled, [self.batch_size*gen_n_points, self.z_dim])
x_unroll = tf.reshape(self.x, [self.batch_size*gen_n_points, 1])
y_unroll = tf.reshape(self.y, [self.batch_size*gen_n_points, 1])
r_unroll = tf.reshape(self.r, [self.batch_size*gen_n_points, 1])
U = fully_connected(z_unroll, n_network, self.model_name+'_g_0_z') + \
fully_connected(x_unroll, n_network, self.model_name+'_g_0_x', with_bias = False) + \
fully_connected(y_unroll, n_network, self.model_name+'_g_0_y', with_bias = False) + \
fully_connected(r_unroll, n_network, self.model_name+'_g_0_r', with_bias = False)
H = tf.nn.relu(U)
for i in range(1, self.net_depth_g):
H = tf.nn.tanh(fully_connected(H, n_network, self.model_name+'_g_tanh_'+str(i)))
H = tf.nn.relu(fully_connected(H, n_network, self.model_name+'_g_relu_'+str(i)))
output = tf.nn.sigmoid(fully_connected(H, self.c_dim, self.model_name+'_g_'+str(self.net_depth_g)))
result = tf.reshape(output, [self.batch_size, gen_y_dim, gen_x_dim, self.c_dim])
return result
示例13: sampler
def sampler(self,images, y=None):
tf.get_variable_scope().reuse_variables()
if not self.y_dim:
h1 = conv2d(images,self.gf_dim*2,d_h=1,d_w=1, name='g_h1')
h1 = tf.nn.relu(self.g_bn1(h1,train=False))
h2 = conv2d(h1,self.gf_dim*4,d_h=1,d_w=1, name='g_h2')
h2 = tf.nn.relu(self.g_bn2(h2,train=False))
h3 = conv2d(h2,self.gf_dim*4,d_h=1,d_w=1, name='g_h3')
h3 = tf.nn.relu(self.g_bn3(h3,train=False))
h4 = conv2d(h3,self.gf_dim*2,d_h=1,d_w=1, name='g_h4')
h4 = tf.nn.relu(self.g_bn4(h4,train=False))
h5 = conv2d(h4,3, d_h=1,d_w=1, name='g_h5')
return tf.nn.tanh(h5)
else:
yb = tf.reshape(y, [None, 1, 1, self.y_dim])
z = tf.concat(1, [z, y])
h0 = tf.nn.relu(self.bn0(linear(z, self.gfc_dim, 'g_h0_lin')))
h0 = tf.concat(1, [h0, y])
h1 = tf.nn.relu(self.g_bn1(linear(z, self.gf_dim*2*7*7, 'g_h1_lin')))
h1 = tf.reshape(h1, [None, 7, 7, self.gf_dim * 2])
h1 = conv_cond_concat(h1, yb)
h2 = tf.nn.relu(self.bn2(deconv2d(h1, self.gf_dim, name='g_h2')))
h2 = conv_cond_concat(h2, yb)
return tf.nn.sigmoid(deconv2d(h2, self.c_dim, name='g_h3'))
示例14: _build_graph
def _build_graph(self, inputs, is_training):
state, action, reward, next_state, isOver = inputs
self.predict_value = self._get_DQN_prediction(state, is_training)
action_onehot = tf.one_hot(action, NUM_ACTIONS, 1.0, 0.0)
pred_action_value = tf.reduce_sum(self.predict_value * action_onehot, 1) #N,
max_pred_reward = tf.reduce_mean(tf.reduce_max(
self.predict_value, 1), name='predict_reward')
add_moving_summary(max_pred_reward)
with tf.variable_scope('target'):
targetQ_predict_value = self._get_DQN_prediction(next_state, False) # NxA
# DQN
#best_v = tf.reduce_max(targetQ_predict_value, 1) # N,
# Double-DQN
tf.get_variable_scope().reuse_variables()
next_predict_value = self._get_DQN_prediction(next_state, is_training)
self.greedy_choice = tf.argmax(next_predict_value, 1) # N,
predict_onehot = tf.one_hot(self.greedy_choice, NUM_ACTIONS, 1.0, 0.0)
best_v = tf.reduce_sum(targetQ_predict_value * predict_onehot, 1)
target = reward + (1.0 - tf.cast(isOver, tf.float32)) * GAMMA * tf.stop_gradient(best_v)
sqrcost = tf.square(target - pred_action_value)
abscost = tf.abs(target - pred_action_value) # robust error func
cost = tf.select(abscost < 1, sqrcost, abscost)
summary.add_param_summary([('conv.*/W', ['histogram', 'rms']),
('fc.*/W', ['histogram', 'rms']) ]) # monitor all W
self.cost = tf.reduce_mean(cost, name='cost')
示例15: build_decoder_rnn
def build_decoder_rnn(self, first_step):
"""
This function build a decoder
if first_step is true, the state is initialized by mean context
if first_step is not true, the states are placeholder, and should be assigned.
"""
with tf.variable_scope("rnnlm"):
flattened_ctx = tf.reshape(self.context, [self.batch_size, 196, 512])
ctx_mean = tf.reduce_mean(flattened_ctx, 1)
self.decoder_prev_word = tf.placeholder(tf.int32, [None])
if first_step:
rnn_input = tf.nn.embedding_lookup(self.Wemb, tf.zeros([self.batch_size], tf.int32))
else:
rnn_input = tf.nn.embedding_lookup(self.Wemb, self.decoder_prev_word)
tf.get_variable_scope().reuse_variables()
if not first_step:
initial_state = utils.get_placeholder_state(self.cell.state_size)
self.decoder_flattened_state = utils.flatten_state(initial_state)
else:
initial_state = utils.get_initial_state(ctx_mean, self.cell.state_size)
outputs, state = tf.contrib.legacy_seq2seq.attention_decoder([rnn_input], initial_state, flattened_ctx, self.cell, initial_state_attention = not first_step)
logits = slim.fully_connected(outputs[0], self.vocab_size + 1, activation_fn = None, scope = 'logit')
decoder_probs = tf.reshape(tf.nn.softmax(logits), [self.batch_size, self.vocab_size + 1])
decoder_state = utils.flatten_state(state)
# output the probability and flattened state to next time step
return [decoder_probs, decoder_state]