Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
589 views
in Technique[技术] by (71.8m points)

python - Keras Dense layer shape error

I am using keras to create a LSTM model. While training, I am getting this error.

ValueError: Error when checking target: expected dense_4 to have shape (1,) but got array with shape (34,)

Here is my model

model = Sequential()

model.add(Embedding(max_words, embedding_dim, input_length=maxlen))    
model.add(LSTM(128, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))

model.add(Dense(units = 34 ,activation='softmax'))

model.layers[0].set_weights([embedding_matrix])
model.layers[0].trainable = False

model.compile(optimizer='rmsprop',loss='sparse_categorical_crossentropy',metrics=['acc'])

Model Summary:

Layer (type)                 Output Shape              Param #   
=================================================================
embedding_2 (Embedding)      (None, 15, 50)            500000    
_________________________________________________________________
lstm_2 (LSTM)                (None, 128)               91648     
_________________________________________________________________
dense_3 (Dense)              (None, 64)                8256      
_________________________________________________________________
dropout_2 (Dropout)          (None, 64)                0         
_________________________________________________________________
dense_4 (Dense)              (None, 34)                2210      
=================================================================
Total params: 602,114
Trainable params: 102,114
Non-trainable params: 500,000
_________________________________________________________________

I am calling fit using

history = model.fit(X_train, y_train,epochs=100,batch_size=128)

y_train is a one-hot-encoded label with shape (299, 34). X_train is of shape (299, 15).

I am not sure why model is looking for shape(1,) as I can see that dense_4 (Dense) has an output shape of `(None, 34).

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, I found the issue. I am posting this as answer so that it can help others also who is facing the same issue.

It was not the layer configuration but the wrong loss function.

I was using sparse_categorical_crossentropy as loss where labels must have the shape [batch_size] and the dtype int32 or int64. I have changed is to categorical_crossentropy which expect label of [batch_size, num_classes].

Error message thrown by keras was misleading.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...