here is my code.
early_stopping = EarlyStopping(
min_delta=0.001, # minimium amount of change to count as an improvement
patience=5, # how many epochs to wait before stopping
restore_best_weights=True,
)
learning_rate_reduce = k.callbacks.ReduceLROnPlateau(
monitor='val_acc', # Metric to monitor for changes (usually validation accuracy)
patience=2, # Number of epochs with no improvement after which learning rate will be reduced
verbose=1, # Verbosity mode (0: silent, 1: update messages)
factor=0.5, # Factor by which the learning rate will be reduced (e.g., 0.5 means halving)
min_lr=0.00001 # Lower bound for the learning rate (it won't go below this value)
)
learning_rate_schedule = keras.optimizers.schedules.ExponentialDecay(
initial_learning_rate=0.01, # Initial learning rate for training
decay_steps=1, # Number of steps before decaying the learning rate
decay_rate=0.5, # Rate at which the learning rate decreases
)
lr_callback = LearningRateScheduler(learning_rate_schedule)
callback=[ lr_callback , learning_rate_reduce ,early_stopping ]
base_model = keras.applications.NASNetLarge(
weights="imagenet", # Load weights pre-trained on ImageNet.
input_shape=(IMG_SIZE, IMG_SIZE, 3),
include_top=False,
) # Do not include the ImageNet classifier at the top.
# Freeze the base_model
base_model.trainable = False
# Create new model on top
model3 = keras.Sequential([
base_model,
layers.GlobalAveragePooling2D(),
layers.Dropout(0.5),
layers.Dense(2, activation='softmax')
])
model3.summary()
model3.compile(
optimizer=keras.optimizers.Adam(learning_rate = learning_rate_schedule),
# optimizer="adam",
loss='categorical_crossentropy',
metrics=["accuracy"],
)
history = model3.fit(
train_generator2,
steps_per_epoch=train_generator2.samples // batch_size,
epochs=10,
validation_data=valid_generator2,
validation_steps=valid_generator2.samples // batch_size,
callbacks=[callback]
)
I got error
“The output of the
schedule
function should be a float. Got: 0.009999999776482582″
but no errors when using google colab(keras 2.15.0)”
It works in google colab(keras 2.15.0) but it doesn’t work in jupyter notebook in vscode(keras 3.0.0).
New contributor
fjkh777 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.