本文整理匯總了Python中tensorflow.python.ops.rnn_cell.LSTMCell方法的典型用法代碼示例。如果您正苦於以下問題:Python rnn_cell.LSTMCell方法的具體用法?Python rnn_cell.LSTMCell怎麽用?Python rnn_cell.LSTMCell使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類tensorflow.python.ops.rnn_cell
的用法示例。
在下文中一共展示了rnn_cell.LSTMCell方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: __init__
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def __init__(self,
num_units,
forget_bias=1.0,
use_peephole=False,
use_compatible_names=False):
"""Initialize the basic LSTM cell.
Args:
num_units: int, The number of units in the LSTM cell.
forget_bias: float, The bias added to forget gates (see above).
use_peephole: Whether to use peephole connections or not.
use_compatible_names: If True, use the same variable naming as
rnn_cell.LSTMCell
"""
self._num_units = num_units
self._forget_bias = forget_bias
self._use_peephole = use_peephole
if use_compatible_names:
self._names = {
"W": "W_0",
"b": "B",
"wci": "W_I_diag",
"wco": "W_O_diag",
"wcf": "W_F_diag",
"scope": "LSTMCell"
}
else:
self._names = {
"W": "W",
"b": "b",
"wci": "wci",
"wco": "wco",
"wcf": "wcf",
"scope": "LSTMBlockCell"
}
示例2: __init__
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def __init__(self, num_units, use_peepholes=False, forget_bias=1.0):
super(Grid1LSTMCell, self).__init__(
num_units=num_units, num_dims=1,
input_dims=0, output_dims=0, priority_dims=0,
cell_fn=lambda n, i: rnn_cell.LSTMCell(
num_units=n, input_size=i, use_peepholes=use_peepholes,
forget_bias=forget_bias, state_is_tuple=False))
示例3: _build_pre
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def _build_pre(self):
self.dimA = 20
self.cellA = MultiRNNCell([LSTMCell(self.dimA)] * 2)
self.b1 = 0.95
self.b2 = 0.95
self.lr = 0.1
self.eps = 1e-8
示例4: _build_pre
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def _build_pre(self):
self.dimH = 20
self.cellH = MultiRNNCell([LSTMCell(self.dimH)] * 2)
self.lr = 0.1
示例5: _create_rnn_cell
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def _create_rnn_cell(self):
cell = GRUCell(self.cfg.num_units) if self.cfg.cell_type == "gru" else LSTMCell(self.cfg.num_units)
if self.cfg.use_dropout:
cell = DropoutWrapper(cell, output_keep_prob=self.keep_prob)
if self.cfg.use_residual:
cell = ResidualWrapper(cell)
return cell
示例6: __init__
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def __init__(self, num_units, cell_type='lstm', scope='bi_rnn'):
self.cell_fw = LSTMCell(num_units) if cell_type == 'lstm' else GRUCell(num_units)
self.cell_bw = LSTMCell(num_units) if cell_type == 'lstm' else GRUCell(num_units)
self.scope = scope
示例7: _create_single_rnn_cell
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def _create_single_rnn_cell(self, num_units):
cell = GRUCell(num_units) if self.cfg["cell_type"] == "gru" else LSTMCell(num_units)
return cell
示例8: _create_single_rnn_cell
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def _create_single_rnn_cell(self, num_units):
cell = GRUCell(num_units) if self.cfg["cell_type"] == "gru" else LSTMCell(num_units)
if self.cfg["use_dropout"]:
cell = DropoutWrapper(cell, output_keep_prob=self.rnn_keep_prob)
if self.cfg["use_residual"]:
cell = ResidualWrapper(cell)
return cell
示例9: __init__
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def __init__(self, num_units, cell_type='lstm', scope=None):
self.cell_fw = GRUCell(num_units) if cell_type == 'gru' else LSTMCell(num_units)
self.cell_bw = GRUCell(num_units) if cell_type == 'gru' else LSTMCell(num_units)
self.scope = scope or "bi_rnn"
示例10: __init__
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def __init__(self, num_units, scope='bi_rnn'):
self.num_units = num_units
self.cell_fw = LSTMCell(self.num_units)
self.cell_bw = LSTMCell(self.num_units)
self.scope = scope
示例11: _build_model
# 需要導入模塊: from tensorflow.python.ops import rnn_cell [as 別名]
# 或者: from tensorflow.python.ops.rnn_cell import LSTMCell [as 別名]
def _build_model(self):
with tf.variable_scope("embeddings"):
self.source_embs = tf.get_variable(name="source_embs", shape=[self.cfg.source_vocab_size, self.cfg.emb_dim],
dtype=tf.float32, trainable=True)
self.target_embs = tf.get_variable(name="embeddings", shape=[self.cfg.vocab_size, self.cfg.emb_dim],
dtype=tf.float32, trainable=True)
source_emb = tf.nn.embedding_lookup(self.source_embs, self.enc_source)
target_emb = tf.nn.embedding_lookup(self.target_embs, self.dec_target_in)
print("source embedding shape: {}".format(source_emb.get_shape().as_list()))
print("target input embedding shape: {}".format(target_emb.get_shape().as_list()))
with tf.variable_scope("encoder"):
if self.cfg.use_bi_rnn:
with tf.variable_scope("bi-directional_rnn"):
cell_fw = GRUCell(self.cfg.num_units) if self.cfg.cell_type == "gru" else \
LSTMCell(self.cfg.num_units)
cell_bw = GRUCell(self.cfg.num_units) if self.cfg.cell_type == "gru" else \
LSTMCell(self.cfg.num_units)
bi_outputs, _ = bidirectional_dynamic_rnn(cell_fw, cell_bw, source_emb, dtype=tf.float32,
sequence_length=self.enc_seq_len)
source_emb = tf.concat(bi_outputs, axis=-1)
print("bi-directional rnn output shape: {}".format(source_emb.get_shape().as_list()))
input_project = tf.layers.Dense(units=self.cfg.num_units, dtype=tf.float32, name="input_projection")
source_emb = input_project(source_emb)
print("encoder input projection shape: {}".format(source_emb.get_shape().as_list()))
enc_cells = self._create_encoder_cell()
self.enc_outputs, self.enc_states = dynamic_rnn(enc_cells, source_emb, sequence_length=self.enc_seq_len,
dtype=tf.float32)
print("encoder output shape: {}".format(self.enc_outputs.get_shape().as_list()))
with tf.variable_scope("decoder"):
self.max_dec_seq_len = tf.reduce_max(self.dec_seq_len, name="max_dec_seq_len")
self.dec_cells, self.dec_init_states = self._create_decoder_cell()
# define input and output projection layer
input_project = tf.layers.Dense(units=self.cfg.num_units, name="input_projection")
self.dense_layer = tf.layers.Dense(units=self.cfg.vocab_size, name="output_projection")
if self.mode == "train": # either "train" or "decode"
# for training
target_emb = input_project(target_emb)
train_helper = TrainingHelper(target_emb, sequence_length=self.dec_seq_len, name="train_helper")
train_decoder = BasicDecoder(self.dec_cells, helper=train_helper, output_layer=self.dense_layer,
initial_state=self.dec_init_states)
self.dec_output, _, _ = dynamic_decode(train_decoder, impute_finished=True,
maximum_iterations=self.max_dec_seq_len)
print("decoder output shape: {} (vocab size)".format(self.dec_output.rnn_output.get_shape().as_list()))
# for decode
start_token = tf.ones(shape=[self.batch_size, ], dtype=tf.int32) * self.cfg.target_dict[GO]
end_token = self.cfg.target_dict[EOS]
def inputs_project(inputs):
return input_project(tf.nn.embedding_lookup(self.target_embs, inputs))
dec_helper = GreedyEmbeddingHelper(embedding=inputs_project, start_tokens=start_token,
end_token=end_token)
infer_decoder = BasicDecoder(self.dec_cells, helper=dec_helper, initial_state=self.dec_init_states,
output_layer=self.dense_layer)
infer_dec_output, _, _ = dynamic_decode(infer_decoder, maximum_iterations=self.cfg.maximum_iterations)
self.dec_predicts = infer_dec_output.sample_id