本文整理汇总了Python中tensorflow.tanh函数的典型用法代码示例。如果您正苦于以下问题:Python tanh函数的具体用法?Python tanh怎么用?Python tanh使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了tanh函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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)
示例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
示例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)
示例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
示例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
示例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
示例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
示例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.
示例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
示例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
示例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
示例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.
示例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
示例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
示例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