如何将输入提供给pytorch lstm图层

时间:2018-05-21 14:14:35

标签: python neural-network lstm pytorch

我是PyTorch的新手,并且不了解如何使用此框架适应网络。

我在Keras有一个简单的模型:

model = Sequential()
model.add(Embedding(vocab_size, embedding_size, input_length=55, weights=[pretrained_weights]))
model.add(Bidirectional(LSTM(units=len(X_train))))
model.add(Dense(n_classes, activation='softmax'))
model.compile(loss='categorical_crossentropy',
          optimizer = RMSprop(lr=0.0005),
          metrics=['accuracy'])

model.fit(np.array(X_train), np.array(y_train), epochs=100, validation_data=(np.array(X_val), np.array(y_val)))

在Keras中,我可以使用我的X_train(包含索引的2D数组)和我的y_train(包含每个输入的单个索引的2D数组)来提供网络。

现在,为了提供我的PyTorch模型,我将矩阵转换为这样的张量:

M = torch.tensor(X_train)

并定义了我的网络:

# Bidirectional recurrent neural network (many-to-one)
class BiRNN(nn.Module):
  def __init__(self, input_size, hidden_size, num_layers, num_classes):
    super(BiRNN, self).__init__()
    self.hidden_size = hidden_size
    self.num_layers = num_layers
    self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True)
    self.fc = nn.Linear(hidden_size*2, num_classes)  # 2 for bidirection

  def forward(self, x):
    # Set initial states
    h0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device) # 2 for bidirection 
    c0 = torch.zeros(self.num_layers*2, x.size(0), self.hidden_size).to(device)

    # Forward propagate LSTM
    out, _ = self.lstm(x, (h0, c0))  # out: tensor of shape (batch_size, seq_length, hidden_size*2)

    # Decode the hidden state of the last time step
    out = self.fc(out[:, -1, :])
    return out

model = BiRNN(input_size, hidden_size, num_layers, num_classes).to(device)

# Loss and optimizer
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

但我不明白的是如何调用该函数并使用我自己的数据进行预测。

0 个答案:

没有答案