本文整理汇总了Python中tensorflow.random_uniform_initializer方法的典型用法代码示例。如果您正苦于以下问题:Python tensorflow.random_uniform_initializer方法的具体用法?Python tensorflow.random_uniform_initializer怎么用?Python tensorflow.random_uniform_initializer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类tensorflow
的用法示例。
在下文中一共展示了tensorflow.random_uniform_initializer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: lstm_setup
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def lstm_setup(name, x, batch_size, is_single_step, lstm_dim, lstm_out,
num_steps, state_input_op):
# returns state_name, state_init_op, updated_state_op, out_op
with tf.name_scope('reshape_'+name):
sh = x.get_shape().as_list()
x = tf.reshape(x, shape=[batch_size, -1, sh[-1]])
with tf.variable_scope(name) as varscope:
cell = tf.contrib.rnn.LSTMCell(
num_units=lstm_dim, forget_bias=1.0, state_is_tuple=False,
num_proj=lstm_out, use_peepholes=True,
initializer=tf.random_uniform_initializer(-0.01, 0.01, seed=0),
cell_clip=None, proj_clip=None)
sh = [batch_size, 1, lstm_dim+lstm_out]
state_init_op = tf.constant(0., dtype=tf.float32, shape=sh)
fn = lambda ns: lstm_online(cell, ns, x, state_input_op, varscope)
out_op, updated_state_op = tf.cond(is_single_step, lambda: fn(1), lambda:
fn(num_steps))
return name, state_init_op, updated_state_op, out_op
示例2: get_variable_initializer
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def get_variable_initializer(hparams):
"""Get variable initializer from hparams."""
if not hparams.initializer:
return None
if not tf.contrib.eager.in_eager_mode():
tf.logging.info("Using variable initializer: %s", hparams.initializer)
if hparams.initializer == "orthogonal":
return tf.orthogonal_initializer(gain=hparams.initializer_gain)
elif hparams.initializer == "uniform":
max_val = 0.1 * hparams.initializer_gain
return tf.random_uniform_initializer(-max_val, max_val)
elif hparams.initializer == "normal_unit_scaling":
return tf.variance_scaling_initializer(
hparams.initializer_gain, mode="fan_avg", distribution="normal")
elif hparams.initializer == "uniform_unit_scaling":
return tf.variance_scaling_initializer(
hparams.initializer_gain, mode="fan_avg", distribution="uniform")
elif hparams.initializer == "xavier":
return tf.contrib.layers.xavier_initializer()
else:
raise ValueError("Unrecognized initializer: %s" % hparams.initializer)
示例3: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def __call__(self, obs, reuse=False):
with tf.variable_scope(self.name) as scope:
if reuse:
scope.reuse_variables()
x = obs
x = tf.layers.dense(x, 64)
if self.layer_norm:
x = tc.layers.layer_norm(x, center=True, scale=True)
x = tf.nn.relu(x)
x = tf.layers.dense(x, 64)
if self.layer_norm:
x = tc.layers.layer_norm(x, center=True, scale=True)
x = tf.nn.relu(x)
x = tf.layers.dense(x, self.nb_actions, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
return x
示例4: embedding_matrix
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def embedding_matrix(vocab_size: int, dim: int,
name: str=None):
with tf.name_scope(None, 'embedding-matrix'):
# compute initialization paramters
shape = (vocab_size - 1, dim)
scale = tf.sqrt(1 / shape[0])
# get or initialize embedding matrix
w = tf.get_variable(
name, shape,
dtype=tf.float32,
initializer=tf.random_uniform_initializer(
minval=-scale, maxval=scale
),
trainable=True
)
# 1st row should be zero and not be updated by backprop because of
# zero padding.
emb = tf.concat([
tf.zeros((1, dim), dtype=tf.float32),
w
], 0)
return emb
示例5: init_param
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def init_param(self):
idm = self.input_dim
hs = self.hidden_size
ws = len(self.window)
nf = idm * ws
# author's special initlaization strategy.
self.Wemb = tf.get_variable(name=self.name + '_Wemb', shape=[self.vocab_size, idm], dtype=tf.float32, initializer=tf.random_uniform_initializer())
self.bhid = tf.get_variable(name=self.name + '_bhid', shape=[self.vocab_size], dtype=tf.float32, initializer=tf.zeros_initializer())
self.Vhid = tf.get_variable(name=self.name + '_Vhid', shape=[hs, idm], dtype=tf.float32, initializer=tf.random_uniform_initializer())
self.Vhid = dot(self.Vhid, self.Wemb) # [hidden_size, vocab_size]
self.i2h_W = tf.get_variable(name=self.name + '_i2h_W', shape=[idm, hs * 4], dtype=tf.float32, initializer=tf.random_uniform_initializer())
self.h2h_W = tf.get_variable(name=self.name + '_h2h_W', shape=[hs, hs * 4], dtype=tf.float32, initializer=tf.orthogonal_initializer())
self.z2h_W = tf.get_variable(name=self.name + '_z2h_W', shape=[nf, hs * 4], dtype=tf.float32, initializer=tf.random_uniform_initializer())
b_init_1 = tf.zeros((hs,))
b_init_2 = tf.ones((hs,)) * 3
b_init_3 = tf.zeros((hs,))
b_init_4 = tf.zeros((hs,))
b_init = tf.concat([b_init_1, b_init_2, b_init_3, b_init_4], axis=0)
# b_init = tf.constant(b_init)
# self.b = tf.get_variable(name=self.name + '_b', shape=[hs * 4], dtype=tf.float32, initializer=b_init)
self.b = tf.get_variable(name=self.name + '_b', dtype=tf.float32, initializer=b_init) # ValueError: If initializer is a constant, do not specify shape.
self.C0 = tf.get_variable(name=self.name + '_C0', shape=[nf, hs], dtype=tf.float32, initializer=tf.random_uniform_initializer())
self.b0 = tf.get_variable(name=self.name + '_b0', shape=[hs], dtype=tf.float32, initializer=tf.zeros_initializer())
示例6: fullyConnected
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def fullyConnected(x, num_units_out, wd= 0.0, weight_initializer= None, bias_initializer= None, inference_only= False):
num_units_in = x.get_shape()[1]
stddev = 1./tf.sqrt(tf.cast(num_units_out, tf.float32))
if weight_initializer is None:
weight_initializer = tf.random_uniform_initializer(minval= -stddev, maxval= stddev, dtype= tf.float32)
if bias_initializer is None:
bias_initializer = tf.random_uniform_initializer(minval= -stddev, maxval= stddev, dtype= tf.float32)
weights = _get_variable('weights',
[num_units_in, num_units_out], weight_initializer, tf.contrib.layers.l2_regularizer(wd))
biases = _get_variable('biases',
[num_units_out], bias_initializer)
return tf.nn.xw_plus_b(x, weights, biases)
# Convolution Layer
示例7: spatialConvolution
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def spatialConvolution(x, ksize, stride, filters_out, wd= 0.0, weight_initializer= None, bias_initializer= None, inference_only= False):
filters_in = x.get_shape()[-1]
stddev = 1./tf.sqrt(tf.cast(filters_out, tf.float32))
if weight_initializer is None:
weight_initializer = tf.random_uniform_initializer(minval= -stddev, maxval= stddev, dtype= tf.float32)
if bias_initializer is None:
bias_initializer = tf.random_uniform_initializer(minval= -stddev, maxval= stddev, dtype= tf.float32)
shape = [ksize, ksize, filters_in, filters_out]
weights = _get_variable('weights',
shape, weight_initializer, tf.contrib.layers.l2_regularizer(wd))
conv = tf.nn.conv2d(x, weights, [1, stride, stride, 1], padding= 'SAME')
biases = _get_variable('biases', [filters_out], bias_initializer)
return tf.nn.bias_add(conv, biases)
# Max Pooling Layer
示例8: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def __call__(self, obs, reuse=False):
with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
x = self.network_builder(obs)
# x = tf.layers.dense(x, self.nb_actions, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
# x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.hidden_layer1, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.hidden_layer2, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.hidden_layer3, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.hidden_layer4, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.nb_actions, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
'''
in order to discrete the action, apply the trick of sharp sigmoid.
with the following line: x<0 will be near 0, x>0 will be near 1
then the discrete step in action choice wont affect too much of accuracy (with only small change of action value!)
the key point of this step is to keep most part of discrete operation in the tensor graph, so as to be back propagated
'''
x = tf.nn.sigmoid(1000*x) # sigmoid ~ (0,1), tanh ~ (-1 , 1)
return x
示例9: __call__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def __call__(self, obs, reuse=False):
with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
x = self.network_builder(obs)
# x = tf.layers.dense(x, self.nb_actions, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
# x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.hidden_layer1, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.hidden_layer2, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
x = tf.nn.tanh(x)
# x = tf.layers.dense(x, self.hidden_layer3, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
# x = tf.nn.tanh(x)
# x = tf.layers.dense(x, self.hidden_layer4, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
# x = tf.nn.tanh(x)
x = tf.layers.dense(x, self.nb_actions, kernel_initializer=tf.random_uniform_initializer(minval=-3e-3, maxval=3e-3))
# x = self.step_activation(tf.nn.sigmoid(x)) # sigmoid ~ (0,1), tanh ~ (-1 , 1)
'''1000 makes the binarization of output of sigmoid has smaller difference
that cannot be backpropagated with tensorflow'''
x = tf.nn.sigmoid(1000*x) # sigmoid ~ (0,1), tanh ~ (-1 , 1)
return x
示例10: test_get_embedding
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def test_get_embedding(self):
"""Tests :func:`~texar.modules.embedder.embedder_utils.get_embedding`.
"""
vocab_size = 100
emb = embedder_utils.get_embedding(num_embeds=vocab_size)
self.assertEqual(emb.shape[0].value, vocab_size)
self.assertEqual(emb.shape[1].value,
embedder_utils.default_embedding_hparams()["dim"])
hparams = {
"initializer": {
"type": tf.random_uniform_initializer(minval=-0.1, maxval=0.1)
},
"regularizer": {
"type": tf.keras.regularizers.L1L2(0.1, 0.1)
}
}
emb = embedder_utils.get_embedding(
hparams=hparams, num_embeds=vocab_size,
variable_scope='embedding_2')
self.assertEqual(emb.shape[0].value, vocab_size)
self.assertEqual(emb.shape[1].value,
embedder_utils.default_embedding_hparams()["dim"])
示例11: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def __init__(self, name, layer_conf):
self._name = layer_conf.pop('name', None) or name
activation_name = layer_conf.get('activation', None)
if activation_name:
layer_conf['activation'] = Layer.activation_dict[activation_name]
self._kernel_initializer = layer_conf.pop('kernel_initializer', None)
if isinstance(self._kernel_initializer, str):
assert self._kernel_initializer in ('random_normal_initializer',
'random_uniform_initializer',
'glorot_normal_initializer',
'glorot_uniform_initializer'), \
"Invalid value of kernel_initializer, available value is one of " \
"['random_normal_initializer', 'random_uniform_initializer'," \
"'glorot_normal_initializer', 'glorot_uniform_initializer']"
self._kernel_initializer = Layer.initializer_dict[
self._kernel_initializer]
elif (isinstance(self._kernel_initializer, int)
or isinstance(self._kernel_initializer, float)):
self._kernel_initializer = tf.constant_initializer(
value=self._kernel_initializer)
示例12: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def __init__(self, args):
self.args = args
self.global_step = tf.Variable(0, name="global_step", trainable=False)
# euclidean
self.l2_loss = tf.constant(0.)
self.uniform_init = tf.random_uniform_initializer(
-args.u_scope, args.u_scope, seed=args.seed)
self.init_placeholders()
self.init_graph()
optimizer = tf.train.AdamOptimizer(
args.lr, beta1=0.9, beta2=0.98, epsilon=1e-8)
self.train_op = optimizer.minimize(
self.loss, global_step=self.global_step)
示例13: __init__
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def __init__(self, args, pre_w2v=None):
self.args = args
self.global_step = tf.Variable(0, name="global_step", trainable=False)
#euclidean
self.l2_loss = tf.constant(0.)
self.uniform_init = tf.random_uniform_initializer(
-args.u_scope, args.u_scope, seed=args.seed)
self.init_placeholders()
self.init_graph()
optimizer = tf.train.AdamOptimizer(
args.lr, beta1=0.9, beta2=0.98, epsilon=1e-8)
self.train_op = optimizer.minimize(self.loss, global_step=self.global_step)
示例14: create_latent_space
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def create_latent_space(batch_size, shape, steps=None):
""" Create the latent space """
# Setup the latent space. The latent space is a 2-D tensor used for each element in the batch
# with dimensions [batch_size, latent_size, latent_size]. If steps are provided then there is a
# latent space per step with dimensions [step, batch_size, latent_size, latent_size].
latent_shape = shape
if steps is not None:
latent_shape = (steps,) + latent_shape
latent_space = framework.model_variable(
'LatentSpace', shape=latent_shape, trainable=True,
initializer=tf.random_uniform_initializer(0.0, 1e-3))
latent_space = tf.tile(latent_space, (batch_size,) + (1,) * (len(latent_shape) - 1))
latent_space = tf.reshape(latent_space, (batch_size,) + latent_shape)
if steps is not None:
permutation = (1, 0) + tuple(x + 2 for x in range(len(shape)))
latent_space = tf.transpose(latent_space, permutation)
return latent_space
示例15: domain_classifier
# 需要导入模块: import tensorflow [as 别名]
# 或者: from tensorflow import random_uniform_initializer [as 别名]
def domain_classifier(self, images, name="G", reuse=False):
random_uniform_init = tf.random_uniform_initializer(minval=-0.1, maxval=0.1)
with tf.variable_scope(name):
tf.get_variable_scope().reuse_variables()
with tf.variable_scope("images"):
# "generator/images"
images_W = tf.get_variable("images_W", [self.img_dims, self.G_hidden_size], "float32", random_uniform_init)
images_emb = tf.matmul(images, images_W) # B,H
l2_loss = tf.constant(0.0)
with tf.variable_scope("domain"):
if reuse:
tf.get_variable_scope().reuse_variables()
with tf.variable_scope("output"):
output_W = tf.get_variable("output_W", [self.G_hidden_size, self.num_domains],
"float32", random_uniform_init)
output_b = tf.get_variable("output_b", [self.num_domains], "float32", random_uniform_init)
l2_loss += tf.nn.l2_loss(output_W)
l2_loss += tf.nn.l2_loss(output_b)
logits = tf.nn.xw_plus_b(images_emb, output_W, output_b, name="logits")
predictions = tf.argmax(logits, 1, name="predictions")
return predictions, logits, l2_loss