Keras CNN回归 - MSE下降非常缓慢

时间:2017-10-04 17:40:18

标签: keras regression

from __future__ import print_function
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten
from keras.layers import Conv2D, MaxPooling2D
from keras.layers.normalization import BatchNormalization
from keras import backend as K

def sacc(y_true, y_pred):
    return K.mean(K.equal(K.round(y_true), K.round(y_pred)))

batch_size = 256
epochs = 10000
img_rows, img_cols, channels = 32, 32, 3 # input image dimensions

if K.image_data_format() == 'channels_first':
    X_train = X_train.reshape(X_train.shape[0], channels, img_rows, img_cols)
    X_val = X_val.reshape(X_val.shape[0], channels, img_rows, img_cols)
    X_test = X_test.reshape(X_test.shape[0], channels, img_rows, img_cols)
    input_shape = (channels, img_rows, img_cols)
else:
    X_train = X_train.reshape(X_train.shape[0], img_rows, img_cols, channels)
    X_val = X_val.reshape(X_val.shape[0], img_rows, img_cols, channels)
    X_test = X_test.reshape(X_test.shape[0], img_rows, img_cols, channels)
    input_shape = (img_rows, img_cols, channels)

print('X_train shape:', X_train.shape)
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')

model = Sequential()
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same', input_shape=input_shape))
model.add(Conv2D(64, kernel_size=(3, 3), activation='relu', padding='same'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(BatchNormalization())
model.add(Flatten())
model.add(Dense(512, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(1))

model.compile(loss=keras.losses.mean_squared_error, optimizer=keras.optimizers.Adadelta(), metrics=[sacc])

history = model.fit(X_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(X_val, y_val))

score = model.evaluate(X_val, y_val, verbose=0)

print('Test loss:', score[0])
print('Test accuracy:', score[1])

我有这个keras模型,它具有回归作为最终层而不是softmax。我试图预测给定图像的分数(0.0到100.0)。我看到的问题是,需要大约10000个纪元进行训练,而MSE减速非常慢。这是预期的还是我的代码有问题?任何提示或评论都表示赞赏。

1 个答案:

答案 0 :(得分:0)

您应该避免在输出层中将ReLU用作激活函数,因为它会在0 to infinity范围内提供连续输出。您希望您的输出介于0 to 100.0之间。 对于回归问题,您可以创建不具有任何激活函数的输出层,因为您对数值没有任何转换感兴趣,即避免以后在测试数据上使用reverse_transform以获得实际数值。

回归问题对异常值很敏感,因此请检查是否存在异常值。 除此之外,请尝试使用rmspropadam进行回归,并在不同的learning rate上进行网格搜索。