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

Categories

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

python - Training and validation loss is zero

I'm trying to train a network for a textclassification where the texts are labeled with 6 different categories. Each text can have only one label. So far I built the following simple network:

## Network architecture
model = Sequential()
model.add(Embedding(20000, 100, input_length=50))
model.add(LSTM(100, dropout=0.2, recurrent_dropout=0.2))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

## Fit the model
history = model.fit(data, np.array(labels), validation_split=0.2, batch_size = 32, epochs=25)

When I'm training the networtk the training and validation loss stays constantly by '0.0000e+00.

Epoch 1/25
26/26 [==============================] - 8s 225ms/step - loss: 0.0000e+00 - accuracy: 0.2847 - val_loss: 0.0000e+00 - val_accuracy: 0.3188
Epoch 2/25
26/26 [==============================] - 6s 213ms/step - loss: 0.0000e+00 - accuracy: 0.2887 - val_loss: 0.0000e+00 - val_accuracy: 0.2754
Epoch 3/25
26/26 [==============================] - 6s 230ms/step - loss: 0.0000e+00 - accuracy: 0.2350 - val_loss: 0.0000e+00 - val_accuracy: 0.2705
Epoch 4/25
26/26 [==============================] - 6s 217ms/step - loss: 0.0000e+00 - accuracy: 0.2180 - val_loss: 0.0000e+00 - val_accuracy: 0.2657
Epoch 5/25
26/26 [==============================] - 6s 220ms/step - loss: 0.0000e+00 - accuracy: 0.2262 - val_loss: 0.0000e+00 - val_accuracy: 0.2609
Epoch 6/25
26/26 [==============================] - 6s 224ms/step - loss: 0.0000e+00 - accuracy: 0.2542 - val_loss: 0.0000e+00 - val_accuracy: 0.2609
Epoch 7/25
26/26 [==============================] - 6s 223ms/step - loss: 0.0000e+00 - accuracy: 0.2379 - val_loss: 0.0000e+00 - val_accuracy: 0.2512
.
.
.

Does somebody know what's causing this?


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

1 Answer

0 votes
by (71.8m points)

In your last layer

model.add(Dense(1, activation='sigmoid'))

you must set first arg (units) to the number of categories (i.e. it should be 6 instead of 1) and softmax instead of sigmoid

model.add(Dense(6, activation='softmax'))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

62 comments

56.7k users

...