首先,我无缘无故地使用 Python/PyTorch/LSTM。只是好奇而已。 我“认为”我的所有张量都已正确配置。我的窗口大小是 20。我的批量大小是 64。
这些是我将它们输入数据集方法时的形状。
stock_train_tensor shape is: torch.Size([4688, 20, 1])
stock_validate_tensor shape is: torch.Size([1172, 20, 1])
stock_train_target_tensor shape is: torch.Size([4688, 1])
stock_validate_target_tensor shape is: torch.Size([1172, 1])
我的数据集是:
class StockPriceDataSet(Dataset):
def __init__(self, data, targets):
self.data = data
self.targets = targets
def __getitem__(self, index):
x = self.data[index]
y = self.targets[index]
return x, y
def __len__(self):
return len(self.data)
然后我这样做:
training_dataset = StockPriceDataSet(stock_train_tensor, stock_train_target_tensor)
validation_dataset= StockPriceDataSet(stock_validate_tensor, stock_validate_target_tensor)
train_dataloader = DataLoader(training_dataset, batch_size=64, shuffle=False)
validate_dataloader = DataLoader(validation_dataset, batch_size=64, shuffle=False)
我的 LSTM 模型是这样配置的。
lstm = nn.LSTM(input_size=1, hidden_size=64, num_layers=2, batch_first=True)
criterion = nn.MSELoss()
optimizer = optim.SGD(lstm.parameters(),lr=0.01\])
然后当我尝试训练模型时:
for epoch in range(config\["training"\]\["num_epoch"\]):
for i, (x, y) in enumerate(train_dataloader):
output, \_= lstm(x)
y = y.float()
time.sleep(6)
#compute the loss and backpropogate
loss = criterion(output, y) <===== this causes the warning
我收到这个警告:
loss.py:536: UserWarning: Using a target size (torch.Size([64, 1])) that is different to the input size (torch.Size([64, 20, 64])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
我放了一些调试打印语句,这是我在警告消息之前看到的。
output is shape: torch.Size([64, 20, 64])
y is shape: torch.Size([64, 1])
我尝试在 dim=1 中重复目标值,这样我的目标张量也是 64,20。这导致了同样的信息。
loss.py:536: UserWarning: Using a target size (torch.Size([64, 20])) that is different to the input size (torch.Size([64, 20, 64])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
而且,我尝试添加第三个维度并得到这个:
loss.py:536: UserWarning: Using a target size (torch.Size([64, 20, 1])) that is different to the input size (torch.Size([64, 20, 64])). This will likely lead to incorrect results due to broadcasting. Please ensure they have the same size.
任何关于如何调试它的线索将不胜感激。
谢谢。
乔治