Я очень новичок в использовании Pytorch, и у меня есть очень важная задача, которую нужно отправить завтра. Может кто-нибудь проверить мою модель классификатора, используя Biconditional LSTM. Я буду очень признателен за помощь этого сообщества.
Я не уверен, особенно в последней части. Подача линейного слоя для получения lo git значений
import torch
import torch.nn as nn
class ClassifierBiLSTM(nn.Module):
"""
The RNN model used to perform classification
"""
def __init__(self, output_size, embedding_dim, hidden_dim, n_layers, seq_len, drop_prob, train_on_gpu=False):
"""
Initialize the model by setting up the layers.
"""
super(ClassifierBiLSTM, self).__init__()
self.output_size = output_size
self.n_layers = n_layers
self.hidden_dim = hidden_dim
self.seq_len = seq_len
self.train_on_gpu = train_on_gpu
# define all layers
self.lstm = nn.LSTM(input_size=embedding_dim, hidden_size=hidden_dim,
num_layers=n_layers, batch_first=True, dropout=drop_prob,
bidirectional=True)
self.dropout = nn.Dropout(drop_prob)
self.clf = nn.Linear(in_features=(hidden_dim * 2), out_features=output_size)
def forward(self, x):
"""
Perform a forward pass of our model on some input and hidden state.
"""
# Set initial states
h0 = torch.zeros(self.n_layers * 2, x.size(0), self.hidden_dim)
c0 = torch.zeros(self.n_layers * 2, x.size(0), self.hidden_dim)
if self.train_on_gpu:
h0 = h0.cuda()
c0 = c0.cuda()
# LSTM forward pass
# Expected output shape at this line: (batch_size, seq length, 2 * hidden dim)
### INSERT CODE HERE ###
x = torch.transpose(x, dim0=1, dim1=0) # Swap batch and sentence dimensions
lstm_out, (hn, cn) = self.lstm(x, (h0, c0))
# to properly separate the output into forward & backward output,
# you might need to use following code. This reshapes LSTM output to
# (batch size, seq length, num directions, hidden dimensions):
lstm_out = lstm_out.contiguous().view(-1, self.seq_len, 2, self.hidden_dim)
# get backward output in first node
### INSERT CODE HERE ###
lstm_out = self.fc(out[:, -1, :])
# get forward output in last node
### INSERT CODE HERE ###
# concatenate FW & BW outputs
### INSERT CODE HERE ###
# apply dropout on concatenated output
### INSERT CODE HERE ###
y = self.hidden2label(self.dropout(torch.cat([c0[i,:, :] for i in range(c0.shape[0])], dim=1)))
# feed to linear layer to get logits values
### INSERT CODE HERE ###
logits = self.linear(y)
return logits