损失,准确性,val_loss 没有改变

时间:2021-01-28 08:07:00

标签: python tensorflow image-processing keras conv-neural-network

我创建了一个 CNN 模型来对狗和猫进行分类。我还使用 keras 调谐器来搜索最佳超参数。但是当我尝试搜索我的最佳参数时, loss 、accuracy 和 val_loss 值在 3 个 epoch 后没有改变。有什么解决办法吗?

我在下面分享了我的部分代码,

IMAGE_WIDTH=128
IMAGE_HEIGHT=128
IMAGE_SIZE=(IMAGE_WIDTH, IMAGE_HEIGHT)
IMAGE_CHANNELS=3

train_df, validate_df=train_test_split(df,test_size=0.2,random_state=42)
train_df.reset_index(drop=True)
validate_df=validate_df.reset_index(drop=True)

datagen=ImageDataGenerator(rotation_range=90,
                           width_shift_range=0.1,
                           height_shift_range=0.1,
                           brightness_range=(1,1),
                           horizontal_flip=True,
                           vertical_flip=True,
                           shear_range=0.1,
                           rescale=1./255,
                           fill_mode='nearest'
                          )

train_gen=datagen.flow_from_dataframe(train_df,
                                      '/kaggle/working/train',
                                      x_col='filenames',
                                      y_col='category',
                                      target_size=IMAGE_SIZE,
                                      class_mode='categorical',
                                      batch_size=15
                                     )

validation_datagen = ImageDataGenerator(rescale=1./255)
validation_gen = validation_datagen.flow_from_dataframe(
    validate_df, 
    "/kaggle/working/train/", 
    x_col='filenames',
    y_col='category',
    target_size=IMAGE_SIZE,
    class_mode='categorical',
    batch_size=15
)

def optm(hp):
    #with tpu_strategy.scope():
        
    #here
    
        model = keras.Sequential([
            keras.layers.Conv2D(
                filters=hp.Int('conv_1_filter', min_value=32, max_value=128, step=16),
                kernel_size=hp.Choice('conv_1_kernel', values = (3,5)),
                activation='relu',
                input_shape=(IMAGE_WIDTH,IMAGE_HEIGHT,IMAGE_CHANNELS)
            ),
            keras.layers.BatchNormalization(),
            keras.layers.MaxPooling2D(pool_size=(3,3)),
            keras.layers.Dropout(0.25),
            
            
            keras.layers.Conv2D(
                filters=hp.Int('conv_2_filter', min_value=32, max_value=64, step=16),
                kernel_size=hp.Choice('conv_2_kernel', values = (3,5)),
                activation='relu'
            ),
            keras.layers.BatchNormalization(),
            keras.layers.MaxPooling2D(pool_size=(3,3)),
            keras.layers.Dropout(0.2),
            
            
            keras.layers.Flatten(),
            keras.layers.Dense(
                units=hp.Int('dense_1_units', min_value=32, max_value=128, step=16),
                activation='relu'
            ),
            keras.layers.Dense(2, activation='sigmoid')
      ])


        model.compile(optimizer=keras.optimizers.Adam(hp.Choice('learning_rate', values=[1e-2, 1e-3])),
                  loss='categorical_crossentropy',
                  metrics=['accuracy'])

        return model

best_param=RandomSearch(optm,objective='val_accuracy',max_trials=5)

best_param.search(train_gen,validation_data=validation_gen,epochs=10)

运行此代码后,超参数调整得到如下输出,

搜索:运行试验 #1

超参数 |值 |迄今为止的最佳值 conv_1_filter |112 |?
conv_1_kernel |3 |?
conv_2_filter |48 |?
conv_2_kernel |5 |?
Dense_1_units |80 |?
learning_rate |0.01 |?

纪元 1/10 1334/1334 [==============================] - 140 秒 105 毫秒/步 - 损失:0.7605 - 准确度:0.4975 - val_loss :0.6931 - val_accuracy:0.5094 时代 2/10 1334/1334 [==============================] - 140 秒 105 毫秒/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094 时代 3/10 1334/1334 [==============================] - 140 秒 105 毫秒/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094 时代 4/10 1334/1334 [==============================] - 139s 104ms/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094 纪元 5/10 1334/1334 [==============================] - 139s 104ms/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094 时代 6/10 1334/1334 [==============================] - 139s 104ms/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094 时代 7/10 1334/1334 [==============================] - 139s 104ms/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094 时代 8/10 1334/1334 [==============================] - 140 秒 105 毫秒/步 - 损失:0.6931 - 准确度:0.4976 - val_loss :0.6931 - val_accuracy:0.5094

0 个答案:

没有答案
相关问题