如何在初始化的时候搭建LSTM网络
Created by: Termset
类似pytorch的代码如下:
class LSTMNet(nn.Module):
def __init__(self, input_size, hidden_size,
num_layers=1,
bias=True,
batch_fisrt=True,
bidirectional=True):
super(LSTMNet, self).__init__()
self.lstm = nn.LSTM(
input_size=input_size,
hidden_size=hidden_size,
num_layers=num_layers,
bias=bias,
batch_first=batch_fisrt,
bidirectional=bidirectional
)
def forward(self, input, wh=None, wc=None):
output, (hidden, cell) = self.lstm(input)
return output
而使用paddlepaddle,layers.lstm()需要传入输入值,无法在init时候搭建lstm
class LSTMNet(parl.Model):
def __init__(self, batch_size, hidden_size,
num_layers=1,
bidirectional=True):
super(LSTMNet, self).__init__()
self.bidirectional = bidirectional
self.num_layers = num_layers
self.hidden_size = hidden_size
cell = layers.LSTMCell(hidden_size=hidden_size)
self.batch_size = batch_size
# output = layers.rnn(cell=cell, inputs=, )
# output = layers.
def forward(self, input):
input.stop_gradient = False
self.init_h = layers.fill_constant([self.num_layers * 2, self.batch_size, self.hidden_size], 'float32', 0.0)
self.init_c = layers.fill_constant([self.num_layers * 2, self.batch_size, self.hidden_size], 'float32', 0.0)
output, hidden, cell = layers.lstm(
input=input,
init_h=self.init_h,
init_c=self.init_c,
max_len=100,
is_test=False,
hidden_size=self.hidden_size,
num_layers=self.num_layers,
is_bidirec=self.bidirectional
)
return output
请问这个应该怎么写呢?