title: Variable_scope runtime error when creating keras custom layer using tensorflow hub models and tensorflow 2.0 as backend
body: <p>I'm trying to use the pretrained <a href="https://tfhub.dev/google/elmo/2" rel="nofollow noreferrer">tf-hub elmo model</a> by integrating it into a keras layer.</p>
  
  <p>Keras Layer:</p>
  
  <pre><code>class ElmoEmbeddingLayer(tf.keras.layers.Layer):
  
      def __init__(self, **kwargs):
          super(ElmoEmbeddingLayer, self).__init__(**kwargs)
          self.dimensions = 1024
          self.trainable = True
          self.elmo = None
  
      def build(self, input_shape):
          url = 'https://tfhub.dev/google/elmo/2'
  
          self.elmo = hub.Module(url)
          self._trainable_weights += trainable_variables(
              scope="^{}_module/.*".format(self.name))
          super(ElmoEmbeddingLayer, self).build(input_shape)
  
      def call(self, x, mask=None):
          result = self.elmo(
              x,
              signature="default",
              as_dict=True)["elmo"]
          return result
  
      def compute_output_shape(self, input_shape):
          return input_shape[0], self.dimensions
  </code></pre>
  
  <p>When I run the code I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "D:/Google Drive/Licenta/Gemini/Emotion Analysis/nn/trainer/model.py", line 170, in &lt;module&gt;
      validation_steps=validation_dataset.size())
    File "D:/Google Drive/Licenta/Gemini/Emotion Analysis/nn/trainer/model.py", line 79, in train_gpu
      model = build_model(self.config, self.embeddings, self.sequence_len, self.out_classes, summary=True)
    File "D:\Google Drive\Licenta\Gemini\Emotion Analysis\nn\architectures\models.py", line 8, in build_model
      return my_model(embeddings, config, sequence_length, out_classes, summary)
    File "D:\Google Drive\Licenta\Gemini\Emotion Analysis\nn\architectures\models.py", line 66, in my_model
      inputs, embedding = resolve_inputs(embeddings, sequence_length, model_config, input_type)
    File "D:\Google Drive\Licenta\Gemini\Emotion Analysis\nn\architectures\models.py", line 19, in resolve_inputs
      return elmo_input(model_conf)
    File "D:\Google Drive\Licenta\Gemini\Emotion Analysis\nn\architectures\models.py", line 58, in elmo_input
      embedding = ElmoEmbeddingLayer()(input_text)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 616, in __call__
      self._maybe_build(inputs)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1966, in _maybe_build
      self.build(input_shapes)
    File "D:\Google Drive\Licenta\Gemini\Emotion Analysis\nn\architectures\custom_layers.py", line 21, in build
      self.elmo = hub.Module(url)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow_hub\module.py", line 156, in __init__
      abs_state_scope = _try_get_state_scope(name, mark_name_scope_used=False)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow_hub\module.py", line 389, in _try_get_state_scope
      "name_scope was already taken." % abs_state_scope)
  RuntimeError: variable_scope module/ was unused but the corresponding name_scope was already taken.
  </code></pre>
  
  <p>It seems to be due to the eager execution behaviour. If I disable eager execution I have to surround the model.fit function within a tensorflow session and initialize the variables by using <code>sess.run(global_variables_initializer())</code> to avoid the next error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "D:/Google Drive/Licenta/Gemini/Emotion Analysis/nn/trainer/model.py", line 168, in &lt;module&gt;
      validation_steps=validation_dataset.size().eval(session=Session()))
    File "D:/Google Drive/Licenta/Gemini/Emotion Analysis/nn/trainer/model.py", line 90, in train_gpu
      class_weight=weighted)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\keras\engine\training.py", line 643, in fit
      use_multiprocessing=use_multiprocessing)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 664, in fit
      steps_name='steps_per_epoch')
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\keras\engine\training_arrays.py", line 294, in model_iteration
      batch_outs = f(actual_inputs)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\keras\backend.py", line 3353, in __call__
      run_metadata=self.run_metadata)
    File "D:\Apps\Anaconda\envs\tf2.0\lib\site-packages\tensorflow\python\client\session.py", line 1458, in __call__
      run_metadata_ptr)
  tensorflow.python.framework.errors_impl.FailedPreconditionError: 2 root error(s) found.
    (0) Failed precondition: Error while reading resource variable module/bilm/RNN_0/RNN/MultiRNNCell/Cell1/rnn/lstm_cell/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/module/bilm/RNN_0/RNN/MultiRNNCell/Cell1/rnn/lstm_cell/bias/class tensorflow::Var does not exist.
       [[{{node elmo_embedding_layer/module_apply_default/bilm/RNN_0/RNN/MultiRNNCell/Cell1/rnn/lstm_cell/bias/Read/ReadVariableOp}}]]
    (1) Failed precondition: Error while reading resource variable module/bilm/RNN_0/RNN/MultiRNNCell/Cell1/rnn/lstm_cell/bias from Container: localhost. This could mean that the variable was uninitialized. Not found: Resource localhost/module/bilm/RNN_0/RNN/MultiRNNCell/Cell1/rnn/lstm_cell/bias/class tensorflow::Var does not exist.
       [[{{node elmo_embedding_layer/module_apply_default/bilm/RNN_0/RNN/MultiRNNCell/Cell1/rnn/lstm_cell/bias/Read/ReadVariableOp}}]]
       [[metrics/f1_micro/Identity/_223]]
  0 successful operations.
  0 derived errors ignored.
  </code></pre>
  
  <p>My solution:</p>
  
  <pre><code>with Session() as sess:
      sess.run(global_variables_initializer())
      history = model.fit(self.train_data.repeat(),
                          epochs=self.config['epochs'],
                          validation_data=self.validation_data.repeat(),
                          steps_per_epoch=steps_per_epoch,
                          validation_steps=validation_steps,
                          callbacks=self.__callbacks(monitor_metric),
                          class_weight=weighted)
  </code></pre>
  
  <p>The main question is if there is another way to use elmo tf-hub module in a keras custom layer and train my model. Another question is if my current solution is not affecting the training performances or give the OOM GPU error (I get the OOM error after a few epochs with a higher batch size, which I've found to be related to sessions not closed or memory leaks). </p>