Problem
Given a dataset consisting of 48-hour sequence of hospital records and a binary target determining whether the patient survives or not, when the model is given a test sequence of 48 hours record, it needs to predict whether the patient survives or not.
Data
I have constructed a dummy dataset as following:
input_ = torch.randn(100, 48, 76)
target_ = torch.randint(0, 2, (100,))
and loading the training data as following:
trainloader = DataLoader(TensorDataset(input_, target_),
batch_size=50,
shuffle=True)
Model
I have constructed an LSTM based model as following:
class LSTMClassification(nn.Module):
def __init__(self, input_dim, hidden_dim, target_size):
super(LSTMClassification, self).__init__()
self.lstm = nn.LSTM(input_dim, hidden_dim, batch_first=True)
self.fc = nn.Linear(hidden_dim, target_size)
def forward(self, input_):
lstm_out, (h, c) = self.lstm(input_)
logits = self.fc(lstm_out[-1])
scores = F.sigmoid(logits)
return scores
with training function as following:
def train(model, n_epochs):
loss_function = nn.BCEWithLogitsLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1)
history = {
'loss': []
}
for epoch in range(n_epochs):
losses = []
for i, data in enumerate(trainloader, 0):
inputs, labels = data
model.zero_grad()
tag_scores = model(inputs)
labels = labels.unsqueeze(1)
loss = loss_function(tag_scores, labels)
loss.backward()
optimizer.step()
losses.append(float(loss))
avg_loss = np.mean(losses)
history['loss'].append(avg_loss)
print("Epoch {} / {}: Loss = {:.3f}".format(epoch+1, n_epochs, avg_loss))
return history
The model construction is as following:
model = LSTMClassification(input_.shape[2],
hidden_dim=6,
target_size=1)
However, when I train the model, I’m getting an error
ValueError: Target size (torch.Size([50, 1])) must be the same as input size (torch.Size([48, 1]))
I’m not sure how to get my model to yield a tensor of size (50,1) whereby for each group of time series data, it yields an output of 0 or 1.