本文整理汇总了Python中tensorflow.get_variable函数的典型用法代码示例。如果您正苦于以下问题:Python get_variable函数的具体用法?Python get_variable怎么用?Python get_variable使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_variable函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, is_training, config):
self.batch_size = batch_size = config.batch_size
size = config.hidden_size
self.max_len = max_len = config.max_len
vocab_size = config.vocab_size
self._input_data = tf.placeholder(tf.int32, [batch_size, config.max_len])
self._targets = tf.placeholder(tf.int32, [batch_size])
embedding = tf.get_variable("embedding", [vocab_size, size])
inputs = tf.nn.embedding_lookup(embedding, self._input_data)
output = tf.reduce_sum(inputs, 1)
softmax_w = tf.get_variable("softmax_w", [size, 2])
softmax_b = tf.get_variable("softmax_b", [2])
logits = tf.matmul(output, softmax_w) + softmax_b
prediction = tf.nn.softmax(logits)
self._prediction = prediction
loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, self._targets)
self._cost = cost = tf.reduce_sum(loss) / batch_size
if not is_training:
return
self._lr = tf.Variable(0.0, trainable=False)
tvars = tf.trainable_variables()
grads, _ = tf.clip_by_global_norm(tf.gradients(cost, tvars),
config.max_grad_norm)
optimizer = tf.train.GradientDescentOptimizer(self.lr)
self._train_op = optimizer.apply_gradients(zip(grads, tvars))
开发者ID:cbienpourtoi,项目名称:test-tensorflow-sentiments,代码行数:33,代码来源:sentiment_model_3.py
示例2: __call__
def __call__(self, inputs, state, scope=None):
"""Long short-term memory cell (LSTM)."""
with tf.variable_scope(scope or type(self).__name__): # "BasicLSTMCell"
# Parameters of gates are concatenated into one multiply for efficiency.
if self._state_is_tuple:
c, h = state
else:
c, h = array_ops.split(1, 2, state)
concat = _linear([inputs, h], 4 * self._num_units, True, 0.,
self.weights_init, self.trainable, self.restore,
self.reuse)
# i = input_gate, j = new_input, f = forget_gate, o = output_gate
i, j, f, o = array_ops.split(1, 4, concat)
new_c = (c * self._inner_activation(f + self._forget_bias) +
self._inner_activation(i) *
self._activation(j))
new_h = self._activation(new_c) * self._inner_activation(o)
if self._state_is_tuple:
new_state = _rnn_cell.LSTMStateTuple(new_c, new_h)
else:
new_state = array_ops.concat(1, [new_c, new_h])
# Retrieve RNN Variables
with tf.variable_scope('Linear', reuse=True):
self.W = tf.get_variable('Matrix')
self.b = tf.get_variable('Bias')
return new_h, new_state
开发者ID:mixml,项目名称:tflearn,代码行数:31,代码来源:recurrent.py
示例3: get_run_op
def get_run_op():
# Create an optimizer that performs gradient descent.
#opt = tf.train.GradientDescentOptimizer(learning_rate=0.01)
slice_size = FLAGS.batch_size / FLAGS.num_cuts
print('Slice size:{}'.format(slice_size))
data = None
label = None
last_fc = [tf.no_op()]
with tf.device('/gpu:0'):
data = tf.get_variable(
name = 'data',
shape=[slice_size, FLAGS.hidden_size],
trainable=False)
'''
label = tf.get_variable(
name = 'label',
shape = [slice_size, FLAGS.hidden_size],
trainable=False))
with tf.variable_scope('fc_in'):
weight_in = tf.zeros([1000, FLAGS.hidden_size])
for k in xrange(FLAGS.num_cuts):
with tf.control_dependencies([last_fc[-1]]):
last_fc.append(tf.matmul(data[k+1], weight_in))
'''
for i in xrange(FLAGS.num_cuts):
last_fc.append(data)
for i in xrange(FLAGS.num_layers):
dev = '/gpu:%d' % (i * FLAGS.num_gpus / FLAGS.num_layers)
with tf.device(dev), scopes.arg_scope([variables.variable], device=dev):
tmp_fc = [tf.no_op()]
with tf.variable_scope('fc%d' % i):
w = tf.get_variable(
name='w',
shape=[FLAGS.hidden_size, FLAGS.hidden_size],
trainable=True)
for k in xrange(FLAGS.num_cuts):
with tf.control_dependencies([tmp_fc[-1]]):
tmp_fc.append(tf.matmul(last_fc[k+1], w))
last_fc = tmp_fc
if i == FLAGS.num_layers - 1:
with tf.control_dependencies(last_fc):
train_op = tf.no_op()
'''
with tf.device('/gpu:%d' % (FLAGS.num_gpus - 1)):
tmp_fc = [tf.no_op()]
with tf.variable_scope('fc_out'):
weight_out = tf.zeros([FLAGS.hidden_size, 1000])
for k in xrange(FLAGS.num_cuts):
with tf.control_dependencies([tmp_fc[-1]]):
tmp_fc.append(tf.matmul(last_fc[k+1], weight_out))
last_fc = tmp_fc
loss = tf.nn_softmax_cross_entropy_with_logits(last_fc, labels, name='xentropy')
grads = opt.compute_gradients(loss)
apply_gradient_op = opt.apply_gradients(grads)
train_op = tf.group(apply_gradient_op)
'''
init_op = tf.initialize_all_variables()
return init_op, train_op
开发者ID:houcy,项目名称:models,代码行数:60,代码来源:pipelining.py
示例4: wide_model
def wide_model(numeric_input, category_input, vocabs):
transpose_category_input = tf.transpose(category_input)
category_sum = None
# Append embadding category to numeric_sum
for i in range(0, len(vocabs)):
embedding = tf.get_variable("wideem" + str(i), [vocabs[i], 8],
initializer=tf.contrib.layers.xavier_initializer()
#partitioner=tf.fixed_size_partitioner(n_pss))
#partitioner=tf.min_max_variable_partitioner(n_pss, 0, 2 << 10)
)
# Pick one column from category input
col = tf.gather(transpose_category_input, [i])[0]
#col = tf.nn.embedding_lookup(transpose_category_input, [i])[0]
# Same as make [0001]*[w1,w2,w3,w4] = lookup w4
#embedded_col = embedding_lookup(tf.identity(embedding), col) # number * embedding output number
embedded_col = embedding_ops.embedding_lookup_unique(embedding, col)
if category_sum is None:
category_sum = embedded_col
else:
category_sum = tf.concat([category_sum, embedded_col], 1)
tf.set_random_seed(1)
w = tf.get_variable("W", [numeric_input.shape[1] + category_sum.shape[1], 1], initializer=tf.contrib.layers.xavier_initializer())
wmodel_logits_sum = tf.matmul(tf.concat([numeric_input, category_sum], 1), w)
return wmodel_logits_sum
开发者ID:ShifuML,项目名称:shifu,代码行数:28,代码来源:wnp_ssgd_not_embadding.py
示例5: loss
def loss(self, logits, labels):
"""Adds loss ops to the computational graph.
Hint: Use sparse_softmax_cross_entropy_with_logits
Hint: Remember to add l2_loss (see tf.nn.l2_loss)
Args:
logits: tensor(num_nodes, output_size)
labels: python list, len = num_nodes
Returns:
loss: tensor 0-D
"""
loss = None
# YOUR CODE HERE
labels = tf.convert_to_tensor(labels, dtype=tf.int64)
softmax_loss = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, labels)
l2 = self.config.l2
with tf.variable_scope('Composition', reuse=True):
W1 = tf.get_variable("W1")
with tf.variable_scope('Projection', reuse=True):
U = tf.get_variable("U")
l2_loss = tf.nn.l2_loss(W1) + tf.nn.l2_loss(U)
l2_loss *= l2
loss = tf.reduce_sum(softmax_loss) + l2_loss
# END YOUR CODE
return loss
开发者ID:h1bernate,项目名称:cs224d,代码行数:27,代码来源:rnn_tuner_l2.py
示例6: init
def init():
d_model = 512
d_k = 64
d_v = 64
sequence_length =6 #5
decoder_sent_length=6
h = 8
batch_size = 4*32
num_layer=6
# 2.set Q,K,V
vocab_size = 1000
embed_size = d_model
initializer = tf.random_normal_initializer(stddev=0.1)
Embedding = tf.get_variable("Embedding_d", shape=[vocab_size, embed_size], initializer=initializer)
decoder_input_x = tf.placeholder(tf.int32, [batch_size, decoder_sent_length], name="input_x") # [4,10]
print("1.decoder_input_x:", decoder_input_x)
decoder_input_embedding = tf.nn.embedding_lookup(Embedding, decoder_input_x) # [batch_size*sequence_length,embed_size]
#Q = embedded_words # [batch_size*sequence_length,embed_size]
#K_s = embedded_words # [batch_size*sequence_length,embed_size]
#K_v_encoder = tf.placeholder(tf.float32, [batch_size,decoder_sent_length, d_model], name="input_x") #sequence_length
Q = tf.placeholder(tf.float32, [batch_size,sequence_length, d_model], name="input_x")
K_s=decoder_input_embedding
K_v_encoder= tf.get_variable("v_variable",shape=[batch_size,decoder_sent_length, d_model],initializer=initializer) #tf.float32,
print("2.output from encoder:",K_v_encoder)
mask = get_mask(decoder_sent_length) #sequence_length
decoder = Decoder(d_model, d_k, d_v, sequence_length, h, batch_size, Q, K_s, K_v_encoder,decoder_sent_length,mask=mask,num_layer=num_layer)
return decoder,Q, K_s
开发者ID:AmjadHisham,项目名称:text_classification,代码行数:27,代码来源:a2_decoder.py
示例7: _initialize_weights
def _initialize_weights(self):
all_weights = dict()
# Encoding layers
for i, n_hidden in enumerate(self.hidden_units):
weight_name = 'encoder%d_W' % i
bias_name = 'encoder%d_b' % i
if i == 0:
weight_shape = [self.n_input, n_hidden]
else:
weight_shape = [self.hidden_units[i-1], n_hidden]
all_weights[weight_name] = tf.get_variable(weight_name, weight_shape,
initializer=tf.contrib.layers.xavier_initializer())
all_weights[bias_name] = tf.get_variable(bias_name, [n_hidden],
initializer=tf.constant_initializer(0.0))
# Decoding layers
hidden_units_rev = self.hidden_units[::-1]
for i, n_hidden in enumerate(hidden_units_rev):
weight_name = 'decoder%d_W' % i
bias_name = 'decoder%d_b' % i
if i != len(hidden_units_rev) - 1: # not the last layer
weight_shape = [n_hidden, hidden_units_rev[i+1]]
else:
weight_shape = [n_hidden, self.n_input]
all_weights[weight_name] = tf.get_variable(weight_name, weight_shape,
initializer=tf.contrib.layers.xavier_initializer())
all_weights[bias_name] = tf.get_variable(bias_name, [n_hidden],
initializer=tf.constant_initializer(0.0))
return all_weights
开发者ID:wangz10,项目名称:tensorflow-playground,代码行数:32,代码来源:autoencoders.py
示例8: _batch_norm
def _batch_norm(x, name, is_train):
""" Apply a batch normalization layer. """
with tf.variable_scope(name):
inputs_shape = x.get_shape()
axis = list(range(len(inputs_shape) - 1))
param_shape = int(inputs_shape[-1])
moving_mean = tf.get_variable('mean', [param_shape], initializer=tf.constant_initializer(0.0), trainable=False)
moving_var = tf.get_variable('variance', [param_shape], initializer=tf.constant_initializer(1.0), trainable=False)
beta = tf.get_variable('offset', [param_shape], initializer=tf.constant_initializer(0.0))
gamma = tf.get_variable('scale', [param_shape], initializer=tf.constant_initializer(1.0))
control_inputs = []
def mean_var_with_update():
mean, var = tf.nn.moments(x, axis)
update_moving_mean = moving_averages.assign_moving_average(moving_mean, mean, 0.995)
update_moving_var = moving_averages.assign_moving_average(moving_var, var, 0.995)
control_inputs = [update_moving_mean, update_moving_var]
return tf.identity(mean), tf.identity(var)
def mean_var():
mean = moving_mean
var = moving_var
return tf.identity(mean), tf.identity(var)
mean, var = tf.cond(is_train, mean_var_with_update, mean_var)
with tf.control_dependencies(control_inputs):
normed = tf.nn.batch_normalization(x, mean, var, beta, gamma, 1e-4)
return normed
开发者ID:kisslotus,项目名称:image_captioning,代码行数:33,代码来源:nn.py
示例9: __init__
def __init__(self, epsilon=1e-2, shape=()):
self._sum = tf.get_variable(
dtype=tf.float64,
shape=shape,
initializer=tf.constant_initializer(0.0),
name="runningsum", trainable=False)
self._sumsq = tf.get_variable(
dtype=tf.float64,
shape=shape,
initializer=tf.constant_initializer(epsilon),
name="runningsumsq", trainable=False)
self._count = tf.get_variable(
dtype=tf.float64,
shape=(),
initializer=tf.constant_initializer(epsilon),
name="count", trainable=False)
self.shape = shape
self.mean = tf.to_float(self._sum / self._count)
self.std = tf.sqrt( tf.maximum( tf.to_float(self._sumsq / self._count) - tf.square(self.mean) , 1e-2 ))
newsum = tf.placeholder(shape=self.shape, dtype=tf.float64, name='sum')
newsumsq = tf.placeholder(shape=self.shape, dtype=tf.float64, name='var')
newcount = tf.placeholder(shape=[], dtype=tf.float64, name='count')
self.incfiltparams = U.function([newsum, newsumsq, newcount], [],
updates=[tf.assign_add(self._sum, newsum),
tf.assign_add(self._sumsq, newsumsq),
tf.assign_add(self._count, newcount)])
开发者ID:IcarusTan,项目名称:baselines,代码行数:29,代码来源:mpi_running_mean_std.py
示例10: add_projection
def add_projection(self, rnn_outputs):
"""Adds a projection layer.
The projection layer transforms the hidden representation to a distribution
over the vocabulary.
Hint: Here are the dimensions of the variables you will need to
create
U: (hidden_size, len(vocab))
b_2: (len(vocab),)
Args:
rnn_outputs: List of length num_steps, each of whose elements should be
a tensor of shape (batch_size, embed_size).
Returns:
outputs: List of length num_steps, each a tensor of shape
(batch_size, len(vocab)
"""
### YOUR CODE HERE
with tf.name_scope('Projection Layer'):
U = tf.get_variable('U', [self.config.hidden_size, len(self.vocab)])
b2 = tf.get_variable('b2', len(self.vocab))
outputs = [tf.nn.softmax(tf.matmul(o,U)+b2) for o in rnn_outputs]
### END YOUR CODE
return outputs
开发者ID:jingshuangliu22,项目名称:cs224d_assignment2,代码行数:26,代码来源:q3_RNNLM.py
示例11: logistic_regression
def logistic_regression(X, y, class_weight=None):
"""Creates logistic regression TensorFlow subgraph.
Args:
X: tensor or placeholder for input features,
shape should be [batch_size, n_features].
y: tensor or placeholder for target,
shape should be [batch_size, n_classes].
class_weight: tensor, [n_classes], where for each class
it has weight of the class. If not provided
will check if graph contains tensor `class_weight:0`.
If that is not provided either all ones are used.
Returns:
Predictions and loss tensors.
"""
with tf.variable_scope('logistic_regression'):
tf.histogram_summary('logistic_regression.X', X)
tf.histogram_summary('logistic_regression.y', y)
weights = tf.get_variable('weights', [X.get_shape()[1],
y.get_shape()[-1]])
bias = tf.get_variable('bias', [y.get_shape()[-1]])
tf.histogram_summary('logistic_regression.weights', weights)
tf.histogram_summary('logistic_regression.bias', bias)
# If no class weight provided, try to retrieve one from pre-defined
# tensor name in the graph.
if not class_weight:
try:
class_weight = tf.get_default_graph().get_tensor_by_name('class_weight:0')
except KeyError:
pass
return softmax_classifier(X, y, weights, bias,
class_weight=class_weight)
开发者ID:JuliusKunze,项目名称:tensorflow,代码行数:33,代码来源:models.py
示例12: testVarOpScopeOuterScope
def testVarOpScopeOuterScope(self):
with self.test_session():
with tf.variable_scope("outer") as outer:
pass
with tf.variable_op_scope([], outer, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"outer/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer_1/scope2/")
with tf.variable_op_scope([], None, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"outer/default/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer_1/default/scope2/")
with tf.variable_op_scope([], outer, "default", reuse=True):
self.assertEqual(tf.get_variable("w", []).name,
"outer/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer_2/scope2/")
outer.reuse_variables()
with tf.variable_op_scope([], None, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"outer/default/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer_2/default/scope2/")
开发者ID:285219011,项目名称:hello-world,代码行数:26,代码来源:variable_scope_test.py
示例13: testVarOpScopeReuseParam
def testVarOpScopeReuseParam(self):
with self.test_session():
with tf.variable_scope("outer") as outer:
with tf.variable_op_scope([], "tower", "default"):
self.assertEqual(tf.get_variable("w", []).name,
"outer/tower/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer/tower/scope2/")
with tf.variable_op_scope([], None, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"outer/default/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer/default/scope2/")
with tf.variable_scope(outer) as outer:
with tf.variable_op_scope([], "tower", "default", reuse=True):
self.assertEqual(tf.get_variable("w", []).name,
"outer/tower/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer_1/tower/scope2/")
outer.reuse_variables()
with tf.variable_op_scope([], None, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"outer/default/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "outer_1/default/scope2/")
开发者ID:285219011,项目名称:hello-world,代码行数:26,代码来源:variable_scope_test.py
示例14: testVarOpScope
def testVarOpScope(self):
with self.test_session():
with tf.name_scope("scope1"):
with tf.variable_op_scope([], "tower", "default"):
self.assertEqual(tf.get_variable("w", []).name,
"tower/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "scope1/tower/scope2/")
with tf.variable_op_scope([], "tower", "default"):
with self.assertRaises(ValueError):
tf.get_variable("w", [])
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "scope1/tower_1/scope2/")
with tf.name_scope("scope2"):
with tf.variable_op_scope([], None, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"default/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "scope2/default/scope2/")
with tf.variable_op_scope([], None, "default"):
self.assertEqual(tf.get_variable("w", []).name,
"default_1/w:0")
with tf.name_scope("scope2") as sc2:
self.assertEqual(sc2, "scope2/default_1/scope2/")
开发者ID:285219011,项目名称:hello-world,代码行数:25,代码来源:variable_scope_test.py
示例15: project_bilstm_layer
def project_bilstm_layer(self, lstm_outputs, name=None):
"""
hidden layer between lstm layer and logits
:param lstm_outputs: [batch_size, num_steps, emb_size]
:return: [batch_size, num_steps, num_tags]
"""
with tf.variable_scope("project" if not name else name):
with tf.variable_scope("hidden"):
W = tf.get_variable("W", shape=[self.hidden_unit * 2, self.hidden_unit],
dtype=tf.float32, initializer=self.initializers.xavier_initializer())
b = tf.get_variable("b", shape=[self.hidden_unit], dtype=tf.float32,
initializer=tf.zeros_initializer())
output = tf.reshape(lstm_outputs, shape=[-1, self.hidden_unit * 2])
hidden = tf.tanh(tf.nn.xw_plus_b(output, W, b))
# project to score of tags
with tf.variable_scope("logits"):
W = tf.get_variable("W", shape=[self.hidden_unit, self.num_labels],
dtype=tf.float32, initializer=self.initializers.xavier_initializer())
b = tf.get_variable("b", shape=[self.num_labels], dtype=tf.float32,
initializer=tf.zeros_initializer())
pred = tf.nn.xw_plus_b(hidden, W, b)
return tf.reshape(pred, [-1, self.seq_length, self.num_labels])
开发者ID:chongp,项目名称:Name-Entity-Recognition,代码行数:26,代码来源:lstm_crf_layer.py
示例16: testLSTMBasicToBlockPeeping
def testLSTMBasicToBlockPeeping(self):
with self.test_session(use_gpu=self._use_gpu) as sess:
batch_size = 2
input_size = 3
cell_size = 4
sequence_length = 5
inputs = []
for _ in range(sequence_length):
inp = tf.convert_to_tensor(
np.random.randn(batch_size, input_size),
dtype=tf.float32)
inputs.append(inp)
initializer = tf.random_uniform_initializer(-0.01, 0.01, seed=19890212)
with tf.variable_scope("basic", initializer=initializer):
cell = tf.nn.rnn_cell.LSTMCell(cell_size,
use_peepholes=True,
state_is_tuple=True)
outputs, _ = tf.nn.rnn(cell, inputs, dtype=tf.float32)
sess.run([tf.initialize_all_variables()])
basic_outputs = sess.run(outputs)
basic_grads = sess.run(tf.gradients(outputs, inputs))
basic_wgrads = sess.run(tf.gradients(outputs, tf.trainable_variables()))
with tf.variable_scope("block", initializer=initializer):
w = tf.get_variable("w",
shape=[input_size + cell_size, cell_size * 4],
dtype=tf.float32)
b = tf.get_variable("b",
shape=[cell_size * 4],
dtype=tf.float32,
initializer=tf.zeros_initializer)
wci = tf.get_variable("wci", shape=[cell_size], dtype=tf.float32)
wcf = tf.get_variable("wcf", shape=[cell_size], dtype=tf.float32)
wco = tf.get_variable("wco", shape=[cell_size], dtype=tf.float32)
_, _, _, _, _, _, outputs = fused_lstm(
tf.convert_to_tensor(sequence_length,
dtype=tf.int64),
inputs,
w,
b,
wci=wci,
wcf=wcf,
wco=wco,
cell_clip=0,
use_peephole=True)
sess.run([tf.initialize_all_variables()])
block_outputs = sess.run(outputs)
block_grads = sess.run(tf.gradients(outputs, inputs))
block_wgrads = sess.run(tf.gradients(outputs, [w, b, wci, wcf, wco]))
self.assertAllClose(basic_outputs, block_outputs)
self.assertAllClose(basic_grads, block_grads)
for basic, block in zip(basic_wgrads, block_wgrads):
self.assertAllClose(basic, block, rtol=1e-2, atol=1e-2)
开发者ID:10imaging,项目名称:tensorflow,代码行数:60,代码来源:lstm_ops_test.py
示例17: add_logits_op
def add_logits_op(self):
"""
Adds logits to self
"""
with tf.variable_scope("bi-lstm"):
lstm_fwrd_cell = tf.contrib.rnn.LSTMCell(self.hidden_size)
lstm_back_cell = tf.contrib.rnn.LSTMCell(self.hidden_size)
(output_fw, output_bw), _ = tf.nn.bidirectional_dynamic_rnn(lstm_fwrd_cell,
lstm_back_cell,
self.word_embeddings,
sequence_length=self.sequence_lengths,
dtype=tf.float32)
output = tf.concat([output_fw, output_bw], axis=-1)
output = tf.nn.dropout(output, self.dropout)
with tf.variable_scope("proj"):
W = tf.get_variable("W", shape=[2*self.hidden_size, self.ntags],
dtype=tf.float32)
b = tf.get_variable("b", shape=[self.ntags], dtype=tf.float32,
initializer=tf.zeros_initializer())
ntime_steps = tf.shape(output)[1]
output = tf.reshape(output, [-1, 2*self.hidden_size])
pred = tf.matmul(output, W) + b
self.logits = tf.reshape(pred, [-1, ntime_steps, self.ntags])
开发者ID:yyf013932,项目名称:tensormsa,代码行数:26,代码来源:neuralnet_node_bilstmcrf.py
示例18: ternarize
def ternarize(x, thresh=0.05):
"""
Implemented Trained Ternary Quantization:
https://arxiv.org/abs/1612.01064
Code modified from the authors' at:
https://github.com/czhu95/ternarynet/blob/master/examples/Ternary-Net/ternary.py
"""
shape = x.get_shape()
thre_x = tf.stop_gradient(tf.reduce_max(tf.abs(x)) * thresh)
w_p = tf.get_variable('Wp', initializer=1.0, dtype=tf.float32)
w_n = tf.get_variable('Wn', initializer=1.0, dtype=tf.float32)
tf.summary.scalar(w_p.op.name + '-summary', w_p)
tf.summary.scalar(w_n.op.name + '-summary', w_n)
mask = tf.ones(shape)
mask_p = tf.where(x > thre_x, tf.ones(shape) * w_p, mask)
mask_np = tf.where(x < -thre_x, tf.ones(shape) * w_n, mask_p)
mask_z = tf.where((x < thre_x) & (x > - thre_x), tf.zeros(shape), mask)
@tf.custom_gradient
def _sign_mask(x):
return tf.sign(x) * mask_z, lambda dy: dy
w = _sign_mask(x)
w = w * mask_np
tf.summary.histogram(w.name, w)
return w
开发者ID:quanlzheng,项目名称:tensorpack,代码行数:33,代码来源:dorefa.py
示例19: Linear
def Linear(args, output_dim, bias=True, bias_init=0.0, scope=None):
if not isinstance(args, (list, tuple)):
args = [args]
input_dim = 0
shapes = [a.get_shape().as_list() for a in args]
for shape in shapes:
if len(shape) != 2:
raise ValueError("Linear is expecting 2d arguments: %s" % str(shapes))
elif not shape[1]:
raise ValueError("Linear expects shape[1] of arguments: %s" % str(shapes))
else:
input_dim += shape[1]
with tf.variable_scope(scope or "linear"):
W = tf.get_variable("W", (input_dim, output_dim))
if len(args) == 1:
result = tf.matmul(args[0], W)
else:
result = tf.matmul(tf.concat(1, args), W)
if not bias:
return result
b = tf.get_variable("b", (output_dim,),
initializer=tf.constant_initializer(bias_init))
return result + b
开发者ID:hans,项目名称:thinstack-rl,代码行数:29,代码来源:blocks.py
示例20: tf_baseline_conv2d
def tf_baseline_conv2d():
import tensorflow as tf
import cntk.contrib.crosstalk.crosstalk_tensorflow as crtf
ci = crtf.instance
tf.reset_default_graph()
x = tf.placeholder(tf.float32, [batch_size, num_chars, char_emb_dim])
filter_bank = tf.get_variable("char_filter_bank",
shape=[filter_width, char_emb_dim, num_filters],
dtype=tf.float32)
bias = tf.get_variable("char_filter_biases", shape=[num_filters], dtype=tf.float32)
char_conv = tf.expand_dims(tf.transpose(tf.nn.conv1d(x, filter_bank, stride=1, padding='VALID') + bias, perm=[0,2,1]), -1)
ci.watch(cstk.Conv2DArgs(W=crtf.find_trainable('char_filter_bank'), b=crtf.find_trainable('char_filter_biases')), 'conv2d', var_type=cstk.Conv2DAttr,
attr=cstk.Conv2DAttr(filter_shape=(filter_width, char_emb_dim,), num_filters=num_filters))
ci.watch(char_conv, 'conv2d_out', var_type=crtf.VariableType) # note the output is transposed to NCHW
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
data = {x:input_data}
ci.set_workdir(workdir)
ci.set_data(sess, data)
ci.fetch('conv2d_out', save=True)
ci.fetch('conv2d', save=True)
ci.assign('conv2d', load=True)
assert ci.compare('conv2d_out')
ci.reset()
sess.close()
开发者ID:AllanYiin,项目名称:CNTK,代码行数:30,代码来源:conv2d_test.py
注:本文中的tensorflow.get_variable函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论