Keras ReduceLROnPlateau - How to check if it is applied - tensorflow

Please advise how to confirm if ReduceLROnPlateau has been actually applied and what learning rates have been applied at each epoch.
The patience of ReduceLROnPlateau is set to 2 and monitor var_loss.
self._history = self.model.fit(
self.X.shuffle(1000).batch(self.batch_size).prefetch(1),
epochs=self.num_epochs,
batch_size=self.batch_size,
validation_data=self.V.shuffle(1000).batch(self.batch_size).prefetch(1),
callbacks=[
EaryStoppingCallback(patience=self.early_stop_patience),
ReduceLRCallback(patience=self.reduce_lr_patience), # <---- set to 2
TensorBoardCallback(self.log_directory)
]
)
class ReduceLRCallback(tf.keras.callbacks.ReduceLROnPlateau):
"""Reduce learning rate when a metric has stopped improving.
See https://keras.io/api/callbacks/reduce_lr_on_plateau/
"""
def __init__(self, patience=3):
assert patience > 0
super().__init__(
monitor="val_loss",
factor=0.3,
patience=patience,
)
The training val_loss have increased more than twice but have not seen any information if ReduceLROnPlateau has been applied.
_________________________________________________________________
Epoch 1/20
3990/3990 [==============================] - 860s 214ms/step - loss: 0.1705 - accuracy: 0.9386 - val_loss: 0.1626 - val_accuracy: 0.9456
Epoch 2/20
3990/3990 [==============================] - 847s 212ms/step - loss: 0.1618 - accuracy: 0.9412 - val_loss: 0.1433 - val_accuracy: 0.9456
Epoch 3/20
3990/3990 [==============================] - 846s 212ms/step - loss: 0.1593 - accuracy: 0.9425 - val_loss: 0.1478 - val_accuracy: 0.9438
Epoch 4/20
3990/3990 [==============================] - 846s 212ms/step - loss: 0.1567 - accuracy: 0.9427 - val_loss: 0.1428 - val_accuracy: 0.9468
Epoch 5/20
3990/3990 [==============================] - 846s 212ms/step - loss: 0.1558 - accuracy: 0.9425 - val_loss: 0.1502 - val_accuracy: 0.9425
Epoch 6/20
3990/3990 [==============================] - 843s 211ms/step - loss: 0.1554 - accuracy: 0.9433 - val_loss: 0.1453 - val_accuracy: 0.9456
Epoch 7/20
3990/3990 [==============================] - 843s 211ms/step - loss: 0.1482 - accuracy: 0.9454 - val_loss: 0.1362 - val_accuracy: 0.9477
Epoch 8/20
3990/3990 [==============================] - 843s 211ms/step - loss: 0.1475 - accuracy: 0.9449 - val_loss: 0.1373 - val_accuracy: 0.9471
Epoch 9/20
3990/3990 [==============================] - 845s 212ms/step - loss: 0.1468 - accuracy: 0.9460 - val_loss: 0.1362 - val_accuracy: 0.9485
Epoch 10/20
3990/3990 [==============================] - 843s 211ms/step - loss: 0.1448 - accuracy: 0.9462 - val_loss: 0.1344 - val_accuracy: 0.9489
Epoch 11/20
3990/3990 [==============================] - 846s 212ms/step - loss: 0.1447 - accuracy: 0.9458 - val_loss: 0.1346 - val_accuracy: 0.9483
Epoch 12/20
3990/3990 [==============================] - 843s 211ms/step - loss: 0.1444 - accuracy: 0.9460 - val_loss: 0.1342 - val_accuracy: 0.9483

Marco Cerliani's answer will probably solve it for you. That gives console output so you can verify the LR was actually reduced.
If you needed to check the model's learning rate in code, you can use
tf.keras.backend.get_value(model.optimizer.lr)

Related

Neural network training and validation accuracy stuck at 50% and 48% respectively, loss not decreasing after that and score turns out bad

i've been working on a multiclass classification neural network model on Rstudio with KERAS and Tensorflow where i am trying to achieve at least 70 to 80% accuracy because i had tried tuning the hyperparameters to increase the accuracy but no matter how much that i had tuned on number of epochs, number of layers, or even changing on batch size, both training and validation accuracy doesn't seem to be increasing and training and validation loss stuck at a certain loss value.
this is my model that im working on:
model %>%
layer_dense(units = 28, activation = 'relu',input_shape = c(24), kernel_regularizer = regularizer_l2(0.0001))%>%
layer_dropout(0.01)%>%
layer_dense(units = 28, activation = 'relu', kernel_regularizer = regularizer_l2(0.0001))%>%
layer_dropout(0.01)%>%
layer_dense(units = 28, activation = 'relu', kernel_regularizer = regularizer_l2(0.0001))%>%
layer_dropout(0.01)%>%
layer_dense(units = 28, activation = 'relu', kernel_regularizer = regularizer_l2(0.0001))%>%
layer_dropout(0.01)%>%
layer_dense(units = 28, activation = 'relu', kernel_regularizer = regularizer_l2(0.0001))%>%
layer_dropout(0.01)%>%
layer_dense(units = 28, activation = 'relu', kernel_regularizer = regularizer_l2(0.0001))%>%
layer_dropout(0.01)%>%
layer_dense(units = 8, activation = 'softmax', kernel_regularizer = regularizer_l2(0.0001))
model %>%
compile(loss = 'categorical_crossentropy', optimizer = 'Adam', metrics = 'accuracy')
history <- model%>%
fit(training, trainlabels, epoch = 12000, batch_size = 64, validation_split = 0.2, shuffle = TRUE )
and this is the epoch progression:
Epoch 11900/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3010 - accuracy: 0.5000 - val_loss: 1.3180 - val_accuracy: 0.4823
Epoch 11901/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3008 - accuracy: 0.5007 - val_loss: 1.3326 - val_accuracy: 0.4694
Epoch 11902/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2999 - accuracy: 0.4961 - val_loss: 1.3188 - val_accuracy: 0.4929
Epoch 11903/12000
326/326 [==============================] - 1s 3ms/step - loss: 1.2983 - accuracy: 0.4945 - val_loss: 1.3154 - val_accuracy: 0.4860
Epoch 11904/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3040 - accuracy: 0.4973 - val_loss: 1.3030 - val_accuracy: 0.4887
Epoch 11905/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2904 - accuracy: 0.5021 - val_loss: 1.3125 - val_accuracy: 0.4896
Epoch 11906/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2956 - accuracy: 0.4947 - val_loss: 1.3187 - val_accuracy: 0.4814
Epoch 11907/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3049 - accuracy: 0.4953 - val_loss: 1.3355 - val_accuracy: 0.4727
Epoch 11908/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2978 - accuracy: 0.5025 - val_loss: 1.3250 - val_accuracy: 0.4864
Epoch 11909/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2958 - accuracy: 0.4979 - val_loss: 1.3273 - val_accuracy: 0.4850
Epoch 11910/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2991 - accuracy: 0.4994 - val_loss: 1.3150 - val_accuracy: 0.4864
Epoch 11911/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3040 - accuracy: 0.5006 - val_loss: 1.3458 - val_accuracy: 0.4775
Epoch 11912/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3066 - accuracy: 0.4969 - val_loss: 1.3051 - val_accuracy: 0.4842
Epoch 11913/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3029 - accuracy: 0.4988 - val_loss: 1.3239 - val_accuracy: 0.4885
Epoch 11914/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2890 - accuracy: 0.5040 - val_loss: 1.3321 - val_accuracy: 0.4719
Epoch 11915/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2987 - accuracy: 0.4969 - val_loss: 1.3495 - val_accuracy: 0.4708
Epoch 11916/12000
326/326 [==============================] - 1s 3ms/step - loss: 1.2952 - accuracy: 0.4981 - val_loss: 1.3177 - val_accuracy: 0.4779
Epoch 11917/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2980 - accuracy: 0.4977 - val_loss: 1.3104 - val_accuracy: 0.4904
Epoch 11918/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2985 - accuracy: 0.4990 - val_loss: 1.3220 - val_accuracy: 0.4850
Epoch 11919/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2940 - accuracy: 0.4988 - val_loss: 1.2994 - val_accuracy: 0.4898
Epoch 11920/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3000 - accuracy: 0.5001 - val_loss: 1.3183 - val_accuracy: 0.4865
Epoch 11921/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2957 - accuracy: 0.4991 - val_loss: 1.3215 - val_accuracy: 0.4841
Epoch 11922/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2930 - accuracy: 0.5024 - val_loss: 1.3205 - val_accuracy: 0.4823
Epoch 11923/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3026 - accuracy: 0.4972 - val_loss: 1.3034 - val_accuracy: 0.4864
Epoch 11924/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2943 - accuracy: 0.4995 - val_loss: 1.3423 - val_accuracy: 0.4812
Epoch 11925/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3033 - accuracy: 0.4998 - val_loss: 1.3082 - val_accuracy: 0.4875
Epoch 11926/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3032 - accuracy: 0.4977 - val_loss: 1.3241 - val_accuracy: 0.4806
Epoch 11927/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2988 - accuracy: 0.4975 - val_loss: 1.3132 - val_accuracy: 0.4827
Epoch 11928/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3064 - accuracy: 0.4952 - val_loss: 1.3422 - val_accuracy: 0.4794
Epoch 11929/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2988 - accuracy: 0.4953 - val_loss: 1.3279 - val_accuracy: 0.4892
Epoch 11930/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3015 - accuracy: 0.4994 - val_loss: 1.3181 - val_accuracy: 0.4779
Epoch 11931/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2967 - accuracy: 0.4990 - val_loss: 1.3400 - val_accuracy: 0.4858
Epoch 11932/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3042 - accuracy: 0.4968 - val_loss: 1.3152 - val_accuracy: 0.4867
Epoch 11933/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3052 - accuracy: 0.4958 - val_loss: 1.3246 - val_accuracy: 0.4864
Epoch 11934/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2974 - accuracy: 0.4971 - val_loss: 1.3155 - val_accuracy: 0.4841
Epoch 11935/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2999 - accuracy: 0.4975 - val_loss: 1.3182 - val_accuracy: 0.4940
Epoch 11936/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3013 - accuracy: 0.5006 - val_loss: 1.3594 - val_accuracy: 0.4660
Epoch 11937/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2975 - accuracy: 0.4955 - val_loss: 1.3295 - val_accuracy: 0.4779
Epoch 11938/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2925 - accuracy: 0.4983 - val_loss: 1.3597 - val_accuracy: 0.4731
Epoch 11939/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3017 - accuracy: 0.4991 - val_loss: 1.3321 - val_accuracy: 0.4854
Epoch 11940/12000
326/326 [==============================] - 1s 3ms/step - loss: 1.2992 - accuracy: 0.4985 - val_loss: 1.3251 - val_accuracy: 0.4831
Epoch 11941/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3023 - accuracy: 0.4955 - val_loss: 1.3430 - val_accuracy: 0.4781
Epoch 11942/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2984 - accuracy: 0.5010 - val_loss: 1.3113 - val_accuracy: 0.4933
Epoch 11943/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3000 - accuracy: 0.4972 - val_loss: 1.3267 - val_accuracy: 0.4783
Epoch 11944/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2963 - accuracy: 0.4989 - val_loss: 1.3713 - val_accuracy: 0.4671
Epoch 11945/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3088 - accuracy: 0.4945 - val_loss: 1.3122 - val_accuracy: 0.4862
Epoch 11946/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3062 - accuracy: 0.4965 - val_loss: 1.3142 - val_accuracy: 0.4802
Epoch 11947/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3005 - accuracy: 0.4993 - val_loss: 1.3279 - val_accuracy: 0.4852
Epoch 11948/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3069 - accuracy: 0.4953 - val_loss: 1.3111 - val_accuracy: 0.4873
Epoch 11949/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3035 - accuracy: 0.4966 - val_loss: 1.3289 - val_accuracy: 0.4867
Epoch 11950/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3024 - accuracy: 0.4963 - val_loss: 1.3416 - val_accuracy: 0.4816
Epoch 11951/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2966 - accuracy: 0.5009 - val_loss: 1.3485 - val_accuracy: 0.4727
Epoch 11952/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2989 - accuracy: 0.4978 - val_loss: 1.3178 - val_accuracy: 0.4829
Epoch 11953/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2961 - accuracy: 0.4982 - val_loss: 1.3267 - val_accuracy: 0.4785
Epoch 11954/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3022 - accuracy: 0.4989 - val_loss: 1.3302 - val_accuracy: 0.4823
Epoch 11955/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2982 - accuracy: 0.4957 - val_loss: 1.3090 - val_accuracy: 0.4839
Epoch 11956/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2947 - accuracy: 0.5020 - val_loss: 1.3337 - val_accuracy: 0.4791
Epoch 11957/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2881 - accuracy: 0.4985 - val_loss: 1.3179 - val_accuracy: 0.4817
Epoch 11958/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2929 - accuracy: 0.5023 - val_loss: 1.3211 - val_accuracy: 0.4792
Epoch 11959/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2977 - accuracy: 0.4980 - val_loss: 1.3222 - val_accuracy: 0.4800
Epoch 11960/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2992 - accuracy: 0.4972 - val_loss: 1.3390 - val_accuracy: 0.4769
Epoch 11961/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3014 - accuracy: 0.5003 - val_loss: 1.3238 - val_accuracy: 0.4856
Epoch 11962/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3005 - accuracy: 0.4960 - val_loss: 1.3300 - val_accuracy: 0.4789
Epoch 11963/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3025 - accuracy: 0.4987 - val_loss: 1.3037 - val_accuracy: 0.4802
Epoch 11964/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2983 - accuracy: 0.4969 - val_loss: 1.3381 - val_accuracy: 0.4792
Epoch 11965/12000
326/326 [==============================] - 1s 3ms/step - loss: 1.2969 - accuracy: 0.4982 - val_loss: 1.3007 - val_accuracy: 0.4871
Epoch 11966/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3018 - accuracy: 0.4967 - val_loss: 1.3417 - val_accuracy: 0.4837
Epoch 11967/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2987 - accuracy: 0.4970 - val_loss: 1.3202 - val_accuracy: 0.4860
Epoch 11968/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2931 - accuracy: 0.5005 - val_loss: 1.3095 - val_accuracy: 0.4871
Epoch 11969/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2977 - accuracy: 0.4967 - val_loss: 1.3038 - val_accuracy: 0.4860
Epoch 11970/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2956 - accuracy: 0.4974 - val_loss: 1.3440 - val_accuracy: 0.4833
Epoch 11971/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3007 - accuracy: 0.5018 - val_loss: 1.3378 - val_accuracy: 0.4777
Epoch 11972/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2973 - accuracy: 0.4972 - val_loss: 1.3173 - val_accuracy: 0.4848
Epoch 11973/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2901 - accuracy: 0.4974 - val_loss: 1.3145 - val_accuracy: 0.4814
Epoch 11974/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2952 - accuracy: 0.4995 - val_loss: 1.3260 - val_accuracy: 0.4850
Epoch 11975/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2976 - accuracy: 0.4982 - val_loss: 1.3476 - val_accuracy: 0.4677
Epoch 11976/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2979 - accuracy: 0.5014 - val_loss: 1.3270 - val_accuracy: 0.4754
Epoch 11977/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2991 - accuracy: 0.5001 - val_loss: 1.3295 - val_accuracy: 0.4779
Epoch 11978/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2974 - accuracy: 0.4973 - val_loss: 1.3053 - val_accuracy: 0.4931
Epoch 11979/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2963 - accuracy: 0.4958 - val_loss: 1.3156 - val_accuracy: 0.4919
Epoch 11980/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2962 - accuracy: 0.5031 - val_loss: 1.3201 - val_accuracy: 0.4791
Epoch 11981/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2908 - accuracy: 0.4987 - val_loss: 1.3110 - val_accuracy: 0.4881
Epoch 11982/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3048 - accuracy: 0.4988 - val_loss: 1.3283 - val_accuracy: 0.4798
Epoch 11983/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3063 - accuracy: 0.4973 - val_loss: 1.3170 - val_accuracy: 0.4821
Epoch 11984/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3019 - accuracy: 0.4958 - val_loss: 1.3084 - val_accuracy: 0.4835
Epoch 11985/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2981 - accuracy: 0.4977 - val_loss: 1.3381 - val_accuracy: 0.4696
Epoch 11986/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3006 - accuracy: 0.4969 - val_loss: 1.3087 - val_accuracy: 0.4827
Epoch 11987/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3093 - accuracy: 0.4911 - val_loss: 1.3154 - val_accuracy: 0.4889
Epoch 11988/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2981 - accuracy: 0.5002 - val_loss: 1.3164 - val_accuracy: 0.4827
Epoch 11989/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3022 - accuracy: 0.4938 - val_loss: 1.3202 - val_accuracy: 0.4808
Epoch 11990/12000
326/326 [==============================] - 1s 3ms/step - loss: 1.2953 - accuracy: 0.4960 - val_loss: 1.3113 - val_accuracy: 0.4908
Epoch 11991/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2984 - accuracy: 0.4963 - val_loss: 1.3170 - val_accuracy: 0.4839
Epoch 11992/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2917 - accuracy: 0.5012 - val_loss: 1.3479 - val_accuracy: 0.4769
Epoch 11993/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3008 - accuracy: 0.4955 - val_loss: 1.3423 - val_accuracy: 0.4833
Epoch 11994/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3007 - accuracy: 0.4984 - val_loss: 1.3168 - val_accuracy: 0.4850
Epoch 11995/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3020 - accuracy: 0.4966 - val_loss: 1.3266 - val_accuracy: 0.4754
Epoch 11996/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2979 - accuracy: 0.4993 - val_loss: 1.3091 - val_accuracy: 0.4808
Epoch 11997/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2962 - accuracy: 0.5026 - val_loss: 1.3339 - val_accuracy: 0.4792
Epoch 11998/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3100 - accuracy: 0.4973 - val_loss: 1.3150 - val_accuracy: 0.4833
Epoch 11999/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.3036 - accuracy: 0.4978 - val_loss: 1.3295 - val_accuracy: 0.4819
Epoch 12000/12000
326/326 [==============================] - 1s 2ms/step - loss: 1.2901 - accuracy: 0.5007 - val_loss: 1.3061 - val_accuracy: 0.4802
but after all the trainings, the scores concludes
loss: 1.3303 - accuracy: 0.4683
the accuracy and the loss is as shown below:
accuracy of the model
loss of the model
Loss and Accuracy
am i underfitting the training model?? how can i actually increase the accuracy and letting the model to learn something?

Unclear difference between progress output in TensorFlow 2.3.0 and 1.15.0 in same code

I am new in ML. I have installed two tensorflows in two anaconda environments 1.15.0 and 2.3.0. (1.15.0 for be able to use my old GTX 660 videocard) and saw the difference in output progress info when training the same model.
Сode from book "Deep Learning with Python" by François Chollet:
import numpy as np
import os
data_dir='C:/Users/Username/_JupyterDocs/sund/data'
fname = os.path.join(data_dir, 'jena_climate_2009_2016.csv')
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="-1"
f = open(fname)
data = f.read()
f.close()
lines = data.split('\n')
header = lines[0].split(',')
lines = lines[1:]
float_data = np.zeros((len(lines), len(header) - 1))
for i, line in enumerate(lines):
values = [float(x) for x in line.split(',')[1:]]
float_data[i, :] = values
mean = float_data[:200000].mean(axis=0)
float_data -= mean
std = float_data[:200000].std(axis=0)
float_data /= std
def generator(data, lookback, delay, min_index, max_index, shuffle=False, batch_size=128, step=6):
if max_index is None:
max_index = len(data) - delay - 1
i = min_index + lookback
while 1:
if shuffle:
rows = np.random.randint(min_index + lookback, max_index, size=batch_size)
else:
if i + batch_size >= max_index:
i = min_index + lookback
rows = np.arange(i, min(i + batch_size, max_index))
i += len(rows)
samples = np.zeros((len(rows), lookback // step, data.shape[-1]))
targets = np.zeros((len(rows),))
for j, row in enumerate(rows):
indices = range(rows[j] - lookback, rows[j], step)
samples[j] = data[indices]
targets[j] = data[rows[j] + delay ][1]
yield samples, targets
lookback = 1440
step = 6
delay = 144
batch_size = 128
train_gen = generator(float_data,
lookback=lookback,
delay=delay,
min_index=0,
max_index=200000,
shuffle=True,
step=step,
batch_size=batch_size)
val_gen = generator(float_data,
lookback=lookback,
delay=delay,
min_index=200001,
max_index=300000,
step=step,
batch_size=batch_size)
test_gen = generator(float_data,
lookback=lookback,
delay=delay,
min_index=300001,
max_index=None,
step=step,
batch_size=batch_size)
val_steps = (300000 - 200001 - lookback) // batch_size
test_steps = (len(float_data) - 300001 - lookback) // batch_size
import time
from tensorflow.keras.models import Sequential
from tensorflow.keras import layers
from tensorflow.keras.optimizers import RMSprop
model = Sequential()
model.add(layers.GRU(32, input_shape=(None, float_data.shape[-1])))
model.add(layers.Dense(1))
model.compile(optimizer=RMSprop(), loss='mae')
start = time.perf_counter()
history = model.fit_generator(train_gen,
steps_per_epoch=500,
epochs=20,
validation_data=val_gen,
validation_steps=val_steps,
verbose=1)
elapsed = time.perf_counter() - start
f = open("C:/Users/Username/Desktop/log1.txt", "a")
f.write('Elapsed %.3f seconds.' % elapsed)
f.close()
print('Elapsed %.3f seconds.' % elapsed)
TF 2.3.0 progress output:
-Warning about deprecated in output:
WARNING:tensorflow:From C:\Users\Username\AppData\Local\Temp/ipykernel_10804/2601851929.py:13: Model.fit_generator (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version. Instructions for updating: Please use Model.fit, which supports generators.
-Output:
Epoch 1/20
500/500 [==============================] - 45s 89ms/step - loss: 0.3050 - val_loss: 0.2686
Epoch 2/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2841 - val_loss: 0.2658
Epoch 3/20
500/500 [==============================] - 46s 92ms/step - loss: 0.2771 - val_loss: 0.2653
Epoch 4/20
500/500 [==============================] - 46s 91ms/step - loss: 0.2729 - val_loss: 0.2795
Epoch 5/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2690 - val_loss: 0.2644
Epoch 6/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2632 - val_loss: 0.2673
Epoch 7/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2602 - val_loss: 0.2641
Epoch 8/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2549 - val_loss: 0.2667
Epoch 9/20
500/500 [==============================] - 45s 91ms/step - loss: 0.2507 - val_loss: 0.2768
Epoch 10/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2447 - val_loss: 0.2785
Epoch 11/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2422 - val_loss: 0.2763
Epoch 12/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2354 - val_loss: 0.2794
Epoch 13/20
500/500 [==============================] - 46s 92ms/step - loss: 0.2320 - val_loss: 0.2807
Epoch 14/20
500/500 [==============================] - 45s 89ms/step - loss: 0.2277 - val_loss: 0.2848
Epoch 15/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2222 - val_loss: 0.2909
Epoch 16/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2178 - val_loss: 0.2910
Epoch 17/20
500/500 [==============================] - 45s 89ms/step - loss: 0.2152 - val_loss: 0.2918
Epoch 18/20
500/500 [==============================] - 45s 90ms/step - loss: 0.2112 - val_loss: 0.2917
Epoch 19/20
500/500 [==============================] - 44s 89ms/step - loss: 0.2103 - val_loss: 0.2979
Epoch 20/20
500/500 [==============================] - 45s 89ms/step - loss: 0.2068 - val_loss: 0.2986
Elapsed 904.779 seconds.
TF 1.15.0 progress output:
-Warning about deprecated in output:
WARNING:tensorflow:From C:\Users\Username\anaconda3\envs\tf-gpu\lib\site-packages\tensorflow_core\python\ops\resource_variable_ops.py:1630: calling BaseResourceVariable.init (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers.
Output:
Epoch 1/20
WARNING:tensorflow:From C:\Users\Username\anaconda3\envs\tf-gpu\lib\site-packages\tensorflow_core\python\ops\math_grad.py:1424: where (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.where in 2.0, which has the same broadcast rule as np.where
499/500 [============================>.] - ETA: 0s - loss: 0.3014Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2285
500/500 [==============================] - 63s 126ms/step - loss: 0.3014 - val_loss: 0.2686
Epoch 2/20
499/500 [============================>.] - ETA: 0s - loss: 0.2836Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2225
500/500 [==============================] - 62s 123ms/step - loss: 0.2836 - val_loss: 0.2667
Epoch 3/20
499/500 [============================>.] - ETA: 0s - loss: 0.2761Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3162
500/500 [==============================] - 62s 123ms/step - loss: 0.2762 - val_loss: 0.2721
Epoch 4/20
499/500 [============================>.] - ETA: 0s - loss: 0.2731Epoch 1/20
769/500 [==============================================] - 16s 21ms/step - loss: 0.2422
500/500 [==============================] - 62s 124ms/step - loss: 0.2730 - val_loss: 0.2667
Epoch 5/20
499/500 [============================>.] - ETA: 0s - loss: 0.2667Epoch 1/20
769/500 [==============================================] - 16s 21ms/step - loss: 0.3732
500/500 [==============================] - 61s 122ms/step - loss: 0.2667 - val_loss: 0.2663
Epoch 6/20
499/500 [============================>.] - ETA: 0s - loss: 0.2613Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2088
500/500 [==============================] - 62s 124ms/step - loss: 0.2613 - val_loss: 0.2648
Epoch 7/20
499/500 [============================>.] - ETA: 0s - loss: 0.2544Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3043
500/500 [==============================] - 62s 125ms/step - loss: 0.2544 - val_loss: 0.2710
Epoch 8/20
499/500 [============================>.] - ETA: 0s - loss: 0.2493Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2767
500/500 [==============================] - 63s 127ms/step - loss: 0.2493 - val_loss: 0.2717
Epoch 9/20
499/500 [============================>.] - ETA: 0s - loss: 0.2455Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2336
500/500 [==============================] - 62s 124ms/step - loss: 0.2455 - val_loss: 0.2743
Epoch 10/20
499/500 [============================>.] - ETA: 0s - loss: 0.2406Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3041
500/500 [==============================] - 63s 126ms/step - loss: 0.2406 - val_loss: 0.2776
Epoch 11/20
499/500 [============================>.] - ETA: 0s - loss: 0.2345Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2655
500/500 [==============================] - 62s 124ms/step - loss: 0.2344 - val_loss: 0.2779
Epoch 12/20
499/500 [============================>.] - ETA: 0s - loss: 0.2310Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3085
500/500 [==============================] - 62s 124ms/step - loss: 0.2310 - val_loss: 0.2800
Epoch 13/20
499/500 [============================>.] - ETA: 0s - loss: 0.2271Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3029
500/500 [==============================] - 64s 127ms/step - loss: 0.2271 - val_loss: 0.2839
Epoch 14/20
499/500 [============================>.] - ETA: 0s - loss: 0.2226Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3110
500/500 [==============================] - 62s 125ms/step - loss: 0.2226 - val_loss: 0.2886
Epoch 15/20
499/500 [============================>.] - ETA: 0s - loss: 0.2190Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3329
500/500 [==============================] - 62s 123ms/step - loss: 0.2190 - val_loss: 0.2919
Epoch 16/20
499/500 [============================>.] - ETA: 0s - loss: 0.2170Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3022
500/500 [==============================] - 62s 125ms/step - loss: 0.2170 - val_loss: 0.2937
Epoch 17/20
499/500 [============================>.] - ETA: 0s - loss: 0.2132Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2463
500/500 [==============================] - 62s 124ms/step - loss: 0.2132 - val_loss: 0.3004
Epoch 18/20
499/500 [============================>.] - ETA: 0s - loss: 0.2101Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.3423
500/500 [==============================] - 62s 124ms/step - loss: 0.2101 - val_loss: 0.3018
Epoch 19/20
499/500 [============================>.] - ETA: 0s - loss: 0.2072Epoch 1/20
769/500 [==============================================] - 17s 23ms/step - loss: 0.2689
500/500 [==============================] - 62s 125ms/step - loss: 0.2073 - val_loss: 0.3045
Epoch 20/20
499/500 [============================>.] - ETA: 0s - loss: 0.2066Epoch 1/20
769/500 [==============================================] - 17s 22ms/step - loss: 0.2809
500/500 [==============================] - 62s 124ms/step - loss: 0.2066 - val_loss: 0.2978
Elapsed 1245.008 seconds.
What the two additional progress bar in each epoch in TF 1.15.0 output?
From Documentation:
verbose: Integer. 0, 1, or 2. Verbosity mode. 0 = silent, 1 = progress
bar, 2 = one line per epoch
Default is 1.
and this is internal TensorFlow warning, you can safely ignore. which is tell us about future versions of TensorFlow, no actions from your side is needed.

Validation accuracy zero and Loss is higher. Intent classification Using LSTM

I'm trying to Build and LSTM model for intent classification using Tensorflow, Keras. But whenever I'm training the model with 30 or 40 epochs, my 1st 20 validation accuracy is zero and loss is more than accuracy. and if I try to change the code a little bit, validation accuracy is getting lower than Loss.
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
embedding (Embedding) (None, 100, 16) 16000
_________________________________________________________________
bidirectional (Bidirectional (None, 64) 12544
_________________________________________________________________
dense (Dense) (None, 24) 1560
_________________________________________________________________
dense_1 (Dense) (None, 3) 75
=================================================================
Total params: 30,179
Trainable params: 30,179
Non-trainable params: 0
_________________________________________________________________
Train on 200 samples, validate on 79 samples
loss='sparse_categorical_crossentropy',optimizer='adam',metrics=['accuracy']
Epoch 1/40
2020-08-16 14:08:17.986786: W tensorflow/core/grappler/optimizers/implementation_selector.cc:310] Skipping optimization due to error while loading function libraries: Invalid argument: Functions '__inference___backward_standard_lstm_4932_5417' and '__inference___backward_standard_lstm_4932_5417_specialized_for_StatefulPartitionedCall_at___inference_distributed_function_6117' both implement 'lstm_6b97c168-2c7b-4dc3-93dd-5e68cddc574f' but their signatures do not match.
2020-08-16 14:08:20.550894: W tensorflow/core/grappler/optimizers/implementation_selector.cc:310] Skipping optimization due to error while loading function libraries: Invalid argument: Functions '__inference_standard_lstm_6338' and '__inference_standard_lstm_6338_specialized_for_sequential_bidirectional_forward_lstm_StatefulPartitionedCall_at___inference_distributed_function_7185' both implement 'lstm_62f19cc7-a1f0-447b-a17f-84a70fc095cd' but their signatures do not match.
200/200 - 10s - loss: 1.0798 - accuracy: 0.7850 - val_loss: 1.1327 - val_accuracy: 0.0000e+00
Epoch 2/40
200/200 - 1s - loss: 1.0286 - accuracy: 0.7850 - val_loss: 1.1956 - val_accuracy: 0.0000e+00
Epoch 3/40
200/200 - 1s - loss: 0.9294 - accuracy: 0.7850 - val_loss: 1.4287 - val_accuracy: 0.0000e+00
Epoch 4/40
200/200 - 1s - loss: 0.7026 - accuracy: 0.7850 - val_loss: 2.2190 - val_accuracy: 0.0000e+00
Epoch 5/40
200/200 - 1s - loss: 0.6183 - accuracy: 0.7850 - val_loss: 1.8499 - val_accuracy: 0.0000e+00
Epoch 6/40
200/200 - 1s - loss: 0.5980 - accuracy: 0.7850 - val_loss: 1.5809 - val_accuracy: 0.0000e+00
Epoch 7/40
200/200 - 1s - loss: 0.5927 - accuracy: 0.7850 - val_loss: 1.5118 - val_accuracy: 0.0000e+00
Epoch 8/40
200/200 - 1s - loss: 0.5861 - accuracy: 0.7850 - val_loss: 1.5711 - val_accuracy: 0.0000e+00
Epoch 9/40
200/200 - 1s - loss: 0.5728 - accuracy: 0.7850 - val_loss: 1.5106 - val_accuracy: 0.0000e+00
Epoch 10/40
200/200 - 1s - loss: 0.5509 - accuracy: 0.7850 - val_loss: 1.6389 - val_accuracy: 0.0000e+00
Epoch 11/40
200/200 - 1s - loss: 0.5239 - accuracy: 0.7850 - val_loss: 1.5991 - val_accuracy: 0.0000e+00
Epoch 12/40
200/200 - 1s - loss: 0.4860 - accuracy: 0.7850 - val_loss: 1.4903 - val_accuracy: 0.0000e+00
Epoch 13/40
200/200 - 1s - loss: 0.4388 - accuracy: 0.7850 - val_loss: 1.3937 - val_accuracy: 0.0000e+00
Epoch 14/40
200/200 - 1s - loss: 0.3859 - accuracy: 0.7850 - val_loss: 1.2329 - val_accuracy: 0.0000e+00
Epoch 15/40
200/200 - 1s - loss: 0.3460 - accuracy: 0.7850 - val_loss: 1.1700 - val_accuracy: 0.0000e+00
Epoch 16/40
200/200 - 1s - loss: 0.3323 - accuracy: 0.7850 - val_loss: 1.0077 - val_accuracy: 0.0127
Epoch 17/40
200/200 - 1s - loss: 0.3007 - accuracy: 0.8150 - val_loss: 1.2465 - val_accuracy: 0.2278
Epoch 18/40
200/200 - 0s - loss: 0.2752 - accuracy: 0.9200 - val_loss: 0.8890 - val_accuracy: 0.6329
Epoch 19/40
200/200 - 1s - loss: 0.2613 - accuracy: 0.9700 - val_loss: 0.9181 - val_accuracy: 0.6582
Epoch 20/40
200/200 - 1s - loss: 0.2447 - accuracy: 0.9600 - val_loss: 0.8786 - val_accuracy: 0.7468
Epoch 21/40
200/200 - 1s - loss: 0.2171 - accuracy: 0.9700 - val_loss: 0.7162 - val_accuracy: 0.8481
Epoch 22/40
200/200 - 1s - loss: 0.1949 - accuracy: 0.9700 - val_loss: 0.8051 - val_accuracy: 0.7848
Epoch 23/40
200/200 - 1s - loss: 0.1654 - accuracy: 0.9700 - val_loss: 0.4710 - val_accuracy: 0.8861
Epoch 24/40
200/200 - 1s - loss: 0.1481 - accuracy: 0.9700 - val_loss: 0.4209 - val_accuracy: 0.8861
Epoch 25/40
200/200 - 1s - loss: 0.1192 - accuracy: 0.9700 - val_loss: 0.3792 - val_accuracy: 0.8861
Epoch 26/40
200/200 - 1s - loss: 0.1022 - accuracy: 0.9700 - val_loss: 0.7279 - val_accuracy: 0.8101
Epoch 27/40
200/200 - 1s - loss: 0.0995 - accuracy: 0.9700 - val_loss: 1.3112 - val_accuracy: 0.6582
Epoch 28/40
200/200 - 1s - loss: 0.1161 - accuracy: 0.9650 - val_loss: 0.1435 - val_accuracy: 0.9747
Epoch 29/40
200/200 - 1s - loss: 0.0889 - accuracy: 0.9700 - val_loss: 0.3896 - val_accuracy: 0.8608
Epoch 30/40
200/200 - 1s - loss: 0.0830 - accuracy: 0.9700 - val_loss: 0.3840 - val_accuracy: 0.8608
Epoch 31/40
200/200 - 1s - loss: 0.0688 - accuracy: 0.9700 - val_loss: 0.3100 - val_accuracy: 0.9241
Epoch 32/40
200/200 - 1s - loss: 0.0611 - accuracy: 0.9700 - val_loss: 0.3524 - val_accuracy: 0.8987
Epoch 33/40
200/200 - 1s - loss: 0.0518 - accuracy: 0.9750 - val_loss: 0.4621 - val_accuracy: 0.8481
Epoch 34/40
200/200 - 1s - loss: 0.0457 - accuracy: 0.9900 - val_loss: 0.4344 - val_accuracy: 0.8481
Epoch 35/40
200/200 - 1s - loss: 0.0423 - accuracy: 0.9900 - val_loss: 0.4417 - val_accuracy: 0.8608
Epoch 36/40
200/200 - 1s - loss: 0.0372 - accuracy: 0.9900 - val_loss: 0.4701 - val_accuracy: 0.8481
Epoch 37/40
200/200 - 1s - loss: 0.0319 - accuracy: 0.9950 - val_loss: 0.3913 - val_accuracy: 0.8608
Epoch 38/40
200/200 - 1s - loss: 0.0309 - accuracy: 0.9950 - val_loss: 0.5739 - val_accuracy: 0.7975
Epoch 39/40
200/200 - 1s - loss: 0.0290 - accuracy: 0.9950 - val_loss: 0.5416 - val_accuracy: 0.8228
Epoch 40/40
200/200 - 1s - loss: 0.0292 - accuracy: 1.0000 - val_loss: 0.3162 - val_accuracy: 0.8861
There can be multiple reasons for the validation accuracy to be zero, you can check on these below things to make changes accordingly.
The samples you had taken are very less, train on 200 samples and validate on 79 samples, you can try increasing samples through some upsampling methods.
There is a possibility that there can be unseen data on validation and weights are not useful with unseen data.
For example, if you have digits from 1-9 to classify, if you keep 1-7 for training and 8-9 for validation this data becomes an unseen scenario and val_acc will be 0.
To fix this, shuffle the data before sending it to model.fit.
This can be a silly mistake, check if your validation data is mapped properly to its label.
You can also give validation data explicitly like below.
model.fit(X_train, y_train, batch_size = 32, nb_epoch=30,
shuffle=True,verbose=1,callbacks=[remote, early_stopping],
validation_data (X_validation, y_validation))

KerasLayer vs tf.keras.applications performances

I've trained some networks with ResNetV2 50 ( https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4 ) and it work very well for my datasets.
Then I tried tf.keras.applications.ResNet50 and accuracy is very lower than the other.
Here two models:
The first (with hub)
base_model = hub.KerasLayer('https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4', input_shape=(IMAGE_H, IMAGE_W, 3))
base_model.trainable = False
model = tf.keras.Sequential([
base_model ,
Dense(num_classes, activation='softmax')
])
The second (with keras.applications)
base_model = tf.keras.applications.ResNet50V2(input_shape=(IMAGE_H, IMAGE_W, 3), include_top=False, weights='imagenet', pooling='avg')
base_model.trainable = False
model = tf.keras.Sequential([
base_model,
Dense(num_classes, activation='softmax')
])
The optimizer is the same (Adam), epochs, steps, dataset (train and validation), learning rate are the same as well. But the first start with a val_accuracy near 80% and end with an accuracy near 99%, the second start with 85% of val_accuracy from first to last epoch, as it's overfitting. I got the same behavior changing dataset and parameters for each model.
What am I doing wrong?
Both tf.keras.applications.ResNet50V2 and hub.KerasLayer for ResNet50V2 are trained on the same image_net dataset and share the same weights, there is no difference between both.
Coming to the accuracy difference, I have tried both the API's to load base model and run on 10 epochs.
I saw a minor difference in the accuracy of both training and validation.
Then again after running the TensorFlow hub base model for the second time, again I found some minor change in accuracy.This may be due to the order of calculation of these and also could be floating point precision error.
Below are the outputs:
tf.keras.applications.ResNet50V2 as base model.
base_model = tf.keras.applications.ResNet50V2(input_shape=(96, 96, 3), include_top=False, weights='imagenet', pooling='avg')
base_model.trainable = False
model = tf.keras.Sequential([
base_model,
Dense(10, activation='softmax')
])
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=16),
validation_data=(testX, testY), steps_per_epoch=len(trainX) // 16,
epochs=10)
Output 1:
Epoch 1/10
103/103 [==============================] - 55s 535ms/step - loss: 1.5820 - accuracy: 0.4789 - val_loss: 0.9162 - val_accuracy: 0.6949
Epoch 2/10
103/103 [==============================] - 57s 554ms/step - loss: 0.9539 - accuracy: 0.6534 - val_loss: 0.8376 - val_accuracy: 0.6852
Epoch 3/10
103/103 [==============================] - 55s 532ms/step - loss: 0.8610 - accuracy: 0.6944 - val_loss: 0.7104 - val_accuracy: 0.7240
Epoch 4/10
103/103 [==============================] - 55s 533ms/step - loss: 0.7671 - accuracy: 0.7214 - val_loss: 0.5988 - val_accuracy: 0.7918
Epoch 5/10
103/103 [==============================] - 55s 536ms/step - loss: 0.6994 - accuracy: 0.7526 - val_loss: 0.6029 - val_accuracy: 0.7676
Epoch 6/10
103/103 [==============================] - 55s 537ms/step - loss: 0.6880 - accuracy: 0.7508 - val_loss: 0.6121 - val_accuracy: 0.7724
Epoch 7/10
103/103 [==============================] - 55s 533ms/step - loss: 0.6588 - accuracy: 0.7593 - val_loss: 0.5486 - val_accuracy: 0.8015
Epoch 8/10
103/103 [==============================] - 55s 534ms/step - loss: 0.6640 - accuracy: 0.7630 - val_loss: 0.5287 - val_accuracy: 0.8232
Epoch 9/10
103/103 [==============================] - 54s 528ms/step - loss: 0.6004 - accuracy: 0.7881 - val_loss: 0.4598 - val_accuracy: 0.8426
Epoch 10/10
103/103 [==============================] - 55s 530ms/step - loss: 0.5583 - accuracy: 0.8016 - val_loss: 0.4605 - val_accuracy: 0.8426
Tensorflow Hub as base model.
tf.keras.backend.clear_session()
np.random.seed(42)
tf.random.set_seed(42)
base_model =hub.KerasLayer('https://tfhub.dev/google/imagenet/resnet_v2_50/feature_vector/4', input_shape=(96, 96, 3))
base_model.trainable = False
model = tf.keras.Sequential([base_model ,Dense(10, activation='softmax')])
model.compile(loss="categorical_crossentropy",optimizer="adam",metrics=["accuracy"])
H = model.fit_generator(aug.flow(trainX, trainY, batch_size=16),
validation_data=(testX, testY), steps_per_epoch=len(trainX) // 16,
epochs=10)
Output 2:
Epoch 1/10
103/103 [==============================] - 54s 526ms/step - loss: 1.7543 - accuracy: 0.4464 - val_loss: 1.0185 - val_accuracy: 0.5981
Epoch 2/10
103/103 [==============================] - 53s 519ms/step - loss: 0.9827 - accuracy: 0.6283 - val_loss: 1.0067 - val_accuracy: 0.6416
Epoch 3/10
103/103 [==============================] - 56s 548ms/step - loss: 0.8719 - accuracy: 0.6944 - val_loss: 0.7195 - val_accuracy: 0.7240
Epoch 4/10
103/103 [==============================] - 54s 521ms/step - loss: 0.8177 - accuracy: 0.7208 - val_loss: 0.7490 - val_accuracy: 0.7385
Epoch 5/10
103/103 [==============================] - 54s 522ms/step - loss: 0.7641 - accuracy: 0.7379 - val_loss: 0.6325 - val_accuracy: 0.7797
Epoch 6/10
103/103 [==============================] - 54s 523ms/step - loss: 0.6551 - accuracy: 0.7655 - val_loss: 0.6431 - val_accuracy: 0.7579
Epoch 7/10
103/103 [==============================] - 55s 530ms/step - loss: 0.6538 - accuracy: 0.7734 - val_loss: 0.5824 - val_accuracy: 0.7797
Epoch 8/10
103/103 [==============================] - 55s 536ms/step - loss: 0.6387 - accuracy: 0.7691 - val_loss: 0.6254 - val_accuracy: 0.7772
Epoch 9/10
103/103 [==============================] - 56s 540ms/step - loss: 0.6394 - accuracy: 0.7685 - val_loss: 0.6539 - val_accuracy: 0.7554
Epoch 10/10
103/103 [==============================] - 55s 536ms/step - loss: 0.5816 - accuracy: 0.7955 - val_loss: 0.5703 - val_accuracy: 0.7990
Now if I run the Tensorflow hub model again.
Output 3:
Epoch 1/10
103/103 [==============================] - 55s 534ms/step - loss: 1.6412 - accuracy: 0.4764 - val_loss: 1.0697 - val_accuracy: 0.5738
Epoch 2/10
103/103 [==============================] - 54s 528ms/step - loss: 1.0312 - accuracy: 0.6412 - val_loss: 1.0196 - val_accuracy: 0.6077
Epoch 3/10
103/103 [==============================] - 59s 570ms/step - loss: 0.8710 - accuracy: 0.6975 - val_loss: 0.7088 - val_accuracy: 0.7240
Epoch 4/10
103/103 [==============================] - 54s 529ms/step - loss: 0.8108 - accuracy: 0.7128 - val_loss: 0.6539 - val_accuracy: 0.7458
Epoch 5/10
103/103 [==============================] - 54s 522ms/step - loss: 0.7311 - accuracy: 0.7440 - val_loss: 0.6029 - val_accuracy: 0.7676
Epoch 6/10
103/103 [==============================] - 54s 523ms/step - loss: 0.6683 - accuracy: 0.7612 - val_loss: 0.6621 - val_accuracy: 0.7506
Epoch 7/10
103/103 [==============================] - 54s 527ms/step - loss: 0.6518 - accuracy: 0.7753 - val_loss: 0.6166 - val_accuracy: 0.7700
Epoch 8/10
103/103 [==============================] - 54s 524ms/step - loss: 0.6147 - accuracy: 0.7795 - val_loss: 0.5611 - val_accuracy: 0.7797
Epoch 9/10
103/103 [==============================] - 54s 522ms/step - loss: 0.6671 - accuracy: 0.7667 - val_loss: 0.5126 - val_accuracy: 0.8087
Epoch 10/10
103/103 [==============================] - 54s 525ms/step - loss: 0.6090 - accuracy: 0.7832 - val_loss: 0.6355 - val_accuracy: 0.7627
Hope this answeres your question.

how to calculate total running time for training process of CNN Model

I need to get the total running time for my CNN training process, but I don't understand how to get this. Is there any package to get the total running time?
I have a result example of my CNN training process here. As you can see that every epoch has a running time process but I need to get the total all of the training processes.
Can anyone help me?
Epoch 1/50
250/250 [==============================] - 18s 71ms/step - loss: 4.0427 - accuracy: 0.0111 - val_loss: 3.8992 - val_accuracy: 0.0200
Epoch 2/50
250/250 [==============================] - 17s 70ms/step - loss: 3.9247 - accuracy: 0.0160 - val_loss: 3.6276 - val_accuracy: 0.0800
Epoch 3/50
250/250 [==============================] - 18s 70ms/step - loss: 3.3692 - accuracy: 0.0546 - val_loss: 2.9868 - val_accuracy: 0.1200
Epoch 4/50
250/250 [==============================] - 17s 70ms/step - loss: 2.8787 - accuracy: 0.1194 - val_loss: 2.5775 - val_accuracy: 0.2000
Epoch 5/50
250/250 [==============================] - 18s 71ms/step - loss: 2.4328 - accuracy: 0.2992 - val_loss: 2.0736 - val_accuracy: 0.3600
Epoch 6/50
250/250 [==============================] - 18s 70ms/step - loss: 1.8779 - accuracy: 0.4849 - val_loss: 1.6079 - val_accuracy: 0.4800
Epoch 7/50
250/250 [==============================] - 18s 71ms/step - loss: 1.4844 - accuracy: 0.6018 - val_loss: 1.3112 - val_accuracy: 0.6800
Epoch 8/50
250/250 [==============================] - 17s 69ms/step - loss: 1.2182 - accuracy: 0.6820 - val_loss: 1.1277 - val_accuracy: 0.6200
Epoch 9/50
250/250 [==============================] - 18s 70ms/step - loss: 1.0166 - accuracy: 0.7420 - val_loss: 0.9915 - val_accuracy: 0.7000
Epoch 10/50
250/250 [==============================] - 17s 70ms/step - loss: 0.8404 - accuracy: 0.8148 - val_loss: 0.8455 - val_accuracy: 0.7600
Epoch 11/50
250/250 [==============================] - 18s 71ms/step - loss: 0.6988 - accuracy: 0.8659 - val_loss: 0.7465 - val_accuracy: 0.7800
Epoch 12/50
250/250 [==============================] - 18s 71ms/step - loss: 0.5902 - accuracy: 0.8940 - val_loss: 0.6760 - val_accuracy: 0.7600
Epoch 13/50
250/250 [==============================] - 18s 71ms/step - loss: 0.5075 - accuracy: 0.9091 - val_loss: 0.5958 - val_accuracy: 0.8400
Epoch 14/50
250/250 [==============================] - 18s 71ms/step - loss: 0.4247 - accuracy: 0.9260 - val_loss: 0.5684 - val_accuracy: 0.8000
Epoch 15/50
250/250 [==============================] - 18s 71ms/step - loss: 0.3621 - accuracy: 0.9402 - val_loss: 0.5031 - val_accuracy: 0.8000
Epoch 16/50
250/250 [==============================] - 17s 70ms/step - loss: 0.3124 - accuracy: 0.9460 - val_loss: 0.4263 - val_accuracy: 0.9000
Epoch 17/50
250/250 [==============================] - 17s 69ms/step - loss: 0.2622 - accuracy: 0.9550 - val_loss: 0.3983 - val_accuracy: 0.8800
Epoch 18/50
250/250 [==============================] - 18s 72ms/step - loss: 0.2351 - accuracy: 0.9547 - val_loss: 0.4028 - val_accuracy: 0.8200
Epoch 19/50
250/250 [==============================] - 18s 72ms/step - loss: 0.2002 - accuracy: 0.9666 - val_loss: 0.3812 - val_accuracy: 0.8600
Epoch 20/50
250/250 [==============================] - 18s 71ms/step - loss: 0.1770 - accuracy: 0.9693 - val_loss: 0.3330 - val_accuracy: 0.9000
Epoch 21/50
250/250 [==============================] - 18s 71ms/step - loss: 0.1538 - accuracy: 0.9762 - val_loss: 0.4010 - val_accuracy: 0.8600
Epoch 22/50
250/250 [==============================] - 18s 71ms/step - loss: 0.1446 - accuracy: 0.9736 - val_loss: 0.3387 - val_accuracy: 0.8600
Epoch 23/50
250/250 [==============================] - 18s 70ms/step - loss: 0.1171 - accuracy: 0.9850 - val_loss: 0.2887 - val_accuracy: 0.9400
Epoch 24/50
250/250 [==============================] - 18s 72ms/step - loss: 0.1301 - accuracy: 0.9712 - val_loss: 0.2748 - val_accuracy: 0.9400
Epoch 25/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0966 - accuracy: 0.9846 - val_loss: 0.2883 - val_accuracy: 0.9200
Epoch 26/50
250/250 [==============================] - 18s 72ms/step - loss: 0.0936 - accuracy: 0.9853 - val_loss: 0.2930 - val_accuracy: 0.8800
Epoch 27/50
250/250 [==============================] - 18s 70ms/step - loss: 0.1104 - accuracy: 0.9761 - val_loss: 0.2626 - val_accuracy: 0.9000
Epoch 28/50
250/250 [==============================] - 18s 71ms/step - loss: 0.1067 - accuracy: 0.9762 - val_loss: 0.3085 - val_accuracy: 0.8800
Epoch 29/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0903 - accuracy: 0.9808 - val_loss: 0.3507 - val_accuracy: 0.8600
Epoch 30/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0792 - accuracy: 0.9886 - val_loss: 0.2796 - val_accuracy: 0.9000
Epoch 31/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0554 - accuracy: 0.9940 - val_loss: 0.2505 - val_accuracy: 0.9000
Epoch 32/50
250/250 [==============================] - 18s 72ms/step - loss: 0.0734 - accuracy: 0.9861 - val_loss: 0.2937 - val_accuracy: 0.9000
Epoch 33/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0446 - accuracy: 0.9949 - val_loss: 0.2639 - val_accuracy: 0.9000
Epoch 34/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0685 - accuracy: 0.9866 - val_loss: 0.3331 - val_accuracy: 0.9000
Epoch 35/50
250/250 [==============================] - 18s 71ms/step - loss: 0.1402 - accuracy: 0.9643 - val_loss: 0.2561 - val_accuracy: 0.9400
Epoch 36/50
250/250 [==============================] - 18s 72ms/step - loss: 0.0424 - accuracy: 0.9962 - val_loss: 0.3020 - val_accuracy: 0.8600
Epoch 37/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0356 - accuracy: 0.9969 - val_loss: 0.2670 - val_accuracy: 0.9200
Epoch 38/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0380 - accuracy: 0.9958 - val_loss: 0.3640 - val_accuracy: 0.8400
Epoch 39/50
250/250 [==============================] - 18s 70ms/step - loss: 0.0960 - accuracy: 0.9793 - val_loss: 0.3186 - val_accuracy: 0.8800
Epoch 40/50
250/250 [==============================] - 18s 72ms/step - loss: 0.0394 - accuracy: 0.9945 - val_loss: 0.2686 - val_accuracy: 0.9000
Epoch 41/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0364 - accuracy: 0.9955 - val_loss: 0.3073 - val_accuracy: 0.9000
Epoch 42/50
250/250 [==============================] - 18s 70ms/step - loss: 0.1673 - accuracy: 0.9567 - val_loss: 0.3678 - val_accuracy: 0.9000
Epoch 43/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0366 - accuracy: 0.9992 - val_loss: 0.3368 - val_accuracy: 0.8800
Epoch 44/50
250/250 [==============================] - 18s 72ms/step - loss: 0.0318 - accuracy: 0.9992 - val_loss: 0.3261 - val_accuracy: 0.8800
Epoch 45/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0338 - accuracy: 0.9969 - val_loss: 0.3418 - val_accuracy: 0.9000
Epoch 46/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0265 - accuracy: 0.9987 - val_loss: 0.3504 - val_accuracy: 0.8600
Epoch 47/50
250/250 [==============================] - 18s 71ms/step - loss: 0.0286 - accuracy: 0.9969 - val_loss: 0.3072 - val_accuracy: 0.9200
Epoch 48/50
250/250 [==============================] - 18s 72ms/step - loss: 0.1499 - accuracy: 0.9646 - val_loss: 0.2840 - val_accuracy: 0.9200
Epoch 49/50
250/250 [==============================] - 17s 70ms/step - loss: 0.0348 - accuracy: 0.9992 - val_loss: 0.2803 - val_accuracy: 0.9000
Epoch 50/50
250/250 [==============================] - 17s 70ms/step - loss: 0.0275 - accuracy: 0.9986 - val_loss: 0.3138 - val_accuracy: 0.9200
You may try this,
import time
start = time.time()
model.fit() # Training statement
print("Total time: ", time.time() - start, "seconds")
You need to add a Callback method, sample code:
class timecallback(tf.keras.callbacks.Callback):
def __init__(self):
self.times = []
# use this value as reference to calculate cumulative time taken
self.timetaken = time.process_time()
def on_epoch_end(self,epoch,logs = {}):
self.times.append((epoch, time.process_time() - self.timetaken))
def on_train_end(self,logs = {}):
plt.xlabel('Epoch')
plt.ylabel('Total time taken until an epoch in seconds')
plt.plot(*zip(*self.times))
plt.show()
from operator import itemgetter
previous_time = 0
for item in self.times:
print("Epoch ", item[0], " run time is: ", item[1]-previous_time)
previous_time = item[1]
print("Total trained time is: ", previous_time)
timetaken = timecallback()
callbacks = [
# This callback saves a SavedModel every epoch
# We include the current epoch in the folder name.
keras.callbacks.ModelCheckpoint(
filepath=os.path.join(os.getcwd(), "ckpt-{epoch}"), save_freq="epoch"
),
timetaken
]
model.fit(train_dataset, epochs=epochs, callbacks=callbacks, validation_data=val_dataset)