我正在尝试用Keras开发CRNN。但是当训练网络时,我的损失在大约两次迭代后保持不变,而且极低!它不可能是正确的,但我不知道我的错误,所以我想向你展示我的模型和我的训练,并希望你们可以帮助我解决这个问题。
这是奇怪的损失:
1. Iteration:
Train: Loss: 4.596062183380127 accuracy: 0.34375
2. Iteration:
Train: Loss: 1.1920930376163597e-07 accuracy: 0.453125
3. Iteration:
Train: Loss: 1.1920930376163597e-07 accuracy: 0.53125
4. Iteration:
Train: Loss: 1.1920930376163597e-07 accuracy: 0.5625
5. Iteration:
Train: Loss: 1.1920930376163597e-07 accuracy: 0.484375
这是我的模特:
cnn_dropout = 0.3
rnn_dropout = 0.3
learning_rate = 0.001
n_units = 256
batch_size = 64
steps = 1000
strides = 3
kernel_size = 3
num_classes = 4
model = Sequential()
# CNN 2D
model.add(Convolution2D(n_units, strides=strides, kernel_size=kernel_size, batch_size=batch_size, input_shape=input_shape, padding='SAME', data_format='channels_first'))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Convolution2D(n_units, strides=strides, kernel_size=kernel_size, padding='SAME'))
model.add(Activation('relu'))
model.add(BatchNormalization())
model.add(MaxPooling2D(pool_size=(2, 2), strides=(2, 2)))
model.add(Dropout(cnn_dropout))
model.add(Reshape((n_units, -1)))
model.add(LSTM(n_units, input_shape=(256, 147), return_sequences=True, go_backwards=True))
model.add(BatchNormalization())
model.add(Activation('tanh'))
model.add(Dropout(rnn_dropout))
model.add(Flatten())
model.add(Dense(100))
model.add(Activation('relu'))
model.add(Dense(4))
model.add(Activation('softmax'))
model.add(Dropout(cnn_dropout))
opt = keras.optimizers.adam(lr=learning_rate)
model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
这是我的培训阶段:
# Training the model
for n in range(1, steps):
print("{}. Iteration:".format(n))
x_train_batch, y_train_batch = next_batch(batch_size, train_feature_paths, train_labels_f0)
score = model.train_on_batch(x_train_batch, y_train_batch)
print("Train: Loss: {} accuracy: {}".format(score[0], score[1]))
# Test every 20 iterations
if n % 100 == 0:
x_test_batch, y_test_batch = next_batch(batch_size, test_feature_paths, test_labels_f0)
score = model.test_on_batch(x_test_batch, y_test_batch)
print("Test: Loss: {} accuracy: {}".format(score[0], score[1]))
如果您需要知道,我的输入数据是mel-spectrograms,它们被提取并预先存储为* .npy文件。存储的mel-specs的形状是(19,128,128)。
我使用tensorflow-gpu版本1.5.1和Keras版本2.1.6
答案 0 :(得分:0)
在最终的softmax图层之后添加一个dropout图层基本上会将输出图层清零,这样即使给出一个完美的模型,你的丢失百分比也会有效地降低你出错的几率!这也意味着最终输出通常不会将概率分布(即总和为1)建模为softmax层的输出,这意味着应用分类交叉熵(依赖于输入是概率分布)会产生非常奇怪的结果(如果他们不应该是0)