2017-10-10 107 views
0

我正在尝试使用LSTM来训练一个简单的多对一RNN分类器。我的时间步长为100个数据点,具有7个特征,总共有192382个样本。这是我的型号:多对一的LSTM,Keras上的softmax尺寸错误

model = Sequential() 
model.add(LSTM(50,input_shape = (100,7),name = 'LSTM',return_sequences=False)) 
model.add(Dropout(0.2)) 
model.add(Dense(3, activation='softmax',name = 'softmax_layer')) 
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'],name='softmax') 
model.fit(datax,datay,epochs=25,batch_size=128) 
model.summary() 

该模型编译罚款没有错误,但我不能适应模型。这是它返回的错误:

ValueError: Error when checking target: expected softmax_layer to have shape (None, 3) but got array with shape (192282, 100) 

有没有人有一个想法,为什么softmax图层返回一个(192282,100)矩阵? LSTM层中的return_sequence = False是否应该只给每个时间步输出一个输出?

+0

假设你正在分类至3班,你传递DATAY错误的值 – ilan

回答

0

实际上,softmax_layer回报率(无,3),因为最后一层的尺寸为3

也许你要修复它?所以,为了解决它,你需要输出层(softmax_layer)的大小应该等于你的标签数组的大小(datay.shape [1])。换句话说,它必须等于类的数量。

快速的解决方法是:

model = Sequential() 
model.add(LSTM(50,input_shape = (100,7),name = 'LSTM',return_sequences=False)) 
model.add(Dropout(0.2)) 
model.add(Dense(datay.shape[1], activation='softmax',name = 'softmax_layer')) 
model.compile(optimizer='adam',loss='categorical_crossentropy',metrics=['accuracy'],name='softmax') 
model.fit(datax,datay,epochs=25,batch_size=128) 
model.summary()