Keras:非常低的精度,非常高的损耗,并且每个输入的预测都是相同的

时间:2019-05-09 16:07:52

标签: python machine-learning keras neural-network

我正在开发一种使用keras进行通用音频标记的系统。

我有以下数据输入: x_train每个输入都有10个不同的数据(data_leng,max,min等),y_train代表41个可能的标签(吉他,bass等)

x_train shape = (7104, 10)
y_train shape = (41,)

print(x_train[0])

[ 3.75732000e+05 -2.23437546e-05 -1.17187500e-02  1.30615234e-02
  2.65964586e-03  2.65973969e-03  9.80024859e-02  1.13624850e+00
  1.00003528e+00 -1.11458333e+00] 

print(y_train[0])

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.]

我的模特是:

from keras.models import Sequential
from keras.optimizers import SGD
from keras.layers import Dense, Dropout, Activation

model = Sequential()

model.add(Dense(units=128, activation='relu', input_dim=10))
model.add(Dropout(0.5))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(41, activation='softmax'))

opt = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)

model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy'])

model.fit(np.array(x_train), np.array(y_train), epochs=5, batch_size=8)

这是我的结果:

Epoch 1/5
7104/7104 [==============================] - 1s 179us/step - loss: 15.7392 - acc: 0.0235
Epoch 2/5
7104/7104 [==============================] - 1s 132us/step - loss: 15.7369 - acc: 0.0236
Epoch 3/5
7104/7104 [==============================] - 1s 133us/step - loss: 15.7415 - acc: 0.0234
Epoch 4/5
7104/7104 [==============================] - 1s 132us/step - loss: 15.7262 - acc: 0.0242
Epoch 5/5
7104/7104 [==============================] - 1s 132us/step - loss: 15.6484 - acc: 0.0291

如您所见,我的结果显示出很高的数据丢失率和非常低的准确性,但是主要问题是当我尝试预测结果时,导致每个输入的输出都相同。我怎样才能解决这个问题 ?


pre = model.predict(np.array(x_train), batch_size=8, verbose=0)

for i in pre:
    print(i)

[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
 0. 0. 0. 0. 0. 1. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
...

2 个答案:

答案 0 :(得分:0)

在“密集”层中,您只需要为第一层指定Input_dim。

Keras负责其他层的Dim。

所以尝试:

model = Sequential()

model.add(Dense(units=128, activation='relu', input_dim=10))
model.add(Dropout(0.5))
model.add(Dense(units=64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(units=32, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(41, activation='softmax'))

也许您的正则化对于此类数据而言太强了,请尝试使用强度不那么强或完全没有辍学的辍学。

您可以做的最后一件事是提高学习速度,从1e-3之类的东西开始,看看是否有变化。

希望我能帮助您

答案 1 :(得分:0)

您可以尝试测试其他优化器,并尝试更改最后一层的激活。我已经遇到了同样的问题,我在最后一个Dense层中使用了Softmax激活,我更改为Sigmoid并运行良好。

一种好的策略是修改模型的架构,添加更多层,更改缺失值等...

希望我能帮助您。祝你好运!