I've been working on a task using bert model https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2. As per the instructions I've added the line:
bert_layer = hub.KerasLayer("https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2", trainable=True)
But problem comes when it downloads the model again on every run. That's annoying. I've already spent a lot of time on searching How to download and work with tf-hub models . But i got no proper answer. That's why I'm here for guidance official tf-hub team.
I've downloaded the .tar.gz file from https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2 and extracted the file. It contains
@devspartan,
Please find the below code which demonstrates Accessing the Bert_Model from the Downloaded Path, in a For Loop as well.
import tensorflow_hub as hub
Bert_Model_URL = "https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2"
bert_layer = hub.KerasLayer(Bert_Model_URL, trainable=True)
Bert_Model_Path = '/home/mothukuru/Jupyter_Notebooks/Stack_Overflow/TF_Hub/bert_en_uncased_L-12_H-768_A-12_2'
bert_layer = hub.KerasLayer(Bert_Model_Path, trainable=True)
Bert_Model_Path = '/home/mothukuru/Jupyter_Notebooks/Stack_Overflow/TF_Hub/bert_en_uncased_L-12_H-768_A-12_2'
for _ in range(5):
bert_layer = hub.KerasLayer(Bert_Model_Path, trainable=True)
Please let me know if it helps. If it doesn't, In order to expedite the trouble-shooting process, please provide a code snippet to reproduce the issue reported here. Also, can you please explain why do you want to download the Model again on every run, rather than using the Model which is downloaded already? Thanks!
Hi @devspartan, the tensorflow_hub library attempts to cache downloaded models for reuse between different runs of your program. The location can be customized by setting the environment variable TFHUB_CACHE_DIR or the command-line flag --tfhub_cache_dir. The default location is os.path.join(tempfile.gettempdir(), "tfhub_modules"), which results in /tmp/tfhub_modules on many Linux systems.
To get more durable caching than what /tmp affords, consider adding
export TFHUB_CACHE_DIR=$HOME/.cache/tfhub_modules
to your .bashrc (or equivalent), start a new shell, and do mkdir -p $TFHUB_CACHE_DIR before the first use. There is no automated cache cleanup, though.
This works transparently, without having to change the canonical tfhub.dev/... URLs used in the code.
In response to this and other user inquiries, there is now expanded documentation at https://www.tensorflow.org/hub/caching
Thanks, @devspartan, for raising this, and thanks, @rmothukuru, for spelling out how to take complete control in case caching is not enough.