sequence_length = 2
input_size = 3
hidden_size = 8000
num_layers = 2
# num_classes = len(mentions)
batch_size = 300
num_epochs = 1000
learning_rate = 0.01
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers):
super(RNN, 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)
# self.fc = nn.Linear(hidden_size, num_classes)
def forward(self, x):
# Set initial hidden and cell states
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size).to(device)
c0 = torch.zeros(self.num_layers, 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)
# Decode the hidden state of the last time step
return out
model = RNN(input_size, hidden_size, num_layers).to(device)
print(model)
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)
# Train the model
total_step = len(train_loader)
for epoch in range(num_epochs):
for i, batch in enumerate(train_loader):
features = batch[0]
labels = batch[1]
features = features.reshape(4,10,3) # only if features's shape is not this already, put the value of n here!!
labels = labels.reshape(1,4,10) # only if labels's shape is not this already
outputs = model(features)
loss = criterion(outputs, labels)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if (i + 1) % 100 == 0:
print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch + 1, num_epochs, i + 1, total_step, loss.item()))