The issue you're encountering where the training process uses a learning rate (lr) of 0.001 instead of your specified lr=0.000001 could be due to various reasons, but primarily it might be related to the exact version of TensorFlow and Keras you're using. Starting from TensorFlow 2.x, the Keras API is integrated directly into TensorFlow, and there have been numerous changes and deprecations over versions.
def evaluate_model(model, X_test, y_test_class, y_test_seg):
with tf.keras.utils.custom_object_scope({"segmentation_loss": segmentation_loss}):
# Load the best model weights
best_model = load_model("multitask_best_weights.h5")
# Evaluate the model on test data
test_loss, test_class_loss, test_seg_loss, test_class_acc, test_seg_acc = best_model.evaluate(
X_test, {"classification_output": y_test_class, "segmentation_output": y_test_seg})
print("Test Classification Loss:", test_class_loss)
print("Test Segmentation Loss:", test_seg_loss)
print("Test Classification Accuracy:", test_class_acc)
print("Test Segmentation Accuracy:", test_seg_acc)
# Evaluate the model on validation data
val_loss, val_class_loss, val_seg_loss, val_class_acc, val_seg_acc = best_model.evaluate(
X_val, {'classification_output': y_val_class, 'segmentation_output': y_val_seg})
print("Validation Classification Loss:", val_class_loss)
print("Validation Segmentation Loss:", val_seg_loss)
print("Validation Classification Accuracy:", val_class_acc)
print("Validation Segmentation Accuracy:", val_seg_acc)
# Evaluate the model on training data
train_loss, train_class_loss, train_seg_loss, train_class_acc, train_seg_acc = best_model.evaluate(X_train, {'classification_output': y_train_class, 'segmentation_output': y_train_seg})
print("Train Classification Loss:", train_class_loss)
print("Train Segmentation Loss:", train_seg_loss)
print("Train Classification Accuracy:", train_class_acc)
print("Train Segmentation Accuracy:", train_seg_acc)
# Return test classification accuracy
return test_class_acc
def plot_performance(history):
# Plot classification accuracy
classification_train_accuracy = history.history["classification_output_accuracy"]
classification_val_accuracy = history.history["val_classification_output_accuracy"]
plt.figure(figsize=(7, 3))
plt.plot(classification_train_accuracy, label="Training Accuracy")
plt.plot(classification_val_accuracy, label="Validation Accuracy")
plt.title("Classification Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
# Plot classification loss
classification_train_loss = history.history["classification_output_loss"]
classification_val_loss = history.history["val_classification_output_loss"]
plt.figure(figsize=(7, 3))
plt.plot(classification_train_loss, "b", label="Training Loss")
plt.plot(classification_val_loss, "r", label="Validation Loss")
plt.title("Classification Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.show()
# Plot segmentation accuracy
segmentation_train_accuracy = history.history["segmentation_output_accuracy"]
segmentation_val_accuracy = history.history["val_segmentation_output_accuracy"]
plt.figure(figsize=(7, 3))
plt.plot(segmentation_train_accuracy, label="Training Accuracy")
plt.plot(segmentation_val_accuracy, label="Validation Accuracy")
plt.title("Segmentation Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.show()
# Plot segmentation loss
segmentation_train_loss = history.history["segmentation_output_loss"]
segmentation_val_loss = history.history["val_segmentation_output_loss"]
plt.figure(figsize=(7, 3))
plt.plot(segmentation_train_loss, "b", label="Training Loss")
plt.plot(segmentation_val_loss, "r", label="Validation Loss")
plt.title("Segmentation Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.show()
"
In my code ,why I adjusted the lr=0.000001 but the model training process still use 0.001 as initial learning rate?
Here's a general check and potential fix:
Ensure optimizer syntax is correct: TensorFlow 2.x promotes using learning_rate instead of lr for specifying the learning rate in optimizers. This could be the reason why your specified learning rate is not being applied.
Replace:
optimizer=Adam(lr=0.000001)
With:
optimizer=Adam(learning_rate=0.000001)
Version Check: Make sure you are using a recent version of TensorFlow that supports the change. The lr parameter has been deprecated in favor of learning_rate in newer versions, so if your code isn't running as expected, version differences could be why. You can check your TensorFlow version using:
import tensorflow as tf
print(tf.__version__)
Keras vs. TensorFlow Keras: Earlier versions had separate keras and tensorflow.keras modules, which could sometimes lead to confusion. Ensure that all your imports are consistent (either use TensorFlow's integrated Keras API everywhere or use a standalone Keras version compatible with your TensorFlow version, though the former is recommended).
Global Keras Settings: In rare cases, there might be global settings or previously set configurations that interfere. Restarting your Python kernel/session (especially in Jupyter notebooks or Google Colab) can help ensure a clean state.
If after making the adjustment to learning_rate the issue persists, it would be beneficial to review the TensorFlow and Keras documentation for the version you're using, as other underlying issues or bugs may affect how parameters are passed to or interpreted by the optimizer.
This change to optimizer=Adam(learning_rate=0.000001) should resolve the described behavior, making the training process utilize the intended initial learning rate.