本文整理汇总了Python中tensorflow.tanh函数的典型用法代码示例。如果您正苦于以下问题:Python tanh函数的具体用法?Python tanh怎么用?Python tanh使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tanh函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: __call__
def __call__(self, x, state, scope=None):
with tf.variable_scope(scope or type(self).__name__):
c, h = state
# Keep W_xh and W_hh separate here as well to reuse initialization methods
x_size = x.get_shape().as_list()[1]
print x.get_shape().as_list()
W_xh = tf.get_variable('W_xh',
[x_size, 4 * self.num_units],
initializer=orthogonal_initializer())
W_hh = tf.get_variable('W_hh',
[self.num_units, 4 * self.num_units],
initializer=bn_lstm_identity_initializer(0.95))
bias = tf.get_variable('bias', [4 * self.num_units])
# hidden = tf.matmul(x, W_xh) + tf.matmul(h, W_hh) + bias
# improve speed by concat.
concat = tf.concat(1, [x, h])
W_both = tf.concat(0, [W_xh, W_hh])
hidden = tf.matmul(concat, W_both) + bias
i, j, f, o = tf.split(1, 4, hidden)
new_c = c * tf.sigmoid(f) + tf.sigmoid(i) * tf.tanh(j)
new_h = tf.tanh(new_c) * tf.sigmoid(o)
return new_h, (new_c, new_h)
开发者ID:darksigma,项目名称:Fundamentals-of-Deep-Learning-Book,代码行数:27,代码来源:lstm.py
示例2: model_encoder_decoder
def model_encoder_decoder(encoder_inputs, world_state_vectors, batch_size):
h_encoder,c1,h1 = encoder(encoder_inputs)
U_V_precalc = precalc_Ux_Vh(encoder_inputs,h_encoder)
## Decoder loop
with tf.name_scope('Decoder') as scope:
# Initial states
s_t = tf.tanh( tf.matmul(h1,w_trans_s)+b_trans_s , name='s_0')
c_t = tf.tanh( tf.matmul(c1,w_trans_c)+b_trans_c , name='c_0')
# Definition of the cell computation.
logits = [] # logits per rolling
predictions = []
for i in xrange(self._decoder_unrollings):
# world state vector at step i
y_t = world_state_vectors[i] # batch_size x num_local_feats (feat_id format)
# embeed world vector | relu nodes
ey = tf.nn.relu(tf.matmul(y_t,w_emby) + b_emby, name='Ey')
# context vector
z_t = context_vector(s_t,h_encoder,U_V_precalc,encoder_inputs,batch_size)
# Dropout
ey = tf.nn.dropout(ey, keep_prob)
s_t,c_t = decoder_cell(ey,s_t,z_t,c_t)
s_t = tf.nn.dropout(s_t, keep_prob)
# Hidden linear layer before output, proyects z_t,y_t, and s_t to an embeeding-size layer
hq = ey + tf.matmul(s_t,ws) + tf.matmul(z_t,wz) + b_q
# Output layer
logit = tf.matmul(hq,wo) + b_o
prediction = tf.nn.softmax(logit,name='prediction')
logits.append(logit)
predictions.append(prediction)
#END-FOR-DECODER-UNROLLING
#END-DECODER-SCOPE
return logits,predictions
开发者ID:ronaldahmed,项目名称:robot-navigation,代码行数:34,代码来源:nav_model_batch.py
示例3: decoder_body
def decoder_body(time, old_state, output_ta_t, attention_tracker):
if feedback:
def from_previous():
prev_1 = tf.matmul(old_state, W_out) + b_out
return tf.gather(embeddings, tf.argmax(prev_1, 1))
x_t = tf.cond(tf.greater(time, 0), from_previous, lambda: input_ta.read(0))
else:
x_t = input_ta.read(time)
# attention
part2 = tf.matmul(old_state, W_a) + b_a
part2 = tf.expand_dims(part2, 1)
john = part1 + part2
e = tf.reduce_sum(v_a * tf.tanh(john), [2])
alpha = tf.nn.softmax(e)
alpha = tf.to_float(mask(attention_lengths)) * alpha
alpha = alpha / tf.reduce_sum(alpha, [1], keep_dims=True)
attention_tracker = attention_tracker.write(time, alpha)
context = tf.reduce_sum(tf.expand_dims(alpha, 2) * tf.squeeze(hidden), [1])
# GRU
con = tf.concat(1, [x_t, old_state, context])
z = tf.sigmoid(tf.matmul(con, W_z) + b_z)
r = tf.sigmoid(tf.matmul(con, W_r) + b_r)
con = tf.concat(1, [x_t, r*old_state, context])
c = tf.tanh(tf.matmul(con, W_c) + b_c)
new_state = (1-z)*c + z*old_state
output_ta_t = output_ta_t.write(time, new_state)
return (time + 1, new_state, output_ta_t, attention_tracker)
开发者ID:alrojo,项目名称:TensorFlowTutorial,代码行数:31,代码来源:tf_utils.py
示例4: build
def build(self, inp):
"""Build LSTM graph.
Args:
inp: input, state.
Returns:
results: state.
"""
self.lazy_init_var()
x = inp['input']
state = inp['state']
with tf.variable_scope(self.scope):
c = tf.slice(state, [0, 0, 0, 0], [-1, -1, -1, self.hid_depth])
h = tf.slice(state, [0, 0, 0, self.hid_depth],
[-1, -1, -1, self.hid_depth])
g_i = tf.sigmoid(Conv2D(self.w_xi)(x) +
Conv2D(self.w_hi)(h) + self.b_i)
g_f = tf.sigmoid(Conv2D(self.w_xf)(x) +
Conv2D(self.w_hf)(h) + self.b_f)
g_o = tf.sigmoid(Conv2D(self.w_xo)(x) +
Conv2D(self.w_ho)(h) + self.b_o)
u = tf.tanh(Conv2D(self.w_xu)(x) +
Conv2D(self.w_hu)(h) + self.b_u)
c = g_f * c + g_i * u
h = g_o * tf.tanh(c)
state = tf.concat(3, [c, h])
return state
开发者ID:ziyu-zhang,项目名称:tfplus,代码行数:30,代码来源:conv_lstm.py
示例5: sample_inference
def sample_inference(state,model_input,model_sample_params):
p = model_sample_params
lstm_input = tf.tanh(parallel_batch_mvx(p['linear_input'],model_input))
state,lstm_output = lstm_rollout(state,lstm_input,p['lstm_layer'])
mean = parallel_batch_mvx(p['linear_output_mean'],tf.tanh(lstm_output)) * 0.05
return state,mean
开发者ID:CurtisHuebner,项目名称:SMP3.0,代码行数:7,代码来源:train_model.py
示例6: __call__
def __call__(self, inputs, state, scope=None):
"""Long short-term memory cell (LSTM)."""
with tf.variable_scope(self, scope or "basic_lstm_cell", reuse=self._reuse):
# Parameters of gates are concatenated into one multiply for
# efficiency.
if self._state_is_tuple:
c_prev, h_prev = state
else:
c_prev, h_prev = tf.split(
value=state, num_or_size_splits=2, axis=1)
concat = tf.contrib.rnn._linear(
[inputs, h_prev], 4 * self._num_units, True)
# i = input_gate, g = new_input, f = forget_gate, o = output_gate
i, g, f, o = tf.split(value=concat, num_or_size_splits=4, axis=1)
c = (c_prev * tf.sigmoid(f + self._forget_bias) +
tf.sigmoid(i) * tf.tanh(g))
h = tf.tanh(c) * tf.sigmoid(o)
if self._state_is_tuple:
new_state = LSTMStateTuple(c, h)
else:
new_state = tf.concat([c, h], 1)
return h, new_state
开发者ID:seasky100,项目名称:tensorflow_end2end_speech_recognition,代码行数:25,代码来源:basic_lstm.py
示例7: build_graph
def build_graph(input_size, minibatch_size):
flat_size = conv1_features * input_size//2 * input_size//2
inputs = tf.placeholder(tf.float32, shape=[minibatch_size, input_size, input_size, input_channels], name='inputs')
labels = tf.placeholder(tf.float32, shape=[minibatch_size], name='labels')
with tf.name_scope('conv1') as scope:
conv1_init = tf.truncated_normal([conv1_size, conv1_size, input_channels, conv1_features], stddev=random_init_stddev, dtype=tf.float32)
conv1_weights = tf.Variable(conv1_init, name='weights')
conv1_bias = tf.Variable(tf.zeros([conv1_features], dtype=tf.float32), name='bias')
conv1_y = tf.nn.conv2d(inputs, conv1_weights, [1, conv1_stride, conv1_stride, 1], padding='SAME', name='y')
conv1_biased = tf.nn.bias_add(conv1_y, conv1_bias)
conv1_activity = tf.tanh(conv1_biased , name='activity')
with tf.name_scope('output') as scope:
output_init = tf.truncated_normal([flat_size, output_features], stddev=random_init_stddev, dtype=tf.float32)
output_weights = tf.Variable(output_init, name='weights')
output_bias = tf.Variable(tf.zeros([output_features], dtype=tf.float32), name='bias')
conv1_flat = tf.reshape(conv1_activity, [minibatch_size, flat_size])
output_y = tf.matmul(conv1_flat, output_weights, name='y')
output_raw = tf.nn.bias_add(output_y, output_bias)
output_tanh = tf.tanh(output_raw)
output = tf.reshape(output_tanh, [minibatch_size])
with tf.name_scope('loss') as scope:
minibatch_loss = tf.squared_difference(labels, output)
loss = tf.reduce_mean(minibatch_loss)
tf.scalar_summary('loss', loss)
return inputs, labels, output, loss
开发者ID:andykitchen,项目名称:nn-detect,代码行数:33,代码来源:rr_graph.py
示例8: __call__
def __call__(self, inputs, state, scope=None):
with tf.device("/gpu:"+str(self._gpu_for_layer)):
"""JZS1, mutant 1 with n units cells."""
with tf.variable_scope(scope or type(self).__name__): # "JZS1Cell"
with tf.variable_scope("Zinput"): # Reset gate and update gate.
# We start with bias of 1.0 to not reset and not update.
'''equation 1 z = sigm(WxzXt+Bz), x_t is inputs'''
z = tf.sigmoid(linear([inputs],
self._num_units, True, 1.0, weight_initializer = self._weight_initializer, orthogonal_scale_factor = self._orthogonal_scale_factor))
with tf.variable_scope("Rinput"):
'''equation 2 r = sigm(WxrXt+Whrht+Br), h_t is the previous state'''
r = tf.sigmoid(linear([inputs,state],
self._num_units, True, 1.0, weight_initializer = self._weight_initializer, orthogonal_scale_factor = self._orthogonal_scale_factor))
'''equation 3'''
with tf.variable_scope("Candidate"):
component_0 = linear([r*state],
self._num_units, True)
component_1 = tf.tanh(tf.tanh(inputs) + component_0)
component_2 = component_1*z
component_3 = state*(1 - z)
h_t = component_2 + component_3
return h_t, h_t #there is only one hidden state output to keep track of.
开发者ID:tonydeep,项目名称:tensorflow_with_latest_papers,代码行数:27,代码来源:rnn_cell_modern.py
示例9: lnlstm
def lnlstm(xs, ms, s, scope, nh, init_scale=1.0):
nbatch, nin = [v.value for v in xs[0].get_shape()]
with tf.variable_scope(scope):
wx = tf.get_variable("wx", [nin, nh*4], initializer=ortho_init(init_scale))
gx = tf.get_variable("gx", [nh*4], initializer=tf.constant_initializer(1.0))
bx = tf.get_variable("bx", [nh*4], initializer=tf.constant_initializer(0.0))
wh = tf.get_variable("wh", [nh, nh*4], initializer=ortho_init(init_scale))
gh = tf.get_variable("gh", [nh*4], initializer=tf.constant_initializer(1.0))
bh = tf.get_variable("bh", [nh*4], initializer=tf.constant_initializer(0.0))
b = tf.get_variable("b", [nh*4], initializer=tf.constant_initializer(0.0))
gc = tf.get_variable("gc", [nh], initializer=tf.constant_initializer(1.0))
bc = tf.get_variable("bc", [nh], initializer=tf.constant_initializer(0.0))
c, h = tf.split(axis=1, num_or_size_splits=2, value=s)
for idx, (x, m) in enumerate(zip(xs, ms)):
c = c*(1-m)
h = h*(1-m)
z = _ln(tf.matmul(x, wx), gx, bx) + _ln(tf.matmul(h, wh), gh, bh) + b
i, f, o, u = tf.split(axis=1, num_or_size_splits=4, value=z)
i = tf.nn.sigmoid(i)
f = tf.nn.sigmoid(f)
o = tf.nn.sigmoid(o)
u = tf.tanh(u)
c = f*c + i*u
h = o*tf.tanh(_ln(c, gc, bc))
xs[idx] = h
s = tf.concat(axis=1, values=[c, h])
return xs, s
开发者ID:MrGoogol,项目名称:baselines,代码行数:31,代码来源:utils.py
示例10: __call__
def __call__(self, inputs, state, timestep = 0, scope=None):
"""Most basic RNN: output = new_state = tanh(W * input + U * state + B)."""
current_state = state
for highway_layer in xrange(self.num_highway_layers):
with tf.variable_scope('highway_factor_'+str(highway_layer)):
if self.use_inputs_on_each_layer or highway_layer == 0:
highway_factor = tf.tanh(multiplicative_integration([inputs, current_state], self._num_units))
else:
highway_factor = tf.tanh(linear([current_state], self._num_units, True))
with tf.variable_scope('gate_for_highway_factor_'+str(highway_layer)):
if self.use_inputs_on_each_layer or highway_layer == 0:
gate_for_highway_factor = tf.sigmoid(multiplicative_integration([inputs, current_state], self._num_units, initial_bias_value = -3.0))
else:
gate_for_highway_factor = tf.sigmoid(linear([current_state], self._num_units, True, -3.0))
gate_for_hidden_factor = 1 - gate_for_highway_factor
if self.use_recurrent_dropout and self.is_training:
highway_factor = tf.nn.dropout(highway_factor, self.recurrent_dropout_factor)
current_state = highway_factor * gate_for_highway_factor + current_state * gate_for_hidden_factor
return current_state, current_state
开发者ID:Ahndaehwan,项目名称:tensorflow_with_latest_papers,代码行数:25,代码来源:rnn_cell_mulint_modern.py
示例11: build_generator
def build_generator(self):
image = tf.placeholder(tf.float32, [self.batch_size, self.dim_image])
question = tf.placeholder(tf.int32, [self.batch_size, self.max_words_q])
state = tf.zeros([self.batch_size, self.stacked_lstm.state_size])
loss = 0.0
for i in range(max_words_q):
if i==0:
ques_emb_linear = tf.zeros([self.batch_size, self.input_embedding_size])
else:
tf.get_variable_scope().reuse_variables()
ques_emb_linear = tf.nn.embedding_lookup(self.embed_ques_W, question[:,i-1])
ques_emb_drop = tf.nn.dropout(ques_emb_linear, 1-self.drop_out_rate)
ques_emb = tf.tanh(ques_emb_drop)
output, state = self.stacked_lstm(ques_emb, state)
# multimodal (fusing question & image)
state_drop = tf.nn.dropout(state, 1-self.drop_out_rate)
state_linear = tf.nn.xw_plus_b(state_drop, self.embed_state_W, self.embed_state_b)
state_emb = tf.tanh(state_linear)
image_drop = tf.nn.dropout(image, 1-self.drop_out_rate)
image_linear = tf.nn.xw_plus_b(image_drop, self.embed_image_W, self.embed_image_b)
image_emb = tf.tanh(image_linear)
scores = tf.mul(state_emb, image_emb)
scores_drop = tf.nn.dropout(scores, 1-self.drop_out_rate)
scores_emb = tf.nn.xw_plus_b(scores_drop, self.embed_scor_W, self.embed_scor_b)
# FINAL ANSWER
generated_ANS = tf.nn.xw_plus_b(scores_drop, self.embed_scor_W, self.embed_scor_b)
return generated_ANS, image, question
开发者ID:JamesChuanggg,项目名称:VQA-tensorflow,代码行数:35,代码来源:model_VQA.py
示例12: __call__
def __call__(self, x, state, scope=None):
with tf.variable_scope(scope or type(self).__name__):
c, h = tf.split(state, 2, 1)
x_size = x.get_shape().as_list()[1]
w_init = None # uniform
h_init = lstm_ortho_initializer(1.0)
# Keep W_xh and W_hh separate here as well to use different init methods.
w_xh = tf.get_variable(
'W_xh', [x_size, 4 * self.num_units], initializer=w_init)
w_hh = tf.get_variable(
'W_hh', [self.num_units, 4 * self.num_units], initializer=h_init)
bias = tf.get_variable(
'bias', [4 * self.num_units],
initializer=tf.constant_initializer(0.0))
concat = tf.concat([x, h], 1)
w_full = tf.concat([w_xh, w_hh], 0)
hidden = tf.matmul(concat, w_full) + bias
i, j, f, o = tf.split(hidden, 4, 1)
if self.use_recurrent_dropout:
g = tf.nn.dropout(tf.tanh(j), self.dropout_keep_prob)
else:
g = tf.tanh(j)
new_c = c * tf.sigmoid(f + self.forget_bias) + tf.sigmoid(i) * g
new_h = tf.tanh(new_c) * tf.sigmoid(o)
return new_h, tf.concat([new_c, new_h], 1) # fuk tuples.
开发者ID:ThierryGrb,项目名称:magenta,代码行数:34,代码来源:rnn.py
示例13: lstm_cell
def lstm_cell(i, o, state):
input_gate = tf.sigmoid(tf.matmul(i, ix) + tf.matmul(o, im) + ib)
forget_gate = tf.sigmoid(tf.matmul(i, fx) + tf.matmul(o, fm) + fb)
update = tf.matmul(i, cx) + tf.matmul(o, cm) + cb
state = forget_gate * state + input_gate * tf.tanh(update)
output_gate = tf.sigmoid(tf.matmul(i, ox) + tf.matmul(o, om) + ob)
return output_gate * tf.tanh(state), state
开发者ID:leafsummer,项目名称:keeplearning,代码行数:7,代码来源:rnn_unsupervised.py
示例14: lstm_cell
def lstm_cell(x, h, c, name=None, reuse=False):
"""LSTM returning hidden state and content cell at a specific timestep."""
nin = x.shape[-1].value
nout = h.shape[-1].value
with tf.variable_scope(name, default_name="lstm",
values=[x, h, c], reuse=reuse):
wx = tf.get_variable("kernel/input", [nin, nout * 4],
dtype=tf.float32,
initializer=tf.orthogonal_initializer(1.0))
wh = tf.get_variable("kernel/hidden", [nout, nout * 4],
dtype=tf.float32,
initializer=tf.orthogonal_initializer(1.0))
b = tf.get_variable("bias", [nout * 4],
dtype=tf.float32,
initializer=tf.constant_initializer(0.0))
z = tf.matmul(x, wx) + tf.matmul(h, wh) + b
i, f, o, u = tf.split(z, 4, axis=1)
i = tf.sigmoid(i)
f = tf.sigmoid(f + 1.0)
o = tf.sigmoid(o)
u = tf.tanh(u)
c = f * c + i * u
h = o * tf.tanh(c)
return h, c
开发者ID:JoyceYa,项目名称:edward,代码行数:25,代码来源:lstm.py
示例15: build_init_cell
def build_init_cell(self):
with tf.variable_scope("init_cell"):
# always zero
dummy = tf.placeholder(tf.float32, [1, 1], name='dummy')
# memory
M_init_linear = tf.tanh(Linear(dummy, self.mem_size * self.mem_dim, name='M_init_linear'))
M_init = tf.reshape(M_init_linear, [self.mem_size, self.mem_dim])
# read weights
read_w_init = tf.Variable(tf.zeros([self.read_head_size, self.mem_size]))
read_init = tf.Variable(tf.zeros([self.read_head_size, 1, self.mem_dim]))
for idx in xrange(self.read_head_size):
# initialize bias distribution with `tf.range(mem_size-2, 0, -1)`
read_w_linear_idx = Linear(dummy, self.mem_size, is_range=True,
name='read_w_linear_%s' % idx)
read_w_init = tf.scatter_update(read_w_init, [idx], tf.nn.softmax(read_w_linear_idx))
read_init_idx = tf.tanh(Linear(dummy, self.mem_dim, name='read_init_%s' % idx))
read_init = tf.scatter_update(read_init, [idx], tf.reshape(read_init_idx, [1, 1, self.mem_dim]))
# write weights
write_w_init = tf.Variable(tf.zeros([self.write_head_size, self.mem_size]))
for idx in xrange(self.write_head_size):
write_w_linear_idx = Linear(dummy, self.mem_size, is_range=True,
name='write_w_linear_%s' % idx)
write_w_init = tf.scatter_update(write_w_init, [idx], tf.nn.softmax(write_w_linear_idx))
# controller state
output_init = tf.Variable(tf.zeros([self.controller_layer_size, self.controller_dim]))
hidden_init = tf.Variable(tf.zeros([self.controller_layer_size, self.controller_dim]))
for idx in xrange(self.controller_layer_size):
output_init = tf.scatter_update(output_init, [idx], tf.reshape(
tf.tanh(Linear(dummy, self.controller_dim, name='output_init_%s' % idx)),
[1, self.controller_dim]
)
)
hidden_init = tf.scatter_update(hidden_init, [idx], tf.reshape(
tf.tanh(Linear(dummy, self.controller_dim, name='hidden_init_%s' % idx)),
[1, self.controller_dim]
)
)
new_output= tf.tanh(Linear(dummy, self.output_dim, name='new_output'))
inputs = {
'input': dummy,
}
outputs = {
'new_output': new_output,
'M': M_init,
'read_w': read_w_init,
'write_w': write_w_init,
'read': tf.reshape(read_init, [self.read_head_size, self.mem_dim]),
'output': output_init,
'hidden': hidden_init
}
return inputs, outputs
开发者ID:ramtej,项目名称:NTM-tensorflow,代码行数:60,代码来源:model.py
示例16: build_node
def build_node(self, x_in, c_in, h_in, scope="lstm_cell"):
#print (x_in, c_in, h_in, scope)
#print [type(thing) for thing in (x_in, c_in, h_in, scope)]
# print [(item.name, item.dtype) for thing in (h_in, c_in) for item in thing]
# print (x_in.name, x_in.dtype)
with tf.variable_scope(scope):
# print x.shape
# print h_in.get_shape()
x_with_h = tf.concat(2, [x_in, h_in])
ones_for_bias = tf.constant(np.ones([batch_size,1,1]), name="b", dtype=tf.float32)
x_h_concat = tf.concat(2, [ones_for_bias, x_with_h])
# forget gate layer
# print "w_f: ", self.w_f.get_shape()
# print "x_h_concat: ", x_h_concat.get_shape()
f = tf.sigmoid(tf.batch_matmul(x_h_concat, self.w_f))
# candidate values
i = tf.sigmoid(tf.batch_matmul(x_h_concat, self.w_i))
candidate_c = tf.tanh(tf.batch_matmul(x_h_concat, self.w_c))
# new cell state (hidden)
# forget old values of c
old_c_to_keep = tf.mul(f, c_in)
# scaled candidate values of c
new_c_to_keep = tf.mul(i, candidate_c)
c = tf.add(old_c_to_keep, new_c_to_keep)
# new scaled output
o = tf.sigmoid(tf.batch_matmul(x_h_concat, self.w_o))
h = tf.mul(o, tf.tanh(c))
return (c, h)
开发者ID:liangkai,项目名称:char-rnn-tf,代码行数:34,代码来源:rnn.py
示例17: lstm_cell
def lstm_cell(i, o, state):
"""
Create a LSTM cell. See e.g.: http://arxiv.org/pdf/1402.1128v1.pdf
Note that in this formulation, we omit the various connections between the
previous state and the gates.
"""
i_list = tf.pack([i, i, i, i])
#print i_list.get_shape().as_list()
o_list = tf.pack([o, o, o, o])
ins = tf.batch_matmul(i_list, fico_x)
outs = tf.batch_matmul(o_list, fico_m)
h_x = ins + outs + fico_b
#print h_x.get_shape().as_list()
#forget_gate = tf.sigmoid(tf.matmul(i, fx) + tf.matmul(o, fm) + fb)
forget_gate = tf.sigmoid(h_x[0,:,:])
#input_gate = tf.sigmoid(tf.matmul(i, ix) + tf.matmul(o, im) + ib)
input_gate = tf.sigmoid(h_x[1,:,:])
#update = tf.tanh(tf.matmul(i, cx) + tf.matmul(o, cm) + cb)
update = tf.tanh(h_x[2,:,:])
state = forget_gate*state + input_gate*update
#output_gate = tf.sigmoid(tf.matmul(i, ox) + tf.matmul(o, om) + ob)
output_gate = tf.sigmoid(h_x[3,:,:])
h = output_gate * tf.tanh(state)
#print 'h', h.get_shape().as_list()
return h, state
开发者ID:kcbighuge,项目名称:tensorflow-deeplearning,代码行数:33,代码来源:6_lstm.py
示例18: __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.
c, h = tf.split(1, 2, state)
concat = linear.linear([inputs, h], 4 * self._num_units, True)
fs = []
# This can be made more efficient since we're doing more than needs to be
# done, but for now w/e
for child_state in child_states:
c_k, h_k = tf.split(1, 2, child_state)
concat = linear.linear([inputs, h_k], 4 * self._num_units, True)
i_k, j_k, f_k, o_k = tf.split(1, 4, concat)
fs.append(f_k)
# i = input_gate, j = new_input, f = forget_gate, o = output_gate
# TODO: forget gate for each child, probably need to split by number
# of child states or something
i, j, f, o = tf.split(1, 4, concat)
# If no children just treat it like a regular lstm
if not fs:
fs.append(f)
new_c = sum(c * tf.sigmoid(fs + self._forget_bias)) + tf.sigmoid(i) * tf.tanh(j)
new_h = tf.tanh(new_c) * tf.sigmoid(o)
return new_h, tf.concat(1, [new_c, new_h])
开发者ID:StevenLOL,项目名称:LSTMRelatedness,代码行数:31,代码来源:treelstm.py
示例19: buildNeuralNet
def buildNeuralNet(inputNodes, hiddenLayers, outputNodes):
layers = []
weights = []
x = tf.placeholder(tf.float32, shape=[None, 3])
# Input Layer
weightsInput = tf.Variable(
tf.random_normal([3, inputNodes], name="InputWeights"))
layerInput = tf.tanh(tf.matmul(x, weightsInput))
weights.append(weightsInput)
layers.append(layerInput)
# Hidden Layer
for layer in range(1, hiddenLayers + 1):
name = "HiddenWeights" + str(layer)
weightsHidden = tf.Variable(tf.random_normal([inputNodes,
inputNodes],
name=name))
layerHidden = tf.tanh(tf.matmul(layers[-1], weightsHidden))
weights.append(weightsHidden)
layers.append(layerHidden)
# Output Layer
weightsOutput = tf.Variable(
tf.random_normal([inputNodes, outputNodes],
name="OutputWeights"))
y = tf.sigmoid(tf.matmul(layers[-1], weightsOutput))
weights.append(weightsOutput)
layers.append(y)
return x, layers, weights
开发者ID:caux,项目名称:ImageGen-tf,代码行数:35,代码来源:NeuralNet.py
示例20: lstm_step
def lstm_step(tensors, state):
# TODO group linear operations for more efficiency
with tf.variable_scope("lstm"):
x, = tensors
h = state["h"]
c = state["c"]
assert is_tensor(x)
assert is_tensor(h)
assert is_tensor(c)
num_units = get_shape_values(h)[-1]
assert get_shape_values(c)[-1] == num_units
forget_logit = add_bias("forget_bias",
linear("forget_x", x, num_units) +
linear("forget_h", h, num_units))
input_logit = add_bias("input_bias",
linear("input_x", x, num_units) +
linear("input_h", h, num_units))
output_logit = add_bias("output_bias",
linear("output_x", x, num_units) +
linear("output_h", h, num_units))
update_logit = add_bias("update_bias",
linear("update_x", x, num_units) +
linear("update_h", h, num_units))
f = tf.nn.sigmoid(forget_logit)
i = tf.nn.sigmoid(input_logit)
o = tf.nn.sigmoid(output_logit)
u = tf.tanh(update_logit)
new_c = f * c + i * u
new_h = tf.tanh(new_c) * o
return {"h": new_h, "c": new_c}
开发者ID:diogo149,项目名称:tensorflow_utils,代码行数:30,代码来源:tf_utils.py
注:本文中的tensorflow.tanh函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论