Hub: Difficulty in reusing model

Created on 8 Apr 2018  路  3Comments  路  Source: tensorflow/hub

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?

Most helpful comment

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?

All 3 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

truas picture truas  路  5Comments

artemmavrin picture artemmavrin  路  3Comments

r-wheeler picture r-wheeler  路  5Comments

codevjs picture codevjs  路  4Comments

marcoaleixo picture marcoaleixo  路  4Comments