Keras:损失值大部分为零,但acc低

时间:2019-07-11 00:53:42

标签: python tensorflow keras

我正在使用tensorflow运行一个简单的神经网络,如下面的代码所示。但是我不明白为什么几批后损耗值显着降低到接近零,而acc值却没有真正增加?

_________________________________________________________________

Layer (type)                 Output Shape              Param #
=================================================================
input_1 (InputLayer)         (None, 3327)              0
_________________________________________________________________
dense_2 (Dense)              (None, 3)                 9984
=================================================================
Total params: 9,984
Trainable params: 9,984
Non-trainable params: 0
_________________________________________________________________
None
Train on 480888 samples, validate on 53433 samples
Epoch 1/20

32/480888 [..............................] - ETA: 1092:47:40 - loss: 0.6549 - acc: 0.3125

224/480888 [..............................] - ETA: 156:05:19 - loss: 0.0936 - acc: 0.3393

576/480888 [..............................] - ETA: 60:40:08 - loss: 0.0364 - acc: 0.3490

1024/480888 [..............................] - ETA: 34:06:04 - loss: 0.0205 - acc: 0.3555

1440/480888 [..............................] - ETA: 24:14:01 - loss: 0.0146 - acc: 0.3604

1856/480888 [..............................] - ETA: 18:47:22 - loss: 0.0113 - acc: 0.3594

  ...
  ...
20960/480888 [>.............................] - ETA: 1:36:49 - loss: 9.9997e-04 - acc: 0.3468

21440/480888 [>.............................] - ETA: 1:34:34 - loss: 9.7758e-04 - acc: 0.3465

21888/480888 [>.............................] - ETA: 1:32:34 - loss: 9.5757e-04 - acc: 0.3469

22336/480888 [>.............................] - ETA: 1:30:38 - loss: 9.3837e-04 - acc: 0.3476

22784/480888 [>.............................] - ETA: 1:28:47 - loss: 9.1992e-04 - acc: 0.3477

23264/480888 [>.............................] - ETA: 1:26:53 - loss: 9.0094e-04 - acc: 0.3475

23712/480888 [>.............................] - ETA: 1:25:10 - loss: 8.8392e-04 - acc: 0.3479

以上acc值是针对每个当前批次进行评估的,对吗?

scaler = preprocessing.MinMaxScaler()
scalerMaxAbs = preprocessing.MaxAbsScaler()
training_metadata = scaler.fit_transform(training_data[metadata].astype(np.float32))
testing_metadata = scaler.transform(testing_data[metadata].astype(np.float32))
training_scores = scalerMaxAbs.fit_transform(training_data[scores])
testing_scores = scalerMaxAbs.transform(testing_data[scores])

y_train = np_utils.to_categorical(training_data['label'], num_classes=3)
y_test = np_utils.to_categorical(testing_data['label'], num_classes=3)

training_features = np.concatenate((training_metadata, training_scores), axis=1)
testing_features = np.concatenate((testing_metadata, testing_scores), axis=1)

inputs = Input(shape=(training_features.shape[1],), dtype='float32')
hh_layer = Dense(128, activation=tf.nn.relu)(inputs)
dropout = Dropout(0.2)(hh_layer)
output = Dense(3, activation=tf.nn.softmax)(inputs)

model = Model(inputs=inputs, output=output)
print(model.summary())
early_stopping_monitor = EarlyStopping(patience=3)
adam = Adam(lr=0.001)
model.compile(optimizer=adam,
              loss='categorical_crossentropy',
              metrics=['accuracy'])
model.fit(training_features, y_train, epochs=20,  validation_split=0.1, callbacks=[early_stopping_monitor])

score = model.evaluate(testing_features, y_test)

4个纪元的结果:

480888/480888 [==============================] - 332s 691us/step - loss: 4.3699e-05 - acc: 0.3474 - val_loss: 1.1921e-07 - val_acc: 0.3493
480888/480888 [==============================] - 71s 148us/step - loss: 1.1921e-07 - acc: 0.3474 - val_loss: 1.1921e-07 - val_acc: 0.3493
480888/480888 [==============================] - 71s 148us/step - loss: 1.1921e-07 - acc: 0.3474 - val_loss: 1.1921e-07 - val_acc: 0.3493
480888/480888 [==============================] - 71s 147us/step - loss: 1.1921e-07 - acc: 0.3474 - val_loss: 1.1921e-07 - val_acc: 0.3493

4个历时的测试集最终结果,并提前停止

loss, acc: [1.1920930376163597e-07, 0.34758880897839645]

1 个答案:

答案 0 :(得分:0)

Step0:保存模型,然后加载模型,并在几个样本上检查其性能,以确保计算损失或acc中是否存在任何错误。如果您没有发现任何问题,请更新问题。