def conv_2d_drop_bn_relu(inp, inp_chan, out_chan, kernel, stride=1, prob=1.0, name="", is_train=True):
weights = tf.Variable(tf.truncated_normal(
shape=[kernel, kernel, inp_chan, out_chan],
mean=0.0,
stddev=0.3),
name=name+"_weights")
bias = tf.Variable(tf.constant(
shape=[out_chan],
value=0.0),
name=name+"_bias")
conv = tf.nn.conv2d(input=inp,
filter=weights,
strides=[1, stride, stride, 1],
padding='VALID',
name=name+"_conv")
drop = tf.nn.dropout(conv, prob, name=name+"_drop")
out = tf.nn.relu(tf.contrib.layers.batch_norm(drop + bias, is_training=is_train))
return out, weights, bias
python类Variable()的实例源码
def fc_drop_bn_relu(inp, inp_size, out_size, prob=1.0, name="", is_train=True):
weights = tf.Variable(tf.truncated_normal(
shape=[inp_size, out_size],
mean=0.0,
stddev=0.3),
name=name+"_weights")
bias = tf.Variable(tf.constant(
shape=[out_size],
value=0.0),
name=name+"_bias")
out = tf.nn.relu(
tf.contrib.layers.batch_norm(
tf.nn.dropout(
tf.matmul(inp, weights) + bias,
prob, name=name+"_drop"), is_training=is_train))
return out, weights, bias
def deconv_2d_drop_bn_relu(inp, inp_chan, out_chan, kernel, stride=1, prob=1.0, name="", is_train=True):
weights = tf.Variable(tf.truncated_normal(
shape=[kernel, kernel, out_chan, inp_chan],
mean=0.0,
stddev=0.3),
name=name+"_weights")
bias = tf.Variable(tf.constant(
shape=[out_chan],
value=0.0),
name=name+"_bias")
inp_shape = tf.shape(inp)
deconv = tf.nn.conv2d_transpose(
value=inp,
filter=weights,
output_shape=tf.stack([inp_shape[0], inp_shape[1]*stride, inp_shape[2]*stride, out_chan]),
strides=[1, stride, stride, 1],
padding='VALID',
name=name+"_deconv")
drop = tf.nn.dropout(deconv, prob, name=name+"_drop")
out = tf.nn.relu(tf.contrib.layers.batch_norm(drop + bias, is_training=is_train))
return out, weights, bias
def get_unique_variable(name):
"""Gets the variable uniquely identified by that name.
Args:
name: a name that uniquely identifies the variable.
Returns:
a tensorflow variable.
Raises:
ValueError: if no variable uniquely identified by the name exists.
"""
candidates = tf.get_collection(tf.GraphKeys.VARIABLES, name)
if not candidates:
raise ValueError('Couldnt find variable %s' % name)
for candidate in candidates:
if candidate.op.name == name:
return candidate
raise ValueError('Variable %s does not uniquely identify a variable', name)
def init_params(self, trainable=True, **kwargs):
i_shape, k_shape = self.shapes
# Compute effective number of neurons per filter. Ignores padding.
conv_out = i_shape[0] * i_shape[1]
if hasattr(self, 'pool_side'): conv_out /= self.pool_side**2
elif hasattr(self, 'pool_width'): conv_out /= self.pool_width
self.params['W'] = xavier_init(self.n_visible, self.n_hidden * conv_out,
shape=k_shape + [self.n_hidden],
name='W', trainable=trainable, dtype=self.dtype)
self.params['bhid'] = tf.Variable(tf.zeros(self.n_hidden, dtype=self.dtype),
name='bhid', trainable=trainable)
self.params['bvis'] = tf.Variable(tf.zeros(i_shape, dtype=self.dtype),
name='bvis', trainable=trainable)
def create_critic_net(self, num_states=4, num_actions=1):
N_HIDDEN_1 = 400
N_HIDDEN_2 = 300
critic_state_in = tf.placeholder("float",[None,num_states])
critic_action_in = tf.placeholder("float",[None,num_actions])
W1_c = tf.Variable(tf.random_uniform([num_states,N_HIDDEN_1],-1/math.sqrt(num_states),1/math.sqrt(num_states)))
B1_c = tf.Variable(tf.random_uniform([N_HIDDEN_1],-1/math.sqrt(num_states),1/math.sqrt(num_states)))
W2_c = tf.Variable(tf.random_uniform([N_HIDDEN_1,N_HIDDEN_2],-1/math.sqrt(N_HIDDEN_1+num_actions),1/math.sqrt(N_HIDDEN_1+num_actions)))
W2_action_c = tf.Variable(tf.random_uniform([num_actions,N_HIDDEN_2],-1/math.sqrt(N_HIDDEN_1+num_actions),1/math.sqrt(N_HIDDEN_1+num_actions)))
B2_c= tf.Variable(tf.random_uniform([N_HIDDEN_2],-1/math.sqrt(N_HIDDEN_1+num_actions),1/math.sqrt(N_HIDDEN_1+num_actions)))
W3_c= tf.Variable(tf.random_uniform([N_HIDDEN_2,1],-0.003,0.003))
B3_c= tf.Variable(tf.random_uniform([1],-0.003,0.003))
H1_c=tf.nn.softplus(tf.matmul(critic_state_in,W1_c)+B1_c)
H2_c=tf.nn.tanh(tf.matmul(H1_c,W2_c)+tf.matmul(critic_action_in,W2_action_c)+B2_c)
critic_q_model=tf.matmul(H2_c,W3_c)+B3_c
return W1_c, B1_c, W2_c, W2_action_c, B2_c, W3_c, B3_c, critic_q_model, critic_state_in, critic_action_in
def create_actor_net(self, num_states=4, num_actions=1):
""" Network that takes states and return action """
N_HIDDEN_1 = 400
N_HIDDEN_2 = 300
actor_state_in = tf.placeholder("float",[None,num_states])
W1_a=tf.Variable(tf.random_uniform([num_states,N_HIDDEN_1],-1/math.sqrt(num_states),1/math.sqrt(num_states)))
B1_a=tf.Variable(tf.random_uniform([N_HIDDEN_1],-1/math.sqrt(num_states),1/math.sqrt(num_states)))
W2_a=tf.Variable(tf.random_uniform([N_HIDDEN_1,N_HIDDEN_2],-1/math.sqrt(N_HIDDEN_1),1/math.sqrt(N_HIDDEN_1)))
B2_a=tf.Variable(tf.random_uniform([N_HIDDEN_2],-1/math.sqrt(N_HIDDEN_1),1/math.sqrt(N_HIDDEN_1)))
W3_a=tf.Variable(tf.random_uniform([N_HIDDEN_2,num_actions],-0.003,0.003))
B3_a=tf.Variable(tf.random_uniform([num_actions],-0.003,0.003))
H1_a=tf.nn.softplus(tf.matmul(actor_state_in,W1_a)+B1_a)
H2_a=tf.nn.tanh(tf.matmul(H1_a,W2_a)+B2_a)
actor_model=tf.matmul(H2_a,W3_a) + B3_a
return W1_a, B1_a, W2_a, B2_a, W3_a, B3_a, actor_state_in, actor_model
def build_loss_grad(self, inp, output):
y_gt = inp['y_gt']
y_out = output['y_out']
ce = tfplus.nn.CE()({'y_gt': y_gt, 'y_out': y_out})
num_ex_f = tf.to_float(tf.shape(inp['x'])[0])
ce = tf.reduce_sum(ce) / num_ex_f
self.add_loss(ce)
learn_rate = self.get_option('learn_rate')
total_loss = self.get_loss()
self.register_var('loss', total_loss)
eps = self.get_option('adam_eps')
optimizer = tf.train.AdamOptimizer(learn_rate, epsilon=eps)
global_step = tf.Variable(0.0)
self.register_var('step', global_step)
train_step = optimizer.minimize(
total_loss, global_step=global_step)
self.register_var('train_step', train_step)
correct = tf.equal(tf.argmax(y_gt, 1), tf.argmax(y_out, 1))
acc = tf.reduce_sum(tf.to_float(correct)) / num_ex_f
self.register_var('acc', acc)
pass
def _generate_labels(self, overlaps):
labels = tf.Variable(tf.ones(shape=(tf.shape(overlaps)[0],), dtype=tf.float32) * -1, trainable=False,
validate_shape=False)
gt_max_overlaps = tf.arg_max(overlaps, dimension=0)
anchor_max_overlaps = tf.arg_max(overlaps, dimension=1)
mask = tf.one_hot(anchor_max_overlaps, tf.shape(overlaps)[1], on_value=True, off_value=False)
max_overlaps = tf.boolean_mask(overlaps, mask)
if self._debug:
max_overlaps = tf.Print(max_overlaps, [max_overlaps])
labels = tf.scatter_update(labels, gt_max_overlaps, tf.ones((tf.shape(gt_max_overlaps)[0],)))
# TODO: extract config object
over_threshold_mask = tf.reshape(tf.where(max_overlaps > 0.5), (-1,))
if self._debug:
over_threshold_mask = tf.Print(over_threshold_mask, [over_threshold_mask], message='over threshold index : ')
labels = tf.scatter_update(labels, over_threshold_mask, tf.ones((tf.shape(over_threshold_mask)[0],)))
# TODO: support clobber positive in the origin implement
below_threshold_mask = tf.reshape(tf.where(max_overlaps < 0.3), (-1,))
if self._debug:
below_threshold_mask = tf.Print(below_threshold_mask, [below_threshold_mask], message='below threshold index : ')
labels = tf.scatter_update(labels, below_threshold_mask, tf.zeros((tf.shape(below_threshold_mask)[0],)))
return labels
def make_moving_average(name, value, init, decay, log=True):
"""Creates an exp-moving average of `value` and an update op, which is added to UPDATE_OPS collection.
:param name: string, name of the created moving average tf.Variable
:param value: tf.Tensor, the value to be averaged
:param init: float, an initial value for the moving average
:param decay: float between 0 and 1, exponential decay of the moving average
:param log: bool, add a summary op if True
:return: tf.Tensor, the moving average
"""
var = tf.get_variable(name, shape=value.get_shape(),
initializer=tf.constant_initializer(init), trainable=False)
update = moving_averages.assign_moving_average(var, value, decay, zero_debias=False)
tf.add_to_collection(tf.GraphKeys.UPDATE_OPS, update)
if log:
tf.summary.scalar(name, var)
return var
def weight_variable(shape):
return tf.Variable(xavier_initializer(shape))
def bias_variable(shape):
return tf.Variable(xavier_initializer(shape))
# Xavier Weights initializer
def weight_variable(shape):
return tf.Variable(xavier_initializer(shape))
def bias_variable(shape):
return tf.Variable(xavier_initializer(shape))
# Xavier Weights initializer
def weight_variable(shape):
return tf.Variable(xavier_initializer(shape))
def weight_variable(shape):
return tf.Variable(xavier_initializer(shape))
def bias_variable(shape):
return tf.Variable(xavier_initializer(shape))
# Xavier Weights initializer
def weight_variable(shape):
return tf.Variable(xavier_initializer(shape))
def bias_variable(shape):
return tf.Variable(xavier_initializer(shape))
# Xavier Weights initializer
def __init__(self, sess, reader, dataset="ptb",
decay_rate=0.96, decay_step=10000, embed_dim=500,
h_dim=50, learning_rate=0.001, max_iter=450000,
checkpoint_dir="checkpoint"):
"""Initialize Neural Varational Document Model.
params:
sess: TensorFlow Session object.
reader: TextReader object for training and test.
dataset: The name of dataset to use.
h_dim: The dimension of document representations (h). [50, 200]
"""
self.sess = sess
self.reader = reader
self.h_dim = h_dim
self.embed_dim = embed_dim
self.max_iter = max_iter
self.decay_rate = decay_rate
self.decay_step = decay_step
self.checkpoint_dir = checkpoint_dir
self.step = tf.Variable(0, trainable=False)
self.lr = tf.train.exponential_decay(
learning_rate, self.step, 10000, decay_rate, staircase=True, name="lr")
_ = tf.scalar_summary("learning rate", self.lr)
self.dataset = dataset
self._attrs = ["h_dim", "embed_dim", "max_iter", "dataset",
"learning_rate", "decay_rate", "decay_step"]
self.build_model()