How can i share a .pt model on huggingFace hub? - pytorch

I have my model saved in a .pt file and im trying now to share it on huggingFace hub.
According to this tutorial, https://huggingface.co/transformers/v4.0.1/model_sharing.html
I need these files:
a config.json file, which saves the configuration of your model ;
a pytorch_model.bin file, which is the PyTorch checkpoint (unless you can’t have it for some reason) ;
a tf_model.h5 file, which is the TensorFlow checkpoint (unless you can’t have it for some reason) ;
a special_tokens_map.json, which is part of your tokenizer save;
a tokenizer_config.json, which is part of your tokenizer save;
files named vocab.json, vocab.txt, merges.txt, or similar, which contain the vocabulary of your tokenizer, part of your tokenizer save;
maybe a added_tokens.json, which is part of your tokenizer save.
My question is: How can i get these files from my saved .pt model?

Related

Loading weights into a keras model from a .pb filed generated by tf.simple_save() in TensorFlow 1.15.2

I am struggling with restoring a keras model from a .pb file. I have seen a couple of posts explaining how to do inference using a model saved in .pb format, but what I need is to load the weights into a keras.Model class.
I have a function that returns an instance of the said original model, but untrained. I want to restore the weights of that model from the .pb file. My goal is to then truncate the model and use the truncated model for other purposes.
So far the furthest I've gotten is using the tf.saved_model.load(session, ['serving'], export_dir) function to get a tensorflow.core.protobuf.meta_graph_pb2.MetaGraphDef object. From here I can access the graph_def attribute, and from there the nodes.
How can I go from that to getting the weights and then loading those into the instance of the untrained keras Model?
Maybe if that's not doable there is a way to "truncate" the graph_def somehow and then make inference using that?

Huggingface pre trained bert model is not working

I have pre-trained a bert model with custom corpus then got vocab file, checkpoints, model.bin, tfrecords, etc.
Then I loaded the model as below :
# Load pre-trained model (weights)
model = BertModel.from_pretrained('/content/drive/My Drive/Anirban_test_pytorch')
But when I am trying to use the model for any task (like q and a, prediction of mask word etc.) then getti9ng below error
from transformers import pipeline
nlp = pipeline("fill-mask", model="model")
nlp(f"This is the best thing I've {nlp.tokenizer.mask_token} in my life.")
ERROR:
OSError: Can't load config for 'model'. Make sure that:
'model' is a correct model identifier listed on 'https://huggingface.co/models'
or 'model' is the correct path to a directory containing a config.json file
Can you please help me?

What is the difference in saving the model as cnn.model or cnn.h5?How are these extensions different?

I am using model.save("cnn.model") and model.save("cnn.h5") to save the model after training.
What is the difference of the saving the model in 2 different extensions?
File name, which includes the extension, doesn't matter. Whatever it is, Keras will save a HDF5 formatted model into that file.
Doc: How can I save a Keras model?
You can use model.save(filepath) to save a Keras model into a single
HDF5 file which will contain:
the architecture of the model, allowing to re-create the model
the weights of the model
the training configuration (loss, optimizer)
the state of the optimizer, allowing to resume training exactly where you left off.

How to load history from saved model(.h5) file?

I have trained my model and saved it. For plotting results against that model I loaded the model and now I want to extract history from that model.
saved model
model = load_model('model_pre_ep3_valloss0.360.h5')
How do I extract accuracy, loss etc plot from there?
That information is not saved a Keras HDF5 model file, so if you did not save it in another file, it is effectively lost.
One simple way to save the loss/accuracy history is to use the CSVLogger callback, more information in the keras docs.

How to train a pretrained binary file on my own corpus using gensim?

Hey guys I have a pretrained binary file and I want to train it on my corpus.
Approach I tried :
I tried to extract the txt file from the bin file I had and use this as a word2vec file at time of loading and further trained it on my own corpus and saved the model but the model is performing badly for the words which are there in the pre-trained bin file (I used intersect_word2vec_format command for this.)
Here is the script I used.
What should be my approach for my model to perform well on words from both the pre-trained file and my corpus?
Load your model and use build_vocab with update = True.
import gensim
from gensim.models import Word2Vec
model = Word2Vec.load('w2vmodel.bin')
my_corpus = ... # load your corpus as sentences here
model.build_vocab(my_corpus, update=True)
model.train(my_corpus)
It's not really clear to me when intersect_word2vec_format is helpful, but you can read more about the intended use case here. It does seem it's not for ordinary re-training of vectors though.

Resources