LSTM model weights to train data for text classification - keras

I built a LSTM model for text classification using Keras. Now I have new data to be trained. instead of appending to the original data and retrain the model, I thought of training the data using the model weights. i.e. making the weights to get trained with the new data.
However, irrespective of the volume i train, the model is not predicting the correct classification (even if i give the same sentence for prediction). What could be the reason?
Kindly help me.

Are you using the following to save the trained model?
model.save('model.h5')
model.save_weights('model_weights.h5')
And the following to load it?
from keras.models import load_model
model = load_model('model.h5') # Load the architecture
model = model.load_weights('model_weights.h5') # Set the weights
# train on new data
model.compile...
model.fit...
The model loaded is the exact same as the model being saved here. If you are doing this, then there must be something different in the data (in comparison with what it is trained on).

Related

How to train a pretrained model.h5 model with new data?

I have a pre-trained model file "model.h5". I am trying to train the model again(fine-tuning) using small dataset. How to do that in Keras?
Just taking,
model = load_model('Model.h5)
model = load_wights('Model.h5) are both giving bad results.

How to get the model architecture from .h5 and .json file?

I have a pre-trained Keras model with ".h5" and ".json" file. I want to know the architecture of the model used and the names of the layers. Is there anyway I can do that?
I am new to this so I don't really know where to start
I am expecting logs that you get when you load a tensorflow model.
Sure, first load the model and then produce a summary of the model:
from keras.models import load_model
model = load_model('your_model.hdf5')
model.summary()
The summary will contain layer names and input/output shapes.

Is there a way to create and train a model without transfer learning using tensorflow object-detection api?

I'm using faster_rcnn_resnet50 to train a model which will detect corrosions in images and I want to train a model from scratch instead of using transfer learning.
I don't know if this right but the reason I want to do this is that the already existing weights (which are trained on COCO) will affect my model trained on corrosion images.
One way I would like to do this is randomize or unfreeze the weights of the feature extractor on the resnet50 and then train the model on my images.
but there's no function or an option in the resnet50 config file to randomize or unfreeze weights.
I've made a new labelmap with a single label and tried it with transfer learning. It's working but I would like to have a model is trained just on my images and the previous weights shouldn't affect my predictions.
This is the first time I'm working with object detection and transfer learning. Will the weights of the pre-trained model on COCO affect my model which is trained on custom images of corrosion? How do you use tensorflow object-detection API without transfer learning?

How to load weights from file and use them to predict test data in Keras

Yesterday night I let a neural network model training and that took time, so I thought to add a statement to save weights model.save_weights('first_try.h5')
Now as I had the file, I want to benefit saved file.
Prediction is like
pred=model.predict_generator(test_generator, steps=4124, verbose=1)
If you saved your model's weights you can load using load_weights method. But first you have to define your model structure.
e.g.
model = method_to_create_the_model()
model.load_weights("path_to_weight_file")

Keras Embedding Layer

I am using Keras newsgroup example code for text classification. I have saved the trained model using the h5py library. Will the embedding layer also get saved or should I write some extra code when loading the model to use the embedding layer?
Embedding layer is part of the model so it will be saved with the model. Check out this on saving the model.
Also one important addition, the Keras Embedding layer will be initialized with the random values at the very start of the training process, and the parameters will be learned in the training phase.

Resources