CNN训练损失停留在51-60之间的值

时间:2019-10-19 15:09:55

标签: python pandas keras deep-learning conv-neural-network

我正在尝试创建我的第一个CNN以预测公寓价格。问题在于,在1-5个周期后,损耗值被卡住并且不会减小,只会稍微增加然后再次减小。预先感谢)

from keras.layers import Conv2D, MaxPool2D, Dense, BatchNormalization, Flatten
from keras.optimizers import Adam
from keras.models import Sequential

from keras.preprocessing.image import ImageDataGenerator
from PIL import Image
import pandas as pd

train_data_df = pd.read_excel('train_data_cnn.xlsx')
test_data_df = pd.read_excel('test_data_cnn.xlsx')

datagen = ImageDataGenerator(rescale=1./255)
train_data = datagen.flow_from_dataframe(dataframe=train_data_df, x_col='filepath', y_col='price', class_mode='raw', directory=r'C:\Users\Kojimba\PycharmProjects\DeepEval\CNN', batch_size=20)
test_data = datagen.flow_from_dataframe(dataframe=train_data_df, x_col='filepath', y_col='price', class_mode='raw', directory=r'C:\Users\Kojimba\PycharmProjects\DeepEval\CNN', batch_size=20)

model = Sequential([
    Conv2D(32, kernel_size=32, strides=(2,2), padding='same', activation='relu', input_shape=(256, 256, 3), data_format='channels_last'),
    #BatchNormalization(),
    MaxPool2D(strides=2),
    Conv2D(128, kernel_size=64, strides=(4,4), padding='same', activation='relu'),
    #BatchNormalization(),
    MaxPool2D(),
    Flatten(),
    Dense(8, activation='relu', kernel_initializer='random_normal', bias_initializer='zeros'),
    Dense(8, activation='relu', kernel_initializer='random_normal', bias_initializer='zeros'),
    Dense(1, activation='linear', kernel_initializer='random_normal', bias_initializer='zeros')
])

model.compile(Adam(lr=0.01, beta_1=0.98, beta_2=0.999), loss='mean_absolute_percentage_error')
model.summary()

model.fit_generator(train_data, steps_per_epoch=24, epochs=100)

model.evaluate_generator(test_data)

1 个答案:

答案 0 :(得分:1)

您的最后一个密集层有一个输出。那是故意的吗? 如果您有两个以上的类,则您希望最后一个密集层具有作为输出的类数。

除此之外,您还尝试过降低lr吗? 看起来很高。 您还可以尝试在Conv2D之后添加一个辍学层。 诸如“ Dropout(0.2)”之类的