def sample_encoded_context(embeddings, model, bAugmentation=True):
'''Helper function for init_opt'''
# Build conditioning augmentation structure for text embedding
# under different variable_scope: 'g_net' and 'hr_g_net'
c_mean_logsigma = model.generate_condition(embeddings)
mean = c_mean_logsigma[0]
if bAugmentation:
# epsilon = tf.random_normal(tf.shape(mean))
epsilon = tf.truncated_normal(tf.shape(mean))
stddev = tf.exp(c_mean_logsigma[1])
c = mean + stddev * epsilon
else:
c = mean
return c
python类truncated_normal()的实例源码
def weight_variable(shape):
inital = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(inital)
def create_variable(shape, name, c=None, sigma=None, trainable=True):
if sigma:
initial = tf.truncated_normal(shape, stddev=sigma, name=name)
else:
initial = tf.constant(c if c else 0.0, shape=shape, name=name)
return tf.Variable(initial, trainable=trainable)
def xavier_normal_dist(shape):
return tf.truncated_normal(shape, mean=0, stddev=tf.sqrt(3. / shape[-1] + shape[-2]))
def xavier_normal_dist_conv3d(shape):
return tf.truncated_normal(shape, mean=0,
stddev=tf.sqrt(3. / (tf.reduce_prod(shape[:3]) * tf.reduce_sum(shape[3:]))))
def convolution_layer_3d(layer_input, filter, strides, padding='SAME'):
assert len(filter) == 5 # [filter_depth, filter_height, filter_width, in_channels, out_channels]
assert len(strides) == 5 # must match input dimensions [batch, in_depth, in_height, in_width, in_channels]
assert padding in ['VALID', 'SAME']
# w = tf.Variable(initial_value=tf.truncated_normal(shape=filter), name='weights')
w = tf.Variable(initial_value=xavier_uniform_dist_conv3d(shape=filter), name='weights')
b = tf.Variable(tf.constant(1.0, shape=[filter[-1]]), name='biases')
convolution = tf.nn.conv3d(layer_input, w, strides, padding)
return convolution + b
def deconvolution_layer_3d(layer_input, filter, output_shape, strides, padding='SAME'):
assert len(filter) == 5 # [depth, height, width, output_channels, in_channels]
assert len(strides) == 5 # must match input dimensions [batch, depth, height, width, in_channels]
assert padding in ['VALID', 'SAME']
# w = tf.Variable(initial_value=tf.truncated_normal(shape=filter), name='weights')
w = tf.Variable(initial_value=xavier_uniform_dist_conv3d(shape=filter), name='weights')
b = tf.Variable(tf.constant(1.0, shape=[filter[-2]]), name='biases')
deconvolution = tf.nn.conv3d_transpose(layer_input, w, output_shape, strides, padding)
return deconvolution + b
def weightVar(shape, mean=0.0, stddev=0.02, name='weights'):
init_w = tf.truncated_normal(shape=shape, mean=mean, stddev=stddev)
return tf.Variable(init_w, name=name)
def weight_variable(self, shape, name):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.get_variable(name=name, initializer=initial, trainable=True)
def weight_variable(self, shape):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial)
def weight_variable(self, shape):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.Variable(initial)
def weight_variable(self, shape, name):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.get_variable(name=name, initializer=initial, trainable=True)
def weight_variable(self, shape, name):
initial = tf.truncated_normal(shape, stddev=0.01)
return tf.get_variable(name=name, initializer=initial, trainable=True)
def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def weight_variable(self,shape,name="v"):
if self.initializer == "graves" and False:
initial = tf.truncated_normal_initializer(mean=0., stddev=.075, seed=None, dtype=tf.float32)
else:
initial = tf.truncated_normal(shape, stddev=.075)
return tf.Variable(initial,name=name+"_weight")
def _linear_layer(self, input, input_size, output_size, scope_name):
with tf.variable_scope(scope_name) as scope:
weights = tf.Variable(name='weights',
initial_value=tf.truncated_normal(shape=[input_size, output_size], stddev=0.1))
biases = tf.Variable(name='biases', initial_value=tf.constant(value=0.1, shape=[output_size]))
output = tf.matmul(input, weights) + biases
return output
def _weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def __init__(self, embedding_length):
self._embedding_length = embedding_length
self._named_tensors = {}
for n in xrange(10):
# Note: the examples only have the numbers 0 through 9 as terminal nodes.
name = 'terminal_' + str(n)
self._named_tensors[name] = tf.Variable(
tf.truncated_normal([embedding_length],
dtype=tf.float32,
stddev=1),
name=name)
self._combiner_weights = {}
self._loom_ops = {}
for name in calculator_pb2.CalculatorExpression.OpCode.keys():
weights_var = tf.Variable(
tf.truncated_normal([2 * embedding_length, embedding_length],
dtype=tf.float32,
stddev=1),
name=name)
self._combiner_weights[name] = weights_var
self._loom_ops[name] = CombineLoomOp(2, embedding_length, weights_var)
self._loom = loom.Loom(
named_tensors=self._named_tensors,
named_ops=self._loom_ops)
self._output = self._loom.output_tensor(
loom.TypeShape('float32', [embedding_length]))
def weight_variable(self, shape):
"""Create a weight variable with appropriate initialization."""
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)