本文整理汇总了Python中mxnet.gluon.rnn.LSTM属性的典型用法代码示例。如果您正苦于以下问题:Python rnn.LSTM属性的具体用法?Python rnn.LSTM怎么用?Python rnn.LSTM使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类mxnet.gluon.rnn
的用法示例。
在下文中一共展示了rnn.LSTM属性的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, vocab_size, tag2idx, embedding_dim, hidden_dim):
super(BiLSTM_CRF, self).__init__()
with self.name_scope():
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.vocab_size = vocab_size
self.tag2idx = tag2idx
self.tagset_size = len(tag2idx)
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
self.lstm = rnn.LSTM(hidden_dim // 2, num_layers=1, bidirectional=True)
# Maps the output of the LSTM into tag space.
self.hidden2tag = nn.Dense(self.tagset_size)
# Matrix of transition parameters. Entry i,j is the score of
# transitioning *to* i *from* j.
self.transitions = self.params.get("crf_transition_matrix",
shape=(self.tagset_size, self.tagset_size))
self.hidden = self.init_hidden()
示例2: hybrid_forward
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def hybrid_forward(self,
F: ModuleType,
x: nd_sym_type,
*args, **kwargs) -> nd_sym_type:
"""
Used for forward pass through LSTM middleware network.
Applies dense layers from selected scheme before passing result to LSTM layer.
:param F: backend api, either `mxnet.nd` or `mxnet.sym` (if block has been hybridized).
:param x: state embedding, of shape (batch_size, in_channels).
:return: state middleware embedding, where shape is (batch_size, channels).
"""
x_ntc = x.reshape(shape=(0, 0, -1))
emb_ntc = super(LSTMMiddleware, self).hybrid_forward(F, x_ntc, *args, **kwargs)
emb_tnc = emb_ntc.transpose(axes=(1, 0, 2))
return self.lstm(emb_tnc)
示例3: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, vocab_size, tag2idx, embedding_dim, hidden_dim,START_TAG = "<START>",STOP_TAG = "<STOP>",ctx=mx.cpu()):
super(BiLSTM_CRF, self).__init__()
with self.name_scope():
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.vocab_size = vocab_size
self.tag2idx = tag2idx
self.START_TAG = START_TAG
self.STOP_TAG = STOP_TAG
self.tagset_size = len(tag2idx)
self.ctx = ctx
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
self.lstm = rnn.LSTM(hidden_dim // 2, num_layers=1, bidirectional=True)
self.hidden2tag = nn.Dense(self.tagset_size)
self.transitions = nd.random.normal(shape=(self.tagset_size, self.tagset_size),ctx=ctx)
self.hidden = self.init_hidden()
示例4: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, vocab_size, tag2idx, embedding_dim, hidden_dim):
super(BiLSTM_CRF, self).__init__()
with self.name_scope():
self.embedding_dim = embedding_dim
self.hidden_dim = hidden_dim
self.vocab_size = vocab_size
self.tag2idx = tag2idx
self.tagset_size = len(tag2idx)
self.word_embeds = nn.Embedding(vocab_size, embedding_dim)
self.lstm = rnn.LSTM(hidden_dim // 2, num_layers=1, bidirectional=True)
# Maps the output of the LSTM into tag space.
self.hidden2tag = nn.Dense(self.tagset_size)
# Matrix of transition parameters. Entry i,j is the score of
# transitioning *to* i *from* j.
self.transitions = nd.random.normal(shape=(self.tagset_size, self.tagset_size))
self.hidden = self.init_hidden()
示例5: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, mode, vocab_size, num_embed, num_hidden,
num_layers, dropout=0.5, tie_weights=False, **kwargs):
super(RNNModel, self).__init__(**kwargs)
with self.name_scope():
self.drop = nn.Dropout(dropout)
self.encoder = nn.Embedding(vocab_size, num_embed,
weight_initializer=mx.init.Uniform(0.1))
if mode == 'rnn_relu':
self.rnn = rnn.RNN(num_hidden, num_layers, dropout=dropout,
input_size=num_embed)
elif mode == 'rnn_tanh':
self.rnn = rnn.RNN(num_hidden, num_layers, 'tanh', dropout=dropout,
input_size=num_embed)
elif mode == 'lstm':
self.rnn = rnn.LSTM(num_hidden, num_layers, dropout=dropout,
input_size=num_embed)
elif mode == 'gru':
self.rnn = rnn.GRU(num_hidden, num_layers, dropout=dropout,
input_size=num_embed)
else:
raise ValueError("Invalid mode %s. Options are rnn_relu, "
"rnn_tanh, lstm, and gru"%mode)
if tie_weights:
self.decoder = nn.Dense(vocab_size, in_units=num_hidden,
params=self.encoder.params)
else:
self.decoder = nn.Dense(vocab_size, in_units=num_hidden)
self.num_hidden = num_hidden
示例6: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, vocab_size=VOCAB_SIZE, embedding_size=32,
rnn_size=128, num_layers=2, drop_rate=0.0, **kwargs):
super(Model, self).__init__(**kwargs)
self.args = {"vocab_size": vocab_size, "embedding_size": embedding_size,
"rnn_size": rnn_size, "num_layers": num_layers,
"drop_rate": drop_rate}
with self.name_scope():
self.encoder = nn.Embedding(vocab_size, embedding_size)
self.dropout = nn.Dropout(drop_rate)
self.rnn = rnn.LSTM(rnn_size, num_layers, dropout=drop_rate,
input_size=embedding_size)
self.decoder = nn.Dense(vocab_size, in_units=rnn_size)
示例7: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, mode, vocab_size, num_embed, num_hidden,
num_layers, dropout=0.5, tie_weights=False, **kwargs):
super(RNNModel, self).__init__(**kwargs)
with self.name_scope():
self.drop = nn.Dropout(dropout)
self.encoder = nn.Embedding(vocab_size, num_embed,
weight_initializer=mx.init.Uniform(0.1))
if mode == 'rnn_relu':
self.rnn = rnn.RNN(num_hidden, 'relu', num_layers, dropout=dropout,
input_size=num_embed)
elif mode == 'rnn_tanh':
self.rnn = rnn.RNN(num_hidden, num_layers, dropout=dropout,
input_size=num_embed)
elif mode == 'lstm':
self.rnn = rnn.LSTM(num_hidden, num_layers, dropout=dropout,
input_size=num_embed)
elif mode == 'gru':
self.rnn = rnn.GRU(num_hidden, num_layers, dropout=dropout,
input_size=num_embed)
else:
raise ValueError("Invalid mode %s. Options are rnn_relu, "
"rnn_tanh, lstm, and gru"%mode)
if tie_weights:
self.decoder = nn.Dense(vocab_size, in_units=num_hidden,
params=self.encoder.params)
else:
self.decoder = nn.Dense(vocab_size, in_units=num_hidden)
self.num_hidden = num_hidden
示例8: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(self, params: LSTMMiddlewareParameters):
"""
LSTMMiddleware or Long Short Term Memory Middleware can be used in the middle part of the network. It takes the
embeddings from the input embedders, after they were aggregated in some method (for example, concatenation)
and passes it through a neural network which can be customizable but shared between the heads of the network.
:param params: parameters object containing batchnorm, activation_function, dropout and
number_of_lstm_cells properties.
"""
super(LSTMMiddleware, self).__init__(params)
self.number_of_lstm_cells = params.number_of_lstm_cells
with self.name_scope():
self.lstm = rnn.LSTM(hidden_size=self.number_of_lstm_cells)
示例9: schemes
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def schemes(self) -> dict:
"""
Schemes are the pre-defined network architectures of various depths and complexities that can be used for the
Middleware. Are used to create Block when LSTMMiddleware is initialised, and are applied before the LSTM.
:return: dictionary of schemes, with key of type MiddlewareScheme enum and value being list of mxnet.gluon.Block.
"""
return {
MiddlewareScheme.Empty:
[],
# Use for PPO
MiddlewareScheme.Shallow:
[
Dense(units=64)
],
# Use for DQN
MiddlewareScheme.Medium:
[
Dense(units=512)
],
MiddlewareScheme.Deep:
[
Dense(units=128),
Dense(units=128),
Dense(units=128)
]
}
示例10: __init__
# 需要导入模块: from mxnet.gluon import rnn [as 别名]
# 或者: from mxnet.gluon.rnn import LSTM [as 别名]
def __init__(
self,
mode: str,
num_hidden: int,
num_layers: int,
bidirectional: bool = False,
**kwargs,
):
super(RNN, self).__init__(**kwargs)
with self.name_scope():
if mode == "rnn_relu":
self.rnn = rnn.RNN(
num_hidden,
num_layers,
bidirectional=bidirectional,
activation="relu",
layout="NTC",
)
elif mode == "rnn_tanh":
self.rnn = rnn.RNN(
num_hidden,
num_layers,
bidirectional=bidirectional,
layout="NTC",
)
elif mode == "lstm":
self.rnn = rnn.LSTM(
num_hidden,
num_layers,
bidirectional=bidirectional,
layout="NTC",
)
elif mode == "gru":
self.rnn = rnn.GRU(
num_hidden,
num_layers,
bidirectional=bidirectional,
layout="NTC",
)
else:
raise ValueError(
"Invalid mode %s. Options are rnn_relu, rnn_tanh, lstm, and gru "
% mode
)