LSTM keras - 值错误如何解决输入维度
LSTM keras - Value error how to resolve input dimension
我已经为训练数据创建了一个函数,它可以随机选择正面或负面的文件。这是一个二元分类问题。
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 个时间步,则必须将其重塑为 array.reshape(6,1,30)
,或者如果您使用 6 个时间步,则 array.reshape(1,6,30)
有关重塑 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 个样本,您只有一个样本,但你正在做验证 split.And 因为你只有一个 X 值,它将只有一个 Y(PositiveOrNegativeLabel),所以我只分配了一个值,即 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)
我已经为训练数据创建了一个函数,它可以随机选择正面或负面的文件。这是一个二元分类问题。
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 个时间步,则必须将其重塑为 array.reshape(6,1,30)
,或者如果您使用 6 个时间步,则 array.reshape(1,6,30)
有关重塑 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 个样本,您只有一个样本,但你正在做验证 split.And 因为你只有一个 X 值,它将只有一个 Y(PositiveOrNegativeLabel),所以我只分配了一个值,即 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)