我制作了一个用于训练数据的函数,该函数选择随机文件,无论是正数还是负数。这是一个二元分类问题。
model=Sequential()
InputBatch = np.expand_dims(InputBatch, 0)
print(InputBatch.shape)
model.add(LSTM(100,input_shape=(1,6,30),return_sequences=True))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=6,nb_epoch=10,verbose=1,validation_split=0.05)
InputBatch变量的形状为(1,6,30)
例如我的输入数据是
[[ nan 1520. 1295. nan 8396. 9322. 12715. nan 5172. 7232.
11266. nan 11266. 2757. 4416. 12020. 12111. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ nan 3045. 11480. 900. 5842. 11496. 4463. nan 11956. 900.
10400. 8022. 2504. 12106. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ nan 9307. 12003. 2879. 6398. 9372. 4614. 5222. nan nan
2879. 10364. 6923. 4709. 4860. 11871. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ nan 6689. 2818. 12003. 6480. nan 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ nan 3395. 1087. 11904. 7232. 8840. 10115. 4494. 11516. 7441.
8535. 12106. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ nan 1287. 420. 4070. 11087. 7410. 12186. 2387. 12111. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
我设置了数据的形状为(6,30)
我收到值错误
ValueError: Error when checking input: expected lstm_16_input to have 3 dimensions, but got array with shape (1,6, 30)
它正在获取三维输入,我不明白如何以及为什么
最佳答案
LSTM 输入必须位于 3 dimensional(samples,timesteps,features)
中。你的数据似乎是二维的。您可以使用 numpys reshape()
将数据转换为 3D 的函数。
例如,如果您使用 1 个时间步长,则必须将其 reshape 为 array.reshape(6,1,30)
或者如果您使用 6 个时间步长,则 array.reshape(1,6,30)
有关 reshape LSTM 输入的更多信息,您可以查看此 site
[[更新]] 你的代码有很多问题
model=Sequential()
InputBatch = np.expand_dims(InputBatch, 0)
print(InputBatch.shape)
model.add(LSTM(100,input_shape=(1,6,30),return_sequences=True))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=6,nb_epoch=10,verbose=1,validation_split=0.05)
当你将数据转换为(1,6,30)时,你基本上是说你只有一个样本(只有1),batch_size是6,但你只有1个样本,你只有一个样本,但你正在做验证分割。由于你只有一个 X 值,因此它也只有一个 Y(正值或负值标签),所以我只需要一个值,即 1。
我已经运行了您的程序,并对您在问题中显示的代码和数据进行了一些更改(我将 NA 更改为 0):
a=np.array([
[0,1520,1295,0,8396,9322,12715,0,5172,7232,11266,0,11266,2757,4416,12020,12111,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,3045,11480,900,5842,11496,4463,0,11956,900,10400,8022,2504,12106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,9307,12003,2879,6398,9372,4614,5222,0,0,2879,10364,6923,4709,4860,11871,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,6689,2818,12003,6480,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,3395,1087,11904,7232,8840,10115,4494,11516,7441,8535,12106,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
[0,1287,420,4070,11087,7410,12186,2387,12111,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
]
)
PositiveOrNegativeLabel=np.array([[1]])
PositiveOrNegativeLabel=PositiveOrNegativeLabel.reshape(1,-1)
PositiveOrNegativeLabel.shape
InputBatch =InputBatch.reshape(1,6,30)
InputBatch.shape
model=Sequential()
model.add(LSTM(1,input_shape=(6,30)))
model.add(Dense(1,activation="sigmoid"))
model.compile(loss='mean_absolute_error',optimizer='adam',metrics=['accuracy'])
model.fit(InputBatch,PositiveOrNegativeLabel,batch_size=1,verbose=1)
关于python - LSTM keras - 值错误如何解析输入维度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52940928/