Comments (6)
@mohammedayub44 ah, ok. In that case, you can export the model as described in #107 and reload it in Tensorflow 2 within your Streamlit app. Here's sample code (caveat: I haven't run this in a Streamlit app. But I have confirmed it works in Tensorflow 2):
import tensorflow as tf
from bilm import Batcher
# reload the model
loaded = tf.saved_model.load("/path/to/saved/model") # this is a directory. Don't include the file itself in the path.
infer = loaded.signatures["serving_default"]
# get the char ids for your documents
vocab_file = '/path/to/my_vocab.txt'
batcher = Batcher(vocab_file, 50)
char_ids = batcher.batch_sentences([["Hello", "world"]])
char_ids = char_ids.astype('int32') # must be cast to int32 before feeding to model
# get embeddings
embs = infer(tf.constant(char_ids))['import/concat_3:0']
Don't be alarmed if you see this message: INFO:tensorflow:Saver not created because there are no variables in the graph to restore.
This is expected.
Regarding the output size, you'll get a 3 x 1024 tensor for every token in your input. So long documents or large batches can both cause large outputs.
from bilm-tf.
Issue #193 contains an explanation of the outputs you can get from this implementation. Unlike the TF Hub implementation, the bilm-tf implementation can't directly give you a weighted sum of the three output layers. You can, however, weight the three output layers yourself, for example by including a keras WeightedAverage layer in a model that's consuming the ELMo embeddings. Note that the first of the three output layers from lm_embeddings
contains the character-based representations you wanted.
Here's a code snippet for getting the three output layers. Also note that per my comment on issue #107, this code requires the model saved in Step 1, not the final model with TF serving tags. When you're ready to deploy the model in TF serving, use the model saved in Step 2.
import tensorflow as tf
from bilm import Batcher, BidirectionalLanguageModel
# load the saved model
frozen_graph = '/path/to/my_saved_model.pb'
with tf.gfile.GFile(frozen_graph, "rb") as f:
restored_graph_def = tf.GraphDef()
restored_graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(
restored_graph_def,
input_map=None,
return_elements=None,
name="")
output_node = graph.get_tensor_by_name("concat_3:0")
input_node = graph.get_tensor_by_name("Placeholder:0")
# generate character ids for your input documents
vocab_file = '/path/to/my_vocab.txt'
batcher = Batcher(vocab_file, 50)
char_ids = batcher.batch_sentences([["Hello", "world"]])
# get embeddings
sess = tf.Session(graph=graph)
my_feed_dict = {input_node: char_ids}
embs = sess.run(output_node, feed_dict=my_feed_dict)
Also, one more note: this model produces very large outputs. When you deploy the model in TF serving, the embeddings have to be serialized to be returned to you. If you're then feeding them to another model, they will have to be de-serialized. The serialization/deserialization steps are time-consuming, and it would be faster to deploy the models in native tensorflow, rather than via TF serving, so that the embeddings can be passed directly to the downstream model as numpy arrays, skipping the serialization/deserialization steps.
from bilm-tf.
@carolmanderson Great. Thanks for in the detailed code snippet.
I was using Streamlit to build my prototype app. All my other word embedding models are using Tensorflow 2 and are natively loaded from checkpoints. Since this repo doesn't support TF2.0. I had to go down this route of including them as REST endpoints.
Good point about output size. I'm passing independent sentences. Does that depend on batch size or no of sentences. My guess is simple python pickling should work ?
from bilm-tf.
@carolmanderson Thanks. Could you verify the lines to be commented in #107 . The link did not work unfortunately.
from bilm-tf.
Sorry about that. The lines are:
Lines 587 to 593 in 7cffee2
from bilm-tf.
No problem. It works smoothly in Tensorflow 2. Guess I will skip the serving part for now as loading natively works better for me using Streamlit.
from bilm-tf.
Related Issues (20)
- UnicodeError when run example: python -m unittest discover tests/ HOT 2
- how many time it takes if I training a 5GB corpus on 1080ti
- How to use ELMo in Keras version? HOT 1
- why should vocabulary file be sorted in descending order by token count in our training data๏ผ
- bug during data loading (and tests) HOT 2
- Training ELMO on TPU for generating embedding from custom dataset
- how to freeze some layers during training HOT 1
- Import Error: No module named bilm.training when I try to incrementally train ELMo HOT 1
- drastic impact of Changing the vocabulary on perplexity HOT 1
- incremental training on less than 2000 sentences HOT 2
- elmo sentence embeddings using usage_token.py
- The PyTorch version replication of the "Deep contextualized word representations" is missing
- Does any code about ELMO+ESIM? HOT 1
- Compute training time
- Preparing training data for a domain with many multi keyword token HOT 1
- Running into error on tests HOT 3
- Saving the custom trained ELMO embedding (hdf5) in the format of tensorhub models
- 2021-10-29 14:13:30.313623: E C:\users\nwani\_bazel_nwani\mmtm6wb6\execroot\org_tensorflow\tensorflow\stream_executor\cuda\cuda_event.cc:49] Error polling for event status: failed to query event: CUDA_ERROR_OUT_OF_MEMORY
- Pre-trained links do not work HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from bilm-tf.