2017-07-25 159 views
2

輸入元件具有3行,每行具有199列和輸出具有46行和1列keras多輸入到simpleRNN尺寸:尺寸失配

Input.shape, output.shape 
((204563, 3, 199), (204563, 46, 1)) 

當輸入給出下面的錯誤被拋出:

from keras.layers import Dense 
from keras.models import Sequential 
from keras.layers.recurrent import SimpleRNN 

model = Sequential() 
model.add(SimpleRNN(100, input_shape = (Input.shape[1], Input.shape[2]))) 
model.add(Dense(output.shape[1], activation = 'softmax')) 
model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) 
model.fit(Input, output, epochs = 20, batch_size = 200) 

錯誤拋出:

Epoch 1/20 

--------------------------------------------------------------------------- 
ValueError        Traceback (most recent call last) 
<ipython-input-134-378dd431cf45> in <module>() 
     3 model.add(Dense(y_target.shape[1], activation = 'softmax')) 
     4 model.compile(loss = 'categorical_crossentropy', optimizer = 'adam', metrics = ['accuracy']) 
----> 5 model.fit(X_input, y_target, epochs = 20, batch_size = 200) 
. 
. 
. 
ValueError: Error when checking model target: expected dense_6 to have 2 dimensions, but got array with shape (204563, 46, 1) 

請解釋這個問題,可能soution的原因

回答

2

的問題是,SimpleRNN(100)返回形狀(204563, 100)的張量,因此,Dense(46)(因爲output.shape[1]=46)將返回形狀(204563, 46)的張量,但您y_target具有形狀(204563, 46, 1)。您需要刪除最後一個維度,例如y_target = np.squeeze(y_target),以便維度一致