def add_hl(self, q_embed, aplus_embed, aminus_embed):
with tf.variable_scope('HL'):
W = tf.get_variable('weights', shape=[self.config.embedding_size, self.config.hidden_size], initializer=tf.uniform_unit_scaling_initializer())
b = tf.get_variable('biases', initializer=tf.constant(0.1, shape=[self.config.hidden_size]))
h_q = tf.reshape(tf.nn.tanh(tf.matmul(tf.reshape(q_embed, [-1, self.config.embedding_size]), W)+b), [self.config.batch_size, self.config.sequence_length, -1])
h_ap = tf.reshape(tf.nn.tanh(tf.matmul(tf.reshape(aplus_embed, [-1, self.config.embedding_size]), W)+b), [self.config.batch_size, self.config.sequence_length, -1])
h_am = tf.reshape(tf.nn.tanh(tf.matmul(tf.reshape(aminus_embed, [-1, self.config.embedding_size]), W)+b), [self.config.batch_size, self.config.sequence_length, -1])
tf.add_to_collection('total_loss', 0.5*self.config.l2_reg_lambda*tf.nn.l2_loss(W))
# print 'h_q[shape]:', tf.shape(h_q)
# print 'h_ap[shape]:', tf.shape(h_ap)
# print 'h_am[shape]:', tf.shape(h_am)
return h_q, h_ap, h_am
# CNN?
python类uniform_unit_scaling_initializer()的实例源码
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
x = tf.reshape(x, [self.hps.batch_size, -1])
w = tf.get_variable(
'DW', [x.get_shape()[1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def score(feature_vec):
W = tf.get_variable("W", shape=[feature_vec.get_shape()[1],1], initializer=tf.uniform_unit_scaling_initializer()) # init_weight([int(feature_vec.get_shape()[1]),1])
return tf.matmul(feature_vec,W)
def check_params(self):
if self.interaction not in DSSA.VALID_INTERACTION:
raise ValueError('interaction not valid, it should be one of {}'
.format(', '.join(map(lambda x: '"' + x + '"', DSSA.VALID_INTERACTION))))
if self.cell_type not in DSSA.VALID_CELL_TYPE:
raise ValueError('cell_type not valid, it should be one of {}'
.format(', '.join(map(lambda x: '"' + x + '"', DSSA.VALID_CELL_TYPE))))
if not isinstance(self.doc_emb, np.ndarray) or not isinstance(self.query_emb, np.ndarray):
raise ValueError('both doc_emb and query_emb should by instance of numpy.ndarray')
self.doc_emb_actual_size = self.n_rel_feat * self.most_n_subquery + self.n_doc_emb
if self.doc_emb.shape[1] != self.doc_emb_actual_size:
raise ValueError('doc_emb shape[1] is unexpected. {} is desired while we got {}'
.format(self.doc_emb_actual_size, self.doc_emb.shape[1]))
self.query_emb_actual_size = self.n_query_emb + 1
if self.query_emb.shape[1] != self.query_emb_actual_size:
raise ValueError('query_emb shape[1] is unexpected. {} is desired while we got {}'
.format(self.query_emb_actual_size, self.query_emb.shape[1]))
if self.optimization not in DSSA.VALID_OPTIMIZATION:
raise ValueError('optimization not valid, it should be one of {}'
.format(', '.join(map(lambda x: '"' + x + '"', DSSA.VALID_OPTIMIZATION))))
self.input_dim = 1 + self.most_n_subquery
self.expand_input_dim = \
self.n_rel_feat * self.most_n_subquery + self.n_doc_emb + (self.n_query_emb + 1) * self.most_n_subquery
if self.reuse_model and not hasattr(self, 'session_'): # read model from file
self.graph_ = tf.Graph()
with self.graph_.as_default():
tf.set_random_seed(self.random_seed)
with vs.variable_scope('DSSA', initializer=
tf.uniform_unit_scaling_initializer(seed=self.random_seed)) as scope:
self.build_graph()
scope.reuse_variables()
self.build_graph_test()
self.session_ = tf.Session(graph=self.graph_)
print('load model from "{}"'.format(self.reuse_model))
self.saver.restore(self.session_, self.reuse_model)
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
x = tf.reshape(x, [self.batch_size, -1])
w = tf.get_variable(
'DW', [self.filters[-1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def _fully_convolutional(self, x, out_dim):
"""FullyConvolutional layer for final output."""
w = tf.get_variable(
'DW', [1, 1, self.filters[-1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.conv2d(x, w, self._stride_arr(1), padding='SAME') + b
def FullyConnected(x, out_dim,
W_init=None, b_init=None,
nl=tf.nn.relu, use_bias=True):
"""
Fully-Connected layer.
:param input: a tensor to be flattened except the first dimension.
:param out_dim: output dimension
:param W_init: initializer for W. default to `xavier_initializer_conv2d`.
:param b_init: initializer for b. default to zero initializer.
:param nl: nonlinearity. default to `relu`.
:param use_bias: whether to use bias. a boolean default to True
:returns: a 2D tensor
"""
x = batch_flatten(x)
in_dim = x.get_shape().as_list()[1]
if W_init is None:
#W_init = tf.truncated_normal_initializer(stddev=1 / math.sqrt(float(in_dim)))
W_init = tf.uniform_unit_scaling_initializer(factor=1.43)
if b_init is None:
b_init = tf.constant_initializer()
W = tf.get_variable('W', [in_dim, out_dim], initializer=W_init)
if use_bias:
b = tf.get_variable('b', [out_dim], initializer=b_init)
prod = tf.nn.xw_plus_b(x, W, b) if use_bias else tf.matmul(x, W)
return nl(prod, name='output')
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
x = tf.reshape(x, [self.hps.batch_size, -1])
w = tf.get_variable(
'DW', [x.get_shape()[1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def _fully_connected(self, x, out_dim):
return slim.layers.fully_connected(x, out_dim,
activation_fn=None,
#weights_initializer=tf.uniform_unit_scaling_initializer(factor=1.0)
weights_initializer=tf.uniform_unit_scaling_initializer(factor=1.0)
#weights_initializer=tf.random_normal_initializer(stddev=0.01)
#weights_initializer=tf.contrib.layers.variance_scaling_initializer()
)
resnet_old_reference.py 文件源码
项目:tensorflow-input-pipelines
作者: ischlag
项目源码
文件源码
阅读 24
收藏 0
点赞 0
评论 0
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
x = tf.reshape(x, [self.hps.batch_size, -1])
w = tf.get_variable(
'DW', [x.get_shape()[1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def _fully_connected(self, x, out_dim):
return slim.layers.fully_connected(x, out_dim,
activation_fn=None,
#weights_initializer=tf.uniform_unit_scaling_initializer(factor=1.0)
weights_initializer=tf.uniform_unit_scaling_initializer(factor=1.0)
#weights_initializer=tf.random_normal_initializer(stddev=0.01)
#weights_initializer=tf.contrib.layers.variance_scaling_initializer()
)
def _fully_connected(self, x, out_dim):
return slim.layers.fully_connected(x, out_dim,
activation_fn=None,
#weights_initializer=tf.uniform_unit_scaling_initializer(factor=1.0)
weights_initializer=tf.uniform_unit_scaling_initializer(factor=1.0)
#weights_initializer=tf.random_normal_initializer(stddev=0.01)
#weights_initializer=tf.contrib.layers.variance_scaling_initializer()
)
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
x = tf.reshape(x, [self.hps.batch_size, -1])
w = tf.get_variable(
'DW', [x.get_shape()[1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def fully_connected(name, l, out_dim):
"""Fully connected layer.
Args:
name: Scope name of this function
l : Output of previous layer
out_dim: Dimension of each output feature
"""
with tf.variable_scope(name):
l = tf.reshape(l, [l.get_shape().as_list()[0], -1])
weights = tf.get_variable('weights', [l.get_shape()[1], out_dim], tf.float32,
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
biases = tf.get_variable('biases', [out_dim], tf.float32, initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(l, weights, biases)
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
w = tf.get_variable(
'DW', [x.get_shape()[1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def _fully_connected(self, x, out_dim):
"""FullyConnected layer for final output."""
x = tf.reshape(x, [self.hps.batch_size, -1])
w = tf.get_variable(
'DW', [x.get_shape()[1], out_dim],
initializer=tf.uniform_unit_scaling_initializer(factor=1.0))
b = tf.get_variable('biases', [out_dim],
initializer=tf.constant_initializer())
return tf.nn.xw_plus_b(x, w, b)
def __init__(self, num_units_out, activation=tf.nn.relu, initializer=None,
input_keep_prob=None, output_keep_prob=None,
normalization_fn=None, weight_norm=False, name=None):
"""Initializes the layer.
Args:
num_units_out: The number of output units in the layer.
activation: The activation function. Default is ReLU. Use `None` to get a
linear layer.
initializer: The initializer for the weights. Defaults to uniform unit
scaling with factor derived in <http://arxiv.org/pdf/1412.6558v3.pdf>
if activation is ReLU, ReLU6, tanh, or linear. Otherwise defaults to
truncated normal initialization with a standard deviation of 0.01.
input_keep_prob: Optional scalar float32 tensor for dropout on input.
Feed 1.0 at serving to disable dropout.
output_keep_prob: Optional scalar float32 tensor for dropout on output.
Feed 1.0 at serving to disable dropout.
normalization_fn: Optional normalization function that will be inserted
before nonlinearity.
weight_norm: A bool to control whether weight normalization is used. See
https://arxiv.org/abs/1602.07868 for how it works.
name: An optional string name. Defaults to `FC_%d % num_units_out`. Used
to name the variable scope where the variables for the layer live.
"""
self.set_constructor_args('td.FC', *get_local_arguments(FC.__init__, True))
if not initializer:
# TODO(SamEisenstat): This constant is calibrated for ReLU, something else
# might be better for ReLU6.
if activation in [tf.nn.relu, tf.nn.relu6]:
initializer = tf.uniform_unit_scaling_initializer(1.43)
elif activation == tf.tanh:
initializer = tf.uniform_unit_scaling_initializer(1.15)
elif not activation:
initializer = tf.uniform_unit_scaling_initializer(1.0)
else:
initializer = tf.truncated_normal_initializer(stddev=0.01)
self._activation = activation
self._initializer = initializer
self._input_keep_prob = input_keep_prob
self._output_keep_prob = output_keep_prob
self._normalization_fn = normalization_fn
self._weight_norm = weight_norm
if name is None: name = 'FC_%d' % num_units_out
super(FC, self).__init__(
output_type=tdt.TensorType([num_units_out]), name_or_scope=name)
def __init__(self, num_buckets, num_units_out, initializer=None, name=None,
trainable=True, mod_inputs=True):
"""Initializes the layer.
Args:
num_buckets: How many buckets the embedding has.
num_units_out: The number of output units in the layer.
initializer: the initializer for the weights. Defaults to uniform unit
scaling. The initializer can also be a Tensor or numpy array, in which
case the weights are initialized to this value and shape. Note that in
this case the weights will still be trainable unless you also pass
`trainable=False`.
name: An optional string name. Defaults to
`Embedding_%d_%d % (num_buckets, num_units_out)`. Used to name the
variable scope where the variables for the layer live.
trainable: Whether or not to make the weights trainable.
mod_inputs: Whether or not to mod the input by the number of buckets.
Raises:
ValueError: If the shape of `weights` is not
`(num_buckets, num_units_out)`.
"""
self.set_constructor_args('td.Embedding',
*get_local_arguments(Embedding.__init__, True))
self._weights_shape = (num_buckets, num_units_out)
if name is None: name = 'Embedding_%d_%d' % self._weights_shape
if initializer is None:
initializer = tf.uniform_unit_scaling_initializer(1.0)
elif isinstance(initializer, np.ndarray):
initializer = tf.convert_to_tensor(initializer)
if isinstance(initializer, tf.Tensor):
initializer.set_shape(self._weights_shape)
self._weights_shape = None # otherwise get_variable barfs
self._initializer = initializer
self._num_buckets = num_buckets
self._num_units_out = num_units_out
self._trainable = trainable
self._mod_inputs = bool(mod_inputs)
super(Embedding, self).__init__(
output_type=tdt.TensorType([num_units_out]), name_or_scope=name)
def conv1d(x,
num_filters,
filter_length,
name,
dilation=1,
causal=True,
kernel_initializer=tf.uniform_unit_scaling_initializer(1.0),
biases_initializer=tf.constant_initializer(0.0)):
"""Fast 1D convolution that supports causal padding and dilation.
Args:
x: The [mb, time, channels] float tensor that we convolve.
num_filters: The number of filter maps in the convolution.
filter_length: The integer length of the filter.
name: The name of the scope for the variables.
dilation: The amount of dilation.
causal: Whether or not this is a causal convolution.
kernel_initializer: The kernel initialization function.
biases_initializer: The biases initialization function.
Returns:
y: The output of the 1D convolution.
"""
batch_size, length, num_input_channels = x.get_shape().as_list()
assert length % dilation == 0
kernel_shape = [1, filter_length, num_input_channels, num_filters]
strides = [1, 1, 1, 1]
biases_shape = [num_filters]
padding = 'VALID' if causal else 'SAME'
with tf.variable_scope(name):
weights = tf.get_variable(
'W', shape=kernel_shape, initializer=kernel_initializer)
biases = tf.get_variable(
'biases', shape=biases_shape, initializer=biases_initializer)
x_ttb = time_to_batch(x, dilation)
if filter_length > 1 and causal:
x_ttb = tf.pad(x_ttb, [[0, 0], [filter_length - 1, 0], [0, 0]])
x_ttb_shape = x_ttb.get_shape().as_list()
x_4d = tf.reshape(x_ttb, [x_ttb_shape[0], 1,
x_ttb_shape[1], num_input_channels])
y = tf.nn.conv2d(x_4d, weights, strides, padding=padding)
y = tf.nn.bias_add(y, biases)
y_shape = y.get_shape().as_list()
y = tf.reshape(y, [y_shape[0], y_shape[2], num_filters])
y = batch_to_time(y, dilation)
y.set_shape([batch_size, length, num_filters])
return y
def _enc_upsampling_conv(encoding,
audio_length,
filter_length=1024,
time_stride=512):
"""Upsample local conditioning encoding to match time dim. of audio
:param encoding: [mb, timeframe, channels] Local conditionining encoding
:param audio_length: Length of time dimension of audio
:param filter_length: transpose conv. filter length
:param time_stride: stride along time dimension (upsamp. factor)
:return: upsampled local conditioning encoding
"""
with tf.variable_scope('upsampling_conv'):
batch_size, _, enc_channels = encoding.get_shape().as_list()
shape = tf.shape(encoding)
strides = [1, 1, time_stride, 1]
output_length = (shape[1] - 1) * time_stride + filter_length
output_shape = tf.stack(
[batch_size, 1, output_length, enc_channels])
kernel_shape = [1, filter_length, enc_channels, enc_channels]
biases_shape = [enc_channels]
upsamp_weights = tf.get_variable(
'weights',
kernel_shape,
initializer=tf.uniform_unit_scaling_initializer(1.0))
upsamp_biases = tf.get_variable(
'biases',
biases_shape,
initializer=tf.constant_initializer(0.0))
encoding = tf.reshape(encoding,
[batch_size, 1, shape[1], enc_channels])
upsamp_conv = tf.nn.conv2d_transpose(
encoding,
upsamp_weights, output_shape, strides, padding='VALID')
output = tf.nn.bias_add(upsamp_conv, upsamp_biases)
output = tf.reshape(output,
[batch_size, output_length, enc_channels])
output_sliced = tf.slice(
output, [0, 0, 0],
tf.stack([-1, audio_length, -1]))
output_sliced.set_shape([batch_size, audio_length, enc_channels])
return output_sliced
# especially for global conditioning coz it doesn't algin with audio input
# on the time dimension, and needs broadcasting its value to input;
# for local conditioning, we've already match their size