def conv3d(input_, output_dim, f_size, is_training, scope='conv3d'):
with tf.variable_scope(scope) as scope:
# VGG network uses two 3*3 conv layers to effectively increase receptive field
w1 = tf.get_variable('w1', [f_size, f_size, f_size, input_.get_shape()[-1], output_dim],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv1 = tf.nn.conv3d(input_, w1, strides=[1, 1, 1, 1, 1], padding='SAME')
b1 = tf.get_variable('b1', [output_dim], initializer=tf.constant_initializer(0.0))
conv1 = tf.nn.bias_add(conv1, b1)
bn1 = tf.contrib.layers.batch_norm(conv1, is_training=is_training, scope='bn1', decay=0.9,
zero_debias_moving_mean=True, variables_collections=['bn_collections'])
r1 = tf.nn.relu(bn1)
w2 = tf.get_variable('w2', [f_size, f_size, f_size, output_dim, output_dim],
initializer=tf.truncated_normal_initializer(stddev=0.1))
conv2 = tf.nn.conv3d(r1, w2, strides=[1, 1, 1, 1, 1], padding='SAME')
b2 = tf.get_variable('b2', [output_dim], initializer=tf.constant_initializer(0.0))
conv2 = tf.nn.bias_add(conv2, b2)
bn2 = tf.contrib.layers.batch_norm(conv2, is_training=is_training, scope='bn2', decay=0.9,
zero_debias_moving_mean=True, variables_collections=['bn_collections'])
r2 = tf.nn.relu(bn2)
return r2
python类constant_initializer()的实例源码
def _create_variables(self):
if self.input_type.dtype != 'float32':
raise TypeError('FC input dtype must be float32: %s' %
self.input_type.dtype)
if self.input_type.ndim != 1:
raise TypeError('FC input shape must be 1D: %s' %
str(self.input_type.shape))
self._bias = tf.get_variable(
'bias', self.output_type.shape, initializer=tf.constant_initializer(0))
self._weights = tf.get_variable(
'weights', [self.input_type.shape[0], self.output_type.shape[0]],
initializer=self._initializer)
if self._weight_norm:
self._scales = tf.get_variable(
'scales',
[self.output_type.shape[0]],
initializer=tf.constant_initializer(1.0))
def batch_norm(x, n_out, phase_train, scope='bn', decay=0.9, eps=1e-5, stddev=0.02):
"""
Code taken from http://stackoverflow.com/a/34634291/2267819
"""
with tf.variable_scope(scope):
beta = tf.get_variable(name='beta', shape=[n_out], initializer=tf.constant_initializer(0.0)
, trainable=True)
gamma = tf.get_variable(name='gamma', shape=[n_out], initializer=tf.random_normal_initializer(1.0, stddev),
trainable=True)
batch_mean, batch_var = tf.nn.moments(x, [0, 1, 2], name='moments')
ema = tf.train.ExponentialMovingAverage(decay=decay)
def mean_var_with_update():
ema_apply_op = ema.apply([batch_mean, batch_var])
with tf.control_dependencies([ema_apply_op]):
return tf.identity(batch_mean), tf.identity(batch_var)
mean, var = tf.cond(phase_train,
mean_var_with_update,
lambda: (ema.average(batch_mean), ema.average(batch_var)))
normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, eps)
return normed
def deconv2d(input_, output_shape,
k_h=5, k_w=5, d_h=2, d_w=2, stddev=0.02,
name="deconv2d", with_w=False):
with tf.variable_scope(name):
# filter : [height, width, output_channels, in_channels]
w = tf.get_variable('w', [k_h, k_w, output_shape[-1], input_.get_shape()[-1]],
initializer=tf.random_normal_initializer(stddev=stddev))
try:
deconv = tf.nn.conv2d_transpose(input_, w, output_shape=output_shape,
strides=[1, d_h, d_w, 1])
# Support for verisons of TensorFlow before 0.7.0
except AttributeError:
deconv = tf.nn.deconv2d(input_, w, output_shape=output_shape,
strides=[1, d_h, d_w, 1])
biases = tf.get_variable('biases', [output_shape[-1]], initializer=tf.constant_initializer(0.0))
deconv = tf.reshape(tf.nn.bias_add(deconv, biases), deconv.get_shape())
if with_w:
return deconv, w, biases
else:
return deconv
def get_deconv_filter(f_shape):
width = f_shape[0]
heigh = f_shape[0]
f = ceil(width/2.0)
c = (2 * f - 1 - f % 2) / (2.0 * f)
bilinear = np.zeros([f_shape[0], f_shape[1]])
for x in range(width):
for y in range(heigh):
value = (1 - abs(x / f - c)) * (1 - abs(y / f - c))
bilinear[x, y] = value
weights = np.zeros(f_shape)
for i in range(f_shape[2]):
weights[:, :, i, i] = bilinear
init = tf.constant_initializer(value=weights,
dtype=tf.float32)
return tf.get_variable(name="up_filter", initializer=init,
shape=weights.shape)
def biasVariable(shape,bias=0.1,name=None):
# create a set of bias nodes initialized with a constant 0.1
name = 'biases' if name is None else name
return tf.get_variable(name,shape,initializer=tf.constant_initializer(bias))
def biasVariable(shape,bias=0.1,name=None):
# create a set of bias nodes initialized with a constant 0.1
name = 'biases' if name is None else name
return tf.get_variable(name,shape,initializer=tf.constant_initializer(bias))
gen_embeddings.py 文件源码
项目:almond-nnparser
作者: Stanford-Mobisocial-IoT-Lab
项目源码
文件源码
阅读 31
收藏 0
点赞 0
评论 0
def make_skipgram_softmax_loss(embeddings_matrix, vocabulary_size, vector_size):
vectors = tf.get_variable('vectors', (vocabulary_size, vector_size), dtype=tf.float32, initializer=tf.constant_initializer(embeddings_matrix))
minibatch = tf.placeholder(shape=(None, 2), dtype=tf.int32)
center_word_vector = tf.nn.embedding_lookup(vectors, minibatch[:,0])
yhat = tf.matmul(center_word_vector, vectors, transpose_b=True)
predict_word = minibatch[:,1]
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(labels=predict_word, logits=yhat)
loss = tf.reduce_mean(loss)
return vectors, minibatch, loss
def encode(self, inputs, _input_length, _parses):
with tf.variable_scope('BagOfWordsEncoder'):
W = tf.get_variable('W', (self.embed_size, self.output_size))
b = tf.get_variable('b', shape=(self.output_size,), initializer=tf.constant_initializer(0, tf.float32))
enc_hidden_states = tf.tanh(tf.tensordot(inputs, W, [[2], [0]]) + b)
enc_final_state = tf.reduce_sum(enc_hidden_states, axis=1)
#assert enc_hidden_states.get_shape()[1:] == (self.config.max_length, self.config.hidden_size)
if self._cell_type == 'lstm':
enc_final_state = (tf.contrib.rnn.LSTMStateTuple(enc_final_state, enc_final_state),)
enc_output = tf.nn.dropout(enc_hidden_states, keep_prob=self._dropout, seed=12345)
return enc_output, enc_final_state
base_aligner.py 文件源码
项目:almond-nnparser
作者: Stanford-Mobisocial-IoT-Lab
项目源码
文件源码
阅读 34
收藏 0
点赞 0
评论 0
def add_input_op(self, xavier):
with tf.variable_scope('embed'):
# first the embed the input
if self.config.train_input_embeddings:
if self.config.input_embedding_matrix:
initializer = tf.constant_initializer(self.config.input_embedding_matrix)
else:
initializer = xavier
input_embed_matrix = tf.get_variable('input_embedding',
shape=(self.config.dictionary_size, self.config.embed_size),
initializer=initializer)
else:
input_embed_matrix = tf.constant(self.config.input_embedding_matrix)
# dictionary size x embed_size
assert input_embed_matrix.get_shape() == (self.config.dictionary_size, self.config.embed_size)
# now embed the output
if self.config.train_output_embeddings:
output_embed_matrix = tf.get_variable('output_embedding',
shape=(self.config.output_size, self.config.output_embed_size),
initializer=xavier)
else:
output_embed_matrix = tf.constant(self.config.output_embedding_matrix)
assert output_embed_matrix.get_shape() == (self.config.output_size, self.config.output_embed_size)
inputs = tf.nn.embedding_lookup([input_embed_matrix], self.input_placeholder)
# batch size x max length x embed_size
assert inputs.get_shape()[1:] == (self.config.max_length, self.config.embed_size)
return inputs, output_embed_matrix
def center_loss(features, label, alfa, nrof_classes):
"""Center loss based on the paper "A Discriminative Feature Learning Approach for Deep Face Recognition"
(http://ydwen.github.io/papers/WenECCV16.pdf)
"""
nrof_features = features.get_shape()[1]
centers = tf.get_variable('centers', [nrof_classes, nrof_features], dtype=tf.float32,
initializer=tf.constant_initializer(0), trainable=False)
label = tf.reshape(label, [-1])
centers_batch = tf.gather(centers, label)
diff = (1 - alfa) * (centers_batch - features)
centers = tf.scatter_sub(centers, label, diff)
loss = tf.reduce_mean(tf.square(features - centers_batch))
return loss, centers
def __call__(self, x, train=True):
shape = x.get_shape().as_list()
with tf.variable_scope(self.name) as scope:
self.beta = tf.get_variable("beta", shape[1:],
initializer=tf.constant_initializer(0.))
self.gamma = tf.get_variable("gamma", shape[1:],
initializer=tf.random_normal_initializer(1.,0.02))
self.mean = tf.get_variable("mean", shape[1:],
initializer=tf.constant_initializer(0.),trainable=False)
self.variance = tf.get_variable("variance",shape[1:],
initializer=tf.constant_initializer(1.),trainable=False)
if train:
batch_mean, batch_var = tf.nn.moments(x, [0], name='moments')
self.mean.assign(batch_mean)
self.variance.assign(batch_var)
ema_apply_op = self.ema.apply([self.mean, self.variance])
with tf.control_dependencies([ema_apply_op]):
mean, var = tf.identity(batch_mean), tf.identity(batch_var)
else:
mean, var = self.ema.average(self.mean), self.ema.average(self.variance)
normed = tf.nn.batch_normalization(x, mean, var, self.beta, self.gamma, self.epsilon)
return normed
def get_mask(self, max_frames, num_frames):
mask_array = []
for i in range(max_frames + 1):
tmp = [0.0] * max_frames
for j in range(i):
tmp[j] = 1.0
mask_array.append(tmp)
mask_array = np.array(mask_array)
mask_init = tf.constant_initializer(mask_array)
mask_emb = tf.get_variable("mask_emb", shape = [max_frames + 1, max_frames],
dtype = tf.float32, trainable = False, initializer = mask_init)
mask = tf.nn.embedding_lookup(mask_emb, num_frames)
return mask
def get_support(self, labels, support_type=None):
if support_type == None:
support_type = FLAGS.support_type
if "," in support_type:
new_labels = []
for st in support_type.split(","):
new_labels.append(tf.cast(self.get_support(labels, st), dtype=tf.float32))
support_labels = tf.concat(new_labels, axis=1)
return support_labels
elif support_type == "vertical":
num_classes = FLAGS.num_classes
num_verticals = FLAGS.num_verticals
vertical_file = FLAGS.vertical_file
vertical_mapping = np.zeros([num_classes, num_verticals], dtype=np.float32)
float_labels = tf.cast(labels, dtype=tf.float32)
with open(vertical_file) as F:
for line in F:
group = map(int, line.strip().split())
if len(group) == 2:
x, y = group
vertical_mapping[x, y] = 1
vm_init = tf.constant_initializer(vertical_mapping)
vm = tf.get_variable("vm", shape = [num_classes, num_verticals],
trainable=False, initializer=vm_init)
vertical_labels = tf.matmul(float_labels, vm)
return tf.cast(vertical_labels > 0.2, tf.float32)
elif support_type == "frequent":
num_frequents = FLAGS.num_frequents
frequent_labels = tf.slice(labels, begin=[0, 0], size=[-1, num_frequents])
frequent_labels = tf.cast(frequent_labels, dtype=tf.float32)
return frequent_labels
elif support_type == "label":
float_labels = tf.cast(labels, dtype=tf.float32)
return float_labels
else:
raise NotImplementedError()
def get_mask(self, max_frames, num_frames):
mask_array = []
for i in xrange(max_frames + 1):
tmp = [0.0] * max_frames
for j in xrange(i):
tmp[j] = 1.0
mask_array.append(tmp)
mask_array = np.array(mask_array)
mask_init = tf.constant_initializer(mask_array)
mask_emb = tf.get_variable("mask_emb", shape = [max_frames + 1, max_frames],
dtype = tf.float32, trainable = False, initializer = mask_init)
mask = tf.nn.embedding_lookup(mask_emb, num_frames)
return mask
positional_cnn_deep_combine_chain_model.py 文件源码
项目:youtube-8m
作者: wangheda
项目源码
文件源码
阅读 28
收藏 0
点赞 0
评论 0
def get_mask(self, max_frames, num_frames):
mask_array = []
for i in xrange(max_frames + 1):
tmp = [0.0] * max_frames
for j in xrange(i):
tmp[j] = 1.0
mask_array.append(tmp)
mask_array = np.array(mask_array)
mask_init = tf.constant_initializer(mask_array)
mask_emb = tf.get_variable("mask_emb", shape = [max_frames + 1, max_frames],
dtype = tf.float32, trainable = False, initializer = mask_init)
mask = tf.nn.embedding_lookup(mask_emb, num_frames)
return mask
multi_view_cnn_deep_combine_chain_model.py 文件源码
项目:youtube-8m
作者: wangheda
项目源码
文件源码
阅读 38
收藏 0
点赞 0
评论 0
def get_mask(self, max_frames, num_frames):
mask_array = []
for i in xrange(max_frames + 1):
tmp = [0.0] * max_frames
for j in xrange(i):
tmp[j] = 1.0
mask_array.append(tmp)
mask_array = np.array(mask_array)
mask_init = tf.constant_initializer(mask_array)
mask_emb = tf.get_variable("mask_emb", shape = [max_frames + 1, max_frames],
dtype = tf.float32, trainable = False, initializer = mask_init)
mask = tf.nn.embedding_lookup(mask_emb, num_frames)
return mask
distillchain_cnn_deep_combine_chain_model.py 文件源码
项目:youtube-8m
作者: wangheda
项目源码
文件源码
阅读 26
收藏 0
点赞 0
评论 0
def get_mask(self, max_frames, num_frames):
mask_array = []
for i in xrange(max_frames + 1):
tmp = [0.0] * max_frames
for j in xrange(i):
tmp[j] = 1.0
mask_array.append(tmp)
mask_array = np.array(mask_array)
mask_init = tf.constant_initializer(mask_array)
mask_emb = tf.get_variable("mask_emb", shape = [max_frames + 1, max_frames],
dtype = tf.float32, trainable = False, initializer = mask_init)
mask = tf.nn.embedding_lookup(mask_emb, num_frames)
return mask
def get_mask(self, max_frames, num_frames):
mask_array = []
for i in xrange(max_frames + 1):
tmp = [0.0] * max_frames
for j in xrange(i):
tmp[j] = 1.0
mask_array.append(tmp)
mask_array = np.array(mask_array)
mask_init = tf.constant_initializer(mask_array)
mask_emb = tf.get_variable("mask_emb", shape = [max_frames + 1, max_frames],
dtype = tf.float32, trainable = False, initializer = mask_init)
mask = tf.nn.embedding_lookup(mask_emb, num_frames)
return mask