import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset class LSTM(nn.Module): def __init__(self, inputDim, hiddenDim, layerNum, batchSize): super(LSTM, self).__init__() self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.inputDim = inputDim self.hiddenDim = hiddenDim self.layerNum = layerNum self.batchSize = batchSize self.lstm = nn.LSTM(inputDim, hiddenDim, layerNum, batch_first = True).to(self.device) self.fc = nn.Linear(hiddenDim, 1).to(self.device) def forward(self, inputData): h0 = torch.zeros(self.layerNum, inputData.size(0), self.hiddenDim, device = inputData.device) c0 = torch.zeros(self.layerNum, inputData.size(0), self.hiddenDim, device = inputData.device) out, hidden = self.lstm(inputData, (h0, c0)) out = self.fc(out[:, -1, :]) return out def SetCriterion(self, func): self.criterion = func def SetOptimizer(self, func): self.optimizer = func def SetLstmTrainData(self, inputData, labelData): data = TensorDataset(inputData.to(device), labelData.to(device)) self.dataloader = DataLoader(data, batch_size = self.batchSize, shuffle = True) def TrainLstmModule(self, epochNum, learnRate, statPeriod): for epoch in range(epochNum): for batch_x, batch_y in self.dataloader: self.optimizer.zero_grad() output = self.forward(batch_x) loss = self.criterion(output, batch_y) loss.backward() self.optimizer.step() if epoch % statPeriod == 0: print("Epoch[{}/{}], loss:{:.6f}".format(epoch + 1, epochNum, loss.item())) def GetLstmModuleTrainRst(self, verifyData): results = [] with torch.no_grad(): output = self.forward(verifyData) results = output.squeeze().tolist() # 将预测结果转换为 Python 列表 return results if __name__ == "__main__": inputDataNum = 100 timeStep = 5 inputDataDim = 10000 labelDataDim = 1 hiddenDataDim = 200 layerNum = 20 trainBatchSize = 100 epochNum = 1 learnRate = 0.01 statPeriod = 1 weightDecay = 0.001 device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = LSTM(inputDataDim, hiddenDataDim, layerNum, trainBatchSize).to(device) model.SetCriterion(nn.MSELoss()) model.SetOptimizer(torch.optim.Adam(model.parameters(), lr = learnRate, weight_decay = weightDecay)) inputData = torch.randn(inputDataNum, timeStep, inputDataDim) labelData = torch.randn(inputDataNum, labelDataDim) verifyData = inputData model.SetLstmTrainData(inputData, labelData) model.TrainLstmModule(epochNum, learnRate, statPeriod) torch.save(model.state_dict(), "lstm_model.pth") model.load_state_dict(torch.load("lstm_model.pth")) model.GetLstmModuleTrainRst(verifyData) 这段代码,为什么output = self.forward(batch_x)总是输出相同的值
时间: 2024-04-08 09:31:38 浏览: 51
浅谈pytorch中torch.max和F.softmax函数的维度解释
5星 · 资源好评率100%
在定的代码中,`output = self.forward(batch_x)`总是输出相同的值的原因可能是因为没有正确设置模型的训练参数和优化器。在`SetOptimizer`方法中,你需要传入一个优化器函数,并将其赋值给`self.optimizer`。同样,在`SetCriterion`方法中,你需要传入一个损失函数并将其赋值给`self.criterion`。
确保在使用`TrainLstmModule`方法之前,已经正确设置了损失函数和优化器。另外,检查一下学习率和权重衰减是否适当。
如果仍然存在问题,请检查数据集是否存在偏差或者模型的超参数是否需要更进一步调整
阅读全文