Extract hyperparameters from a trained Keras model - python

For re-training a model it is useful to know the hyperparameters of the elder model. It could give us a starting point. I have a .h5 keras model where I want to extract some hyperparameters like Batch_size and number of epochs. I have tried model.summary() but doesn't give the necessary information and also model.get_config().
Is there any way to extract these hyperparameters of the Keras model?

If you want to get all hyperparameters of optimizer, please do:
model.optimizer.get_config()
If you want to get all hyperparameters of layers, please do:
model.get_config()

Related

Accuracy and Validation Accuracy of pretrained model is not changed

I have 600 000 images and I want to classify them using keras. I am just trying the pretrained model on greyscale images. And I am trying to use the model architecture of pre-trained models like resnet50, inceptionv3, etc. But accuracy and validation accuracy of the model has not changed which is stuck at 67%. I tried changing the network, applying more epochs and also changing the pretrained model, but I always get the same result like 67% accuracy and validation accuracy. I don't understand why I am getting the same result. Please recommended some ideas on how can I solve this problem. This is my code. In this steps_per_epochs = no. of images/batch size and batch size is 128.No of images in the training dataset is 479369 and in the validation dataset is 136962.This is the output of the code.
I think you are using a pre-trained model. so, that is why it gets showed the same accuracy. my suggestion is to change the pre-trained model and tryna your custom model and then see the changes.

Model accuracy is not changing from 0.5%(0.0050)

I am training a CNN model for Image Classification using Keras. I am using VGG19 model and a custom dataset with 200 classes and uniformly distributed 90000 training images, 10000 Validation Images and 10000 test images. Even though the training is at 200 epochs, the accuracy is staying at a constant 0.0050. Same with the loss, 5.2988. I am using Kaggle's TPU instance to run this model.
How can I make the model more accurate? Can you suggest any different pretrained models for this purpose?
Your CNN model is behaving like a random model.
I know this because since there are 200 classes, the probability of getting a correct class at random is 1/200=0.0050 which is the accuracy that you have.
This happens when you use tensorflow/keras API instead of sequential()
Since you are using VGG19, if you are trying to use transfer learning, then maybe you have freezed the wrong layer.
If you are using API then you have to do
model = Model(inputs = input_layer, outputs = output_layer) #which is not required in sequential()
print(model.layers) # if you are using API or sequential() this is used to check your layers
Then you have to freeze the layer required as
model.layers[index_of_freeze_layer].trainable = False
If you are not freezing your model layers, then try to use lower learning rate since VGG19 is very sensitive to learning rate. (0.00001 or less depends)

LSTM, Keras : How many layers should the inference model have?

Should the inference model in a chatbot model with keras lstm, have the same amount of layers as the main model or it doesnt matter?
I don't know what you exactly mean by inference model.
The number of layers of a model is an hyperparameter that you tune during training. Let's say that you train an LSTM model with 3 layers, then the model used for inference must have the same number of layers and use the weights resulting from the training.
Otherwise, if you add non trained layer when inference, the results won't make any sense.
Hope this helps

Finetuning the Universal Sentence Encoder

I am new to TensorFlow. I am using Universal Sentence Encoder for text similarity. I would like to finetune USE with my own corpus.
I currently have:
module_url = "https://tfhub.dev/google/universal-sentence-encoder/2"
embed = hub.Module(module_url, trainable=True)
According to here, setting trainable=True will "expose the variables as trainable". However, I have no clue what these trainable variables are and how I can use them to finetune the USE with my own corpus.
Please, any guidance or direction would be greatly appreciated.
To finetune a pre-trained model is to allow it's weights to be updated in the downstream training task.
So you have 2 options:
trainable=False
this option will train quicker but the pretrained model weights will never be updated. A sentence embedding will look identical before and after your own training. Only your own model layers will have their weights changed by training.
trainable=True
this adds a computational burden to your training loop but will allow the weights of the embedder to become updated according to your task and training data. This may result in a more accurate final model

Training accuracy graph with model_to_estimator

I have a Keras sequential model and I'm using:
model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
I can see the training accuracy printed when I use Keras fit() function to train the model.
I need to use Estimator API to train the model and I'm using model_to_estimator to convert the model to estimator. Then I use train_and_evaluate() to train the model.
However I don't see the accuracy graph in Tensorboard. There's only one accuracy value (from evaluation), so the graph is just a dot.
What I need is the accuracy graph from training like the one shown here:
https://www.tensorflow.org/guide/custom_estimators#tensorboard
I checked the examples and all I could find were ones where they use Estimator API to build the model and use following code to define a summary scalar.
# Compute evaluation metrics.
accuracy = tf.metrics.accuracy(labels=labels,
predictions=predicted_classes,
name='acc_op')
metrics = {'accuracy': accuracy}
tf.summary.scalar('accuracy', accuracy[1])
Does anyone know how to use this with models converted from Keras?
I'm using Tensorflow version r1.10.

Categories