赞
踩
我有一列数据想做时间序列预测,有30000+行,用pytorch框架完成,输入是(input_length)时间步,输出是(ouput_length)时间步,数据是csv文件,请帮我写一个框架,完成训练。
- import pandas as pd
- import torch
- import torch.nn as nn
- import torch.optim as optim
- from torch.utils.data import Dataset, DataLoader
-
- class TimeSeriesDataset(Dataset):
- def __init__(self, csv_file, input_length, output_length):
- self.data = pd.read_csv(csv_file)
- self.input_length = input_length
- self.output_length = output_length
-
- def __len__(self):
- return len(self.data) - self.input_length - self.output_length + 1
-
- def __getitem__(self, idx):
- input_data = self.data.iloc[idx:idx+self.input_length].values
- output_data = self.data.iloc[idx+self.input_length:idx+self.input_length+self.output_length].values
- return torch.tensor(input_data, dtype=torch.float), torch.tensor(output_data, dtype=torch.float)
-
- class TimeSeriesModel(nn.Module):
- def __init__(self, input_size, output_size, hidden_size, num_layers):
- super(TimeSeriesModel, self).__init__()
- self.lstm = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
- self.fc = nn.Linear(hidden_size, output_size)
-
- def forward(self, x):
- lstm_out, _ = self.lstm(x)
- output = self.fc(lstm_out[:, -1, :])
- return output
-
- def train(model, train_loader, optimizer, criterion, device):
- model.train()
- train_loss = 0.0
- for inputs, targets in train_loader:
- inputs, targets = inputs.to(device), targets.to(device)
- optimizer.zero_grad()
- outputs = model(inputs)
- loss = criterion(outputs, targets)
- loss.backward()
- optimizer.step()
- train_loss += loss.item()
- return train_loss / len(train_loader)
-
- def validate(model, val_loader, criterion, device):
- model.eval()
- val_loss = 0.0
- with torch.no_grad():
- for inputs, targets in val_loader:
- inputs, targets = inputs.to(device), targets.to(device)
- outputs = model(inputs)
- loss = criterion(outputs, targets)
- val_loss += loss.item()
- return val_loss / len(val_loader)
-
- def main():
- # Hyperparameters
- input_length = 10
- output_length = 5
- batch_size = 32
- hidden_size = 64
- num_layers = 2
- learning_rate = 0.001
- num_epochs = 100
-
- # Load the data
- train_dataset = TimeSeriesDataset('train.csv', input_length, output_length)
- val_dataset = TimeSeriesDataset('val.csv', input_length, output_length)
- train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
- val_loader = DataLoader(val_dataset, batch_size=batch_size, shuffle=False)
-
- # Initialize the model
- input_size = train_dataset[0][0].shape[-1]
- output_size = train_dataset[0][1].shape[-1]
- model = TimeSeriesModel(input_size, output_size, hidden_size, num_layers)
-
- # Use GPU if available
- device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
- model.to(device)
-
- # Set up the optimizer and loss function
- optimizer = optim.Adam(model.parameters(), lr=learning_rate)
- criterion = nn.MSELoss()
-
- # Train the model
- for epoch in range(num_epochs):
- train_loss = train(model, train_loader, optimizer, criterion, device)
- val_loss = validate(model, val_loader, criterion, device)
- print('Epoch [{}/{}], Train Loss: {:.4f}, Validation Loss: {:.4f}'.format(epoch+1, num_epochs, train_loss, val_loss))
-
- # Save the model
- torch.save(model.state_dict(), 'model.pth')
-
- if __name__ == '__main__':
- main()
此框架使用pandas
加载CSV文件,并创建了一个TimeSeriesDataset
类来处理数据,该类将一系列时间步作为输入,并将接下来的一系列时间步作为输出,以便进行时间序列预测。TimeSeriesModel
类定义了一个LSTM模型,其输入和输出大小取决于数据集中的输入长度和输出长度。训练和验证功能被定义为train
和validate
函数,它们分别计算给定数据集上的平均损失。最后,在main
函数中,我们使用训练数据集和验证数据集来训练模型,并保存训练后的模型。
请完成加载训练结束保存的模型,进行推理部分的代码
- import torch
- from torch import nn
-
- # 定义模型结构
- class Model(nn.Module):
- def __init__(self):
- super(Model, self).__init__()
- self.fc1 = nn.Linear(10, 5)
- self.relu = nn.ReLU()
- self.fc2 = nn.Linear(5, 1)
-
- def forward(self, x):
- x = self.fc1(x)
- x = self.relu(x)
- x = self.fc2(x)
- return x
-
- # 构建模型实例
- model = Model()
-
- # 加载保存的模型参数
- model.load_state_dict(torch.load('saved_model.pth'))
-
- # 设置模型为推理模式
- model.eval()
-
- # 构造一个输入样本
- input = torch.randn(1, 10)
-
- # 进行推理
- output = model(input)
-
- print(output)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。