当前位置:网站首页>Pytorch builds CNN LSTM hybrid model to realize multivariable and multi step time series forecasting (load forecasting)
Pytorch builds CNN LSTM hybrid model to realize multivariable and multi step time series forecasting (load forecasting)
2022-06-26 06:56:00 【Cyril_ KI】
Catalog
I. Preface
About LSTM The specific principle of can refer to : Artificial intelligence tutorial . except LSTM outside , This site also includes specific explanations of most other machine learning and deep learning models , Vivid pictures , Simple and easy to understand .
I have written many articles about time series prediction :
- In depth understanding of PyTorch in LSTM Input and output of ( from input Input to Linear Output )
- PyTorch build LSTM Time series prediction is realized ( Load forecasting )
- PyTorch build LSTM Realize multivariable time series prediction ( Load forecasting )
- PyTorch Build a two-way network LSTM Time series prediction is realized ( Load forecasting )
- PyTorch build LSTM Realize multivariable and multi step time series prediction ( One ): Direct multiple output
- PyTorch build LSTM Realize multivariable and multi step time series prediction ( Two ): Single step rolling prediction
- PyTorch build LSTM Realize multivariable and multi step time series prediction ( 3、 ... and ): Multi model single step prediction
- PyTorch build LSTM Realize multivariable and multi step time series prediction ( Four ): Multi model rolling prediction
- PyTorch build LSTM Realize multivariable and multi step time series prediction ( 5、 ... and ):seq2seq
- PyTorch To realize LSTM Several methods of multi step time series prediction are summarized ( Load forecasting )
- PyTorch-LSTM How to predict the real future value in time series prediction
- PyTorch build LSTM Realize multivariable input and multivariable output time series prediction ( Multi task learning )
- PyTorch build ANN Time series prediction is realized ( Wind speed prediction )
- PyTorch build CNN Time series prediction is realized ( Wind speed prediction )
- PyTorch build CNN-LSTM The hybrid model realizes the prediction of multivariable and multi step time series ( Load forecasting )
All of the above articles use LSTM、ANN as well as CNN Three models are used to predict time series respectively . as everyone knows ,CNN The ability to extract features is very strong , So now many papers will CNN and LSTM Combined with time series prediction . This article will use PyTorch To build a simple CNN-LSTM The hybrid model realizes load forecasting .
II. CNN-LSTM
CNN-LSTM The model is built as follows :
class CNN_LSTM(nn.Module):
def __init__(self, args):
super(CNN_LSTM, self).__init__()
self.args = args
self.relu = nn.ReLU(inplace=True)
# (batch_size=30, seq_len=24, input_size=7) ---> permute(0, 2, 1)
# (30, 7, 24)
self.conv = nn.Sequential(
nn.Conv1d(in_channels=args.in_channels, out_channels=args.out_channels, kernel_size=3),
nn.ReLU(),
nn.MaxPool1d(kernel_size=3, stride=1)
)
# (batch_size=30, out_channels=32, seq_len-4=20) ---> permute(0, 2, 1)
# (30, 20, 32)
self.lstm = nn.LSTM(input_size=args.out_channels, hidden_size=args.hidden_size,
num_layers=args.num_layers, batch_first=True)
self.fc = nn.Linear(args.hidden_size, args.output_size)
def forward(self, x):
x = x.permute(0, 2, 1)
x = self.conv(x)
x = x.permute(0, 2, 1)
x, _ = self.lstm(x)
x = self.fc(x)
x = x[:, -1, :]
return x
You can see , The CNN-LSTM By one-dimensional convolution +LSTM form .
adopt PyTorch build CNN Time series prediction is realized ( Wind speed prediction ) We know , The original definition of one-dimensional convolution is as follows :
nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)
In this paper, the definition of one-dimensional convolution of the model :
nn.Conv1d(in_channels=args.in_channels, out_channels=args.out_channels, kernel_size=3)
here in_channels The concept of is equivalent to... In natural language processing embedding, Therefore, the number of input channels is 7, Indicates load + other 6 Environment variables ;out_channels Can be set at will , This article is set to 32;kernel_size Set to 3.
PyTorch The input size of one-dimensional convolution in is :
input(batch_size, input_size, seq_len)=(30, 7, 24)
The data dimension obtained after data processing is :
input(batch_size, seq_len, input_size)=(30, 24, 7)
therefore , We need to exchange dimensions :
x = x.permute(0, 2, 1)
The input data after exchange will conform to CNN The input of .
The convolution operation in one-dimensional convolution is aimed at seq_len Dimensionally , That is to say (30, 7, 24) The last dimension in . therefore , after :
nn.Conv1d(in_channels=args.in_channels, out_channels=args.out_channels, kernel_size=3)
after , The data dimension will become :
(30, 32, 24-3+1)=(30, 32, 22)
First dimensional batch_size unchanged , The second dimension is input_size Will be made by in_channels=7 become out_channels=32, The third dimension is convoluted into 22.
After a maximum pool, it becomes :
(30, 32, 22-3+1)=(30, 32, 20)
At this time (30, 32, 20) Will serve as a LSTM The input of . Because in LSTM We set up batch_first=True, therefore LSTM The input dimensions that can be received are :
input(batch_size, seq_len, input_size)
The data dimension after convolution pooling is :
input(batch_size=30, input_size=32, seq_len=20)
therefore , Dimension exchange is also required :
x = x.permute(0, 2, 1)
Then there is the more conventional LSTM Input and output , No more details .
therefore , complete forward The function is shown below :
def forward(self, x):
x = x.permute(0, 2, 1)
x = self.conv(x)
x = x.permute(0, 2, 1)
x, _ = self.lstm(x)
x = self.fc(x)
x = x[:, -1, :]
return x
III. Code implementation
3.1 Data processing
According to the former 24 The load at one time and the environmental variables at that time are used to predict the next 4 The load of a moment , The direct multiple output strategy is adopted here , adjustment output_size The output step size can be adjusted .
Code implementation :
def nn_seq(args):
seq_len, B, num = args.seq_len, args.batch_size, args.output_size
print('data processing...')
dataset = load_data()
# split
train = dataset[:int(len(dataset) * 0.6)]
val = dataset[int(len(dataset) * 0.6):int(len(dataset) * 0.8)]
test = dataset[int(len(dataset) * 0.8):len(dataset)]
m, n = np.max(train[train.columns[1]]), np.min(train[train.columns[1]])
def process(data, batch_size, step_size):
load = data[data.columns[1]]
data = data.values.tolist()
load = (load - n) / (m - n)
load = load.tolist()
seq = []
for i in range(0, len(data) - seq_len - num, step_size):
train_seq = []
train_label = []
for j in range(i, i + seq_len):
x = [load[j]]
for c in range(2, 8):
x.append(data[j][c])
train_seq.append(x)
for j in range(i + seq_len, i + seq_len + num):
train_label.append(load[j])
train_seq = torch.FloatTensor(train_seq)
train_label = torch.FloatTensor(train_label).view(-1)
seq.append((train_seq, train_label))
# print(seq[-1])
seq = MyDataset(seq)
seq = DataLoader(dataset=seq, batch_size=batch_size, shuffle=False, num_workers=0, drop_last=False)
return seq
Dtr = process(train, B, step_size=1)
Val = process(val, B, step_size=1)
Dte = process(test, B, step_size=num)
return Dtr, Val, Dte, m, n
3.2 model training / test
Same as before :
def train(args, Dtr, Val, path):
model = CNN_LSTM(args).to(args.device)
loss_function = nn.MSELoss().to(args.device)
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
print('training...')
epochs = 50
min_epochs = 10
best_model = None
min_val_loss = 5
for epoch in range(epochs):
train_loss = []
for batch_idx, (seq, target) in enumerate(Dtr, 0):
seq, target = seq.to(args.device), target.to(args.device)
optimizer.zero_grad()
y_pred = model(seq)
loss = loss_function(y_pred, target)
train_loss.append(loss.item())
loss.backward()
optimizer.step()
# validation
val_loss = get_val_loss(args, model, Val)
if epoch + 1 >= min_epochs and val_loss < min_val_loss:
min_val_loss = val_loss
best_model = copy.deepcopy(model)
print('epoch {:03d} train_loss {:.8f} val_loss {:.8f}'.format(epoch, np.mean(train_loss), val_loss))
model.train()
state = {
'model': best_model.state_dict(), 'optimizer': optimizer.state_dict()}
torch.save(state, path)
def test(args, Dte, path, m, n):
print('loading model...')
model = CNN_LSTM(args).to(args.device)
model.load_state_dict(torch.load(path)['model'])
model.eval()
pred = []
y = []
for batch_idx, (seq, target) in enumerate(Dte, 0):
seq = seq.to(args.device)
with torch.no_grad():
target = list(chain.from_iterable(target.tolist()))
y.extend(target)
y_pred = model(seq)
y_pred = list(chain.from_iterable(y_pred.data.tolist()))
pred.extend(y_pred)
y, pred = np.array(y), np.array(pred)
y = (m - n) * y + n
pred = (m - n) * pred + n
print('mape:', get_mape(y, pred))
# plot
x = [i for i in range(1, 151)]
x_smooth = np.linspace(np.min(x), np.max(x), 900)
y_smooth = make_interp_spline(x, y[150:300])(x_smooth)
plt.plot(x_smooth, y_smooth, c='green', marker='*', ms=1, alpha=0.75, label='true')
y_smooth = make_interp_spline(x, pred[150:300])(x_smooth)
plt.plot(x_smooth, y_smooth, c='red', marker='o', ms=1, alpha=0.75, label='pred')
plt.grid(axis='y')
plt.legend()
plt.show()
3.3 experimental result
front 24 A moment to predict the future 4 A moment ,MAPE by 7.41%:
IV. Source code and data
Consider making it public in the future ~
边栏推荐
- Report on China's potassium fluoride Market Survey and suggestions for future development strategic planning 2022
- Matlab linear programming model learning notes
- LabVIEW Arduino TCP/IP远程智能家居系统(项目篇—5)
- 【特征提取】基于稀疏PCA实现目标识别信息特征选择附matlab源码
- “试用期避免被辞退“ 指南攻略
- China's Ni MH battery industry development overview analysis and investment trend forecast report 2022 Edition
- Interviewer: what is the difference between a test plan and a test plan?
- Judgment of SQL null value
- China imported wine circulation and investment market survey and Future Development Trend Outlook report 2022-2027
- Dpdk - tcp/udp protocol stack server implementation (I)
猜你喜欢

Vulnerability discovery - API interface service vulnerability probe type utilization and repair
![[micro service series] protocol buffer dynamic analysis](/img/86/357d55c77cc67d6413af2de59bf395.png)
[micro service series] protocol buffer dynamic analysis

Closure problem C Lua

unity之EasyAR使用

Paths with a certain value in a binary tree (1) (2) (3) (Sword finger offer)

营销技巧:相比较讲产品的优点,更有效的是要向客户展示使用效果
![[004] [stm32] MDK project configuration and commissioning](/img/a8/9817cdbbce557a92739de494490706.jpg)
[004] [stm32] MDK project configuration and commissioning

LabVIEW Arduino TCP/IP遠程智能家居系統(項目篇—5)

分析 NFT 项目的 5 个指标

面试官:测试计划和测试方案有什么区别?
随机推荐
MySQL delete in without index
[image detection] image target size measurement system based on morphology with matlab code
[path planning] robot path planning based on improved artificial potential field with matlab code
Web technology sharing | webrtc recording video stream
Guide to "avoid dismissal during probation period"
Shell编程-用户信息管理
Research Report on pallet handling equipment industry - market status analysis and development prospect forecast
Promise API for getting started with the web
NumPy学习挑战第五关-创建数组
NumPy学习挑战第一关-NumPy的下载与安装
LightGBM--调参笔记
LabVIEW Arduino TCP/IP遠程智能家居系統(項目篇—5)
How to set MySQL triggers is a simple tutorial for novices
My SQL(二)
MySQL基础用法01
【图像增强】基于人工多重曝光融合AMEF实现图像去雾附matlab代码
【图像分割】基于最大主曲率实现视网膜眼底图像中的血管提取附matlab代码
二叉树中和为某一值的路径(一)(二)(三)(剑指offer)
[image enhancement] image defogging based on artificial multiple exposure fusion amef with matlab code
Interviewer: what is the difference between a test plan and a test plan?