python-3.x – keras LSTM模型中的尺寸不匹配
我想使用带有keras的LSTM神经网络来预测时间序列组,我在使模型匹配我想要的东西时遇到麻烦.我的数据的维度是:
输入张量:(数据长度,训练系列数,回顾时间步长) 输出张量:(数据长度,要预测的序列数,向前看的时间步长)
重现问题的虚拟数据代码是: import numpy as np from keras.models import Sequential from keras.layers import Dense,TimeDistributed,LSTM epoch_number = 100 batch_size = 20 input_dim = 4 output_dim = 3 look_back = 24 look_ahead = 24 n = 100 trainX = np.random.rand(n,input_dim,look_back) trainY = np.random.rand(n,output_dim,look_ahead) print('test X:',trainX.shape) print('test Y:',trainY.shape) model = Sequential() # Add the first LSTM layer (The intermediate layers need to pass the sequences to the next layer) model.add(LSTM(10,batch_input_shape=(None,look_back),return_sequences=True)) # add the first LSTM layer (the dimensions are only needed in the first layer) model.add(LSTM(10,return_sequences=True)) # the TimeDistributed object allows a 3D output model.add(TimeDistributed(Dense(look_ahead))) model.compile(loss='mean_squared_error',optimizer='adam',metrics=['accuracy']) model.fit(trainX,trainY,nb_epoch=epoch_number,batch_size=batch_size,verbose=1) 这个流程:
问题似乎是在定义TimeDistributed层时. 如何定义TimeDistributed层以便编译和训练? 解决方法
在您的情况下,错误消息有点误导.您的网络输出节点称为timedistributed_1,因为它是顺序模型中的最后一个节点.错误消息试图告诉您的是,此节点的输出与您的模型适合的目标不匹配,即您的标签trainY.
您的trainY的形状为(n,look_ahead),因此(100,24)但网络正在生成(batch_size,look_ahead)的输出形状.这种情况下的问题是output_dim!= input_dim.如果时间维度发生变化,则可能需要填充或删除所述时间步长的网络节点. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- python中查找excel某一列的重复数据 剔除之后打印
- python学习教程之Numpy和Pandas的使用
- django – ‘HttpResponse’没有属性’_get_content
- python实现广度优先搜索
- 在Python中结合枚举itertools.izip
- Python新手爬虫四:爬取视频
- Python 'takes exactly 1 argument (2 given)'
- python – 在pytest中使用conftest进行setup / teardown
- 详解linux下安装python3环境
- 利用标准库fractions模块让Python支持分数类型的方法详解