我想用7个特征预测一个变量,时间步长为4:
# Shape X_train: torch.Size([24433, 4, 7]
# Shape Y_train: torch.Size([24433, 4, 1]
# Shape X_test: torch.Size([6109, 4, 7]
# Shape Y_test: torch.Size([6109, 4, 1]
train_dataset = TensorDataset(X_train, Y_train)
test_dataset = TensorDataset(X_test, Y_test)
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True)
test_loader = torch.utils.data.DataLoader(test_dataset, batch_size=32, shuffle=False)
- 我的(初始)LSTM模型:**
class LSTMModel(nn.Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.lstm = nn.LSTM(input_size, hidden_size)
self.linear = nn.Linear(hidden_size, output_size)
def forward(self, x):
x, _ = self.lstm(x)
x = self.linear(x)
return x
model = LSTMModel(input_size=7, hidden_size=256, output_size=1)
loss_fn = nn.MSELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.1)
- 适用型号:**
# Loop over the training set
for X, Y in train_loader:
optimizer.zero_grad()
Y_pred = model(X)
loss = loss_fn(Y_pred, Y)
loss.backward()
optimizer.step()
model.eval()
# Loop over the test set
for X, Y in test_loader:
Y_pred = model(X)
loss = loss_fn(Y_pred, Y)
- Y(真实数据)示例:**
tensor([[[59.],
[59.],
[59.],
[59.]],
[[70.],
[70.],
[70.],
[70.]],
[[ 100.],
[ 0.],
[ 0.],
[ 0.]],
# etc.
- 但是,我的Y_pred有点像这样:**
tensor([[[15.8224],
[15.8224],
[15.8224],
[15.8224]],
[[16.1654],
[16.1654],
[16.1654],
[16.1654]],
[[16.2127],
[16.2127],
[16.2127],
[16.2127]],
# etc.
- 我尝试过无数种方法**
- 更改模型架构(不同批量、不同层数)
- 添加压差和衰减参数
- 在训练和测试数据上循环时使用历元并更改历元数
- 具有不同学习率的不同优化器(Adam、SGD)
- 对输入数据进行日志转换
Examples of my data in a previous question.
我是PyTorch和LSTM的新手,所以我可能会做错,但是,无论我做什么改变,我总是从预测中得到一个(接近)常量的值。我做错了什么/我应该做什么?
1条答案
按热度按时间lp0sw83n1#
我通过对输入数据进行归一化来解决这个问题,现在我可以对每个输出进行不同的预测: