卷积神经网络(CNN)可以作为编解码器结构中的编码器。CNN不直接支持序列输入;相反,一维CNN能够读取序列输入并自动学习显著特征。然后可以按照正常情况由LSTM解码器解释这些。我们将使用CNN和LSTM的混合模型称为cn -LSTM模型,在本例中,我们将在一个编解码器体系结构中同时使用它们。CNN希望输入的数据具有与LSTM模型相同的3D结构,尽管多个特性被读取为不同的通道,最终具有相同的效果。我们将简化这个示例,并将重点放在具有单变量输入的cn - lstm上,但是它也可以很容易地更新为使用多元输入,这只是一个练习。与之前一样,我们将使用由14天的日总功耗组成的输入序列。我们将为编码器定义一个简单但有效的CNN架构,它由两个卷积层和一个最大池化层组成,然后将结果扁平化。第一个卷积层读取输入序列并将结果投影到特征图上。第二层对第一层创建的特征图执行相同的操作,试图放大任何显著的特征。我们将在每个卷积层使用64个feature map,读取内核大小为3个时间步长的输入序列。最大池层通过保留最大信号值的1/4来简化特征映射。在池化层之后提取的特征映射被压扁成一个长向量,然后可以用作解码过程的输入。

model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))
model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))
model.add(MaxPooling1D(pool_size=2))
model.add(Flatten())

解码器与前面几节定义的解码器相同。唯一的其他变化是将训练时间设置为20个。带有这些更改的build_model()函数如下所示。

# train the model
def build_model(train, n_input):# prepare datatrain_x, train_y = to_supervised(train, n_input)# define parametersverbose, epochs, batch_size = 0, 20, 16n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]# reshape output into [samples, timesteps, features]train_y = train_y.reshape((train_y.shape[0], train_y.shape[1], 1))# define modelmodel = Sequential()model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))model.add(MaxPooling1D(pool_size=2))model.add(Flatten())model.add(RepeatVector(n_outputs))model.add(LSTM(200, activation='relu', return_sequences=True))model.add(TimeDistributed(Dense(100, activation='relu')))model.add(TimeDistributed(Dense(1)))model.compile(loss='mse', optimizer='adam')# fit networkmodel.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)return model

现在我们准备使用CNN编码器来尝试编码器-解码器架构。下面提供了完整的代码清单。

# univariate multi-step encoder-decoder cnn-lstm
from math import sqrt
from numpy import split
from numpy import array
from pandas import read_csv
from sklearn.metrics import mean_squared_error
from matplotlib import pyplot
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import Flatten
from keras.layers import LSTM
from keras.layers import RepeatVector
from keras.layers import TimeDistributed
from keras.layers.convolutional import Conv1D
from keras.layers.convolutional import MaxPooling1D# split a univariate dataset into train/test sets
def split_dataset(data):# split into standard weekstrain, test = data[1:-328], data[-328:-6]# restructure into windows of weekly datatrain = array(split(train, len(train)/7))test = array(split(test, len(test)/7))return train, test# evaluate one or more weekly forecasts against expected values
def evaluate_forecasts(actual, predicted):scores = list()# calculate an RMSE score for each dayfor i in range(actual.shape[1]):# calculate msemse = mean_squared_error(actual[:, i], predicted[:, i])# calculate rmsermse = sqrt(mse)# storescores.append(rmse)# calculate overall RMSEs = 0for row in range(actual.shape[0]):for col in range(actual.shape[1]):s += (actual[row, col] - predicted[row, col])**2score = sqrt(s / (actual.shape[0] * actual.shape[1]))return score, scores# summarize scores
def summarize_scores(name, score, scores):s_scores = ', '.join(['%.1f' % s for s in scores])print('%s: [%.3f] %s' % (name, score, s_scores))# convert history into inputs and outputs
def to_supervised(train, n_input, n_out=7):# flatten datadata = train.reshape((train.shape[0]*train.shape[1], train.shape[2]))X, y = list(), list()in_start = 0# step over the entire history one time step at a timefor _ in range(len(data)):# define the end of the input sequencein_end = in_start + n_inputout_end = in_end + n_out# ensure we have enough data for this instanceif out_end < len(data):x_input = data[in_start:in_end, 0]x_input = x_input.reshape((len(x_input), 1))X.append(x_input)y.append(data[in_end:out_end, 0])# move along one time stepin_start += 1return array(X), array(y)# train the model
def build_model(train, n_input):# prepare datatrain_x, train_y = to_supervised(train, n_input)# define parametersverbose, epochs, batch_size = 0, 20, 16n_timesteps, n_features, n_outputs = train_x.shape[1], train_x.shape[2], train_y.shape[1]# reshape output into [samples, timesteps, features]train_y = train_y.reshape((train_y.shape[0], train_y.shape[1], 1))# define modelmodel = Sequential()model.add(Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(n_timesteps,n_features)))model.add(Conv1D(filters=64, kernel_size=3, activation='relu'))model.add(MaxPooling1D(pool_size=2))model.add(Flatten())model.add(RepeatVector(n_outputs))model.add(LSTM(200, activation='relu', return_sequences=True))model.add(TimeDistributed(Dense(100, activation='relu')))model.add(TimeDistributed(Dense(1)))model.compile(loss='mse', optimizer='adam')# fit networkmodel.fit(train_x, train_y, epochs=epochs, batch_size=batch_size, verbose=verbose)return model# make a forecast
def forecast(model, history, n_input):# flatten datadata = array(history)data = data.reshape((data.shape[0]*data.shape[1], data.shape[2]))# retrieve last observations for input datainput_x = data[-n_input:, 0]# reshape into [1, n_input, 1]input_x = input_x.reshape((1, len(input_x), 1))# forecast the next weekyhat = model.predict(input_x, verbose=0)# we only want the vector forecastyhat = yhat[0]return yhat# evaluate a single model
def evaluate_model(train, test, n_input):# fit modelmodel = build_model(train, n_input)# history is a list of weekly datahistory = [x for x in train]# walk-forward validation over each weekpredictions = list()for i in range(len(test)):# predict the weekyhat_sequence = forecast(model, history, n_input)# store the predictionspredictions.append(yhat_sequence)# get real observation and add to history for predicting the next weekhistory.append(test[i, :])# evaluate predictions days for each weekpredictions = array(predictions)score, scores = evaluate_forecasts(test[:, :, 0], predictions)return score, scores# load the new file
dataset = read_csv('household_power_consumption_days.csv', header=0, infer_datetime_format=True, parse_dates=['datetime'], index_col=['datetime'])
# split into train and test
train, test = split_dataset(dataset.values)
# evaluate model and get scores
n_input = 14
score, scores = evaluate_model(train, test, n_input)
# summarize scores
summarize_scores('lstm', score, scores)
# plot scores
days = ['sun', 'mon', 'tue', 'wed', 'thr', 'fri', 'sat']
pyplot.plot(days, scores, marker='o', label='lstm')
pyplot.show()

具有单变量输入的CNN-LSTM编解码器模型相关推荐

  1. CNN+LSTM 的模型结合(keras代码实现)

    CNN-LSTM模型 运行环境:python3.6.5 .Keras 2.1.5 .tensorflow 2.3.1等 CNN-LSTM的Sequential()写法: from keras.mode ...

  2. 如何建立Multi-Step(多步预测)的LSTM时间序列模型(以对家庭用电预测为例)

    译自How to Develop LSTM Models for Multi-Step Time Series Forecasting of Household Power Consumption~ ...

  3. 深度学习时间序列预测:卷积神经网络(CNN)算法构建单变量时间序列预测模型预测空气质量(PM2.5)+代码实战

    深度学习时间序列预测:卷积神经网络(CNN)算法构建单变量时间序列预测模型预测空气质量(PM2.5)+代码实战 神经网络(neual networks)是人工智能研究领域的一部分,当前最流行的神经网络 ...

  4. Python时间序列LSTM预测系列教程(6)-单变量

    单变量LSTM预测模型(6) 教程原文连接 前置教程: Python时间序列LSTM预测系列教程(1)-单变量 Python时间序列LSTM预测系列教程(2)-单变量 Python时间序列LSTM预测 ...

  5. 用神经网络做单变量时序预测,如何构造神经网络的输入呢?

    用神经网络做单变量时序预测,如何构造神经网络的输入呢? 单变量时序预测是一种常见的时间序列分析问题,神经网络可以用于对单变量时序数据进行预测.神经网络的输入通常是过去一段时间的数据,输出是未来一个时间 ...

  6. 深度学习时间序列预测:LSTM算法构建时间序列单变量模型预测大气压( air pressure)+代码实战

    深度学习时间序列预测:LSTM算法构建时间序列单变量模型预测大气压( air pressure)+代码实战 长短期记忆(Long short-term memory, LSTM)是一种特殊的RNN,主 ...

  7. 深度学习时间序列预测:LSTM算法构建时间序列单变量模型预测空气质量(PM2.5)+代码实战

    深度学习时间序列预测:LSTM算法构建时间序列单变量模型预测空气质量(PM2.5)+代码实战 # 导入需要的包和函数: from __future__ import print_function im ...

  8. TF之LSTM:基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测

    TF之LSTM:基于tensorflow框架自定义LSTM算法实现股票历史(1990~2015数据集,6112预测后100+单变量最高)行情回归预测 目录 输出结果 LSTM代码 输出结果 数据集 L ...

  9. 基于Keras的单变量单时间步长LSTM电力负荷预测(一)

    这几天一直在研究LSTM,想着用LSTM做个电力负荷预测,之前用Tensorflow搭建怎么也调试不好,一度崩溃.无奈换成Keras,Keras搭建LSTM结构整体思路看着非常清晰,程序量也较少.现将 ...

最新文章

  1. 【PHPExcel】生成xls文件并下载
  2. matplotlib命令与格式:图像(figure)与子区域(axes)布局与规划
  3. Paint the Tree CodeForces - 1244D(看似是树,其实是条链)
  4. Java EE 8中的MVC 1.0:使用Facelets入门
  5. 方差偏差权衡_偏差偏差权衡:快速介绍
  6. Android 客户端与服务器交互方式
  7. linux底行模式显示信息,14天linux命令加强
  8. ros如何编译python文件_Python为ROS编写一个简单的发布者和订阅者
  9. 设计模式(五):命令模式
  10. 怎看沃科斯扫地机器人型号_科沃斯型号区别是什么?
  11. 子慕谈设计模式系列(一)
  12. Android基础之图片的压缩算法
  13. 计算机管理系统有几种,ERP系统有几种?怎么分类
  14. Eviews创建散点图的具体方法
  15. 13 Java反射机制
  16. oracle rat结果分析比较,Oracle RAT- Real Application Testing
  17. 超级简单得App自动化demo,有手就会,Weditor + uiautomator2 实现app自动化
  18. window7调用计算机,教你查看win7系统电脑使用记录的具体方法
  19. Google与k8s
  20. 使用python导出msc.marc后处理数据——PyPost介绍

热门文章

  1. android 朗读英文,Android中用TTS语音朗读一段英文
  2. noi linux 试题解密,关于NOIP丨你想知道的都在这(附NOI2019笔试题库)
  3. 永久免费开源在线客服系统推荐收藏
  4. LOGO(小海龟)编程之父留给我们的思想遗产
  5. 基于全志A10\A20产品跑分测试结果对比
  6. 文章伪原创方法(如何伪原创使文章快速收录)
  7. win10安装misql8_帮忙看下刚刚安装了win10 1803发生的错误问题?
  8. 江民杀毒软件KV2008正式版评测报告
  9. 专访UCloud王冬冬:UDDP如何在大数据下抛头露面
  10. 主流物联网协议选择:MQTT、CoAP 还是 LwM2M?