I am trying to create a new module instance under a reused variable scope. A minimalistic example looks something like this,
````python
import tensorflow as tf
import tensorflow_hub as hub
with tf.variable_scope('abc'):
elmo_train = hub.Module("https://tfhub.dev/google/elmo/1", trainable=True)
with tf.variable_scope('abc', reuse=True):
elmo_test = hub.Module("https://tfhub.dev/google/elmo/1", trainable=True)
```
This gives me [this](https://github.com/tensorflow/hub/blob/master/tensorflow_hub/module.py#L319-L321)RunTimeError`.
Is this the correct way to re-use TF Hub modules? What's a good work-around?
hub.Module instances can be reused many times, but they are similar to how one would share a low-level tf object (e.g. a tf.Variable) and do not support reuse like tf.get_variable. E.g.:
# Instantiate once per graph.
elmo = hub.Module("https://tfhub.dev/google/elmo/1"", trainable=True)
# Use multiple times in the same graph.
with tf.variable_scope('abc'):
embeddings = elmo(train_inputs)
with tf.variable_scope('abc'):
embeddings = elmo(test_inputs)
Does that works for your use case?
Yes that's the kind of workaround I've done. So I assume this is intentional and not a bug?
Yes, this is intentional for now.
Getting the module state to be shared across instances may not to be trivial so we opted it out until we figure out the details. E.g. if a module has a table (as the text modules do) those are shared across applications, however tensorflow does not provides any sort of "get_variable" that would also work on tables or other generic resources beyond variables.
Most helpful comment
hub.Moduleinstances can be reused many times, but they are similar to how one would share a low-level tf object (e.g. atf.Variable) and do not support reuse liketf.get_variable. E.g.:Does that works for your use case?