Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
259 views
in Technique[技术] by (71.8m points)

python - loss, accuracy, val_loss is not changing

I have created a CNN model to classify dogs and cats. I also used keras tuner to search best hyperparameters. but when I try to search for my best parameters the loss , accuracy, val_loss values are not changing after 3 epochs. Any solutions for this?

I have shared part of my code below,

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)

after running this code hyperparameter tuning got the output like below ,

Search: Running Trial #1

Hyperparameter |Value |Best Value So Far conv_1_filter |112 |?
conv_1_kernel |3 |?
conv_2_filter |48 |?
conv_2_kernel |5 |?
dense_1_units |80 |?
learning_rate |0.01 |?

Epoch 1/10 1334/1334 [==============================] - 140s 105ms/step - loss: 0.7605 - accuracy: 0.4975 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 2/10 1334/1334 [==============================] - 140s 105ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 3/10 1334/1334 [==============================] - 140s 105ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 4/10 1334/1334 [==============================] - 139s 104ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 5/10 1334/1334 [==============================] - 139s 104ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 6/10 1334/1334 [==============================] - 139s 104ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 7/10 1334/1334 [==============================] - 139s 104ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094 Epoch 8/10 1334/1334 [==============================] - 140s 105ms/step - loss: 0.6931 - accuracy: 0.4976 - val_loss: 0.6931 - val_accuracy: 0.5094

question from:https://stackoverflow.com/questions/65933190/loss-accuracy-val-loss-is-not-changing

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...