Search code examples
pythonnlpgoogle-colaboratoryhuggingface-transformers

Downloading huggingface pre-trained models


Once I have downloaded a pre-trained model on a Colab Notebook, it disappears after I reset the notebook variables. Is there a way I can download the model to use it for a second occasion?

tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')

Solution

  • Mount your google drive:

    from google.colab import drive
    drive.mount('/content/drive')
    

    Do your stuff and save your models:

    from transformers import BertTokenizer
    
    tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
    tokenizer.save_pretrained('/content/drive/My Drive/tokenizer/')
    

    Reload it in a new session:

    tokenizer2 = BertTokenizer.from_pretrained('/content/drive/My Drive/tokenizer/')