Deshwal
Deshwal

Reputation: 4152

How to use ELMO Embeddings as the First Embedding Layer in tf 2.0 Keras using tf-hub?

I am trying to build a NER model in Keras using ELMO Embeddings. SO I stumped across this tutorial and started implementing. I got lots of errors and some of them are as:

import tensorflow as tf
import tensorflow_hub as hub
from keras import backend as K


sess = tf.Session()
K.set_session(sess)

elmo_model = hub.Module("https://tfhub.dev/google/elmo/2", trainable=True)
sess.run(tf.global_variables_initializer())
sess.run(tf.tables_initializer())

def ElmoEmbedding(x):
    return elmo_model(inputs={"tokens": tf.squeeze(tf.cast(x, tf.string)),
                            "sequence_len": tf.constant(batch_size*[max_len])},signature="tokens",as_dict=True)["elmo"]

input_text = Input(shape=(max_len,), dtype=tf.string)
embedding = Lambda(ElmoEmbedding, output_shape=(None, 1024))(input_text)

It gives me AttributeError: module 'tensorflow' has no attribute 'Session' . So if I comment out sess= code and run, it gives me AttributeError: module 'keras.backend' has no attribute 'set_session'.

Then again, Elmo code line is giving me RuntimeError: Exporting/importing meta graphs is not supported when eager execution is enabled. No graph exists when eager execution is enabled..

I have the following configurations:

tf.__version__
'2.3.1'

keras.__version__
'2.4.3'

import sys
sys.version
'3.8.3 (default, Jul  2 2020, 17:30:36) [MSC v.1916 64 bit (AMD64)]'

How can I use ELMO Embeddings in Keras Model?

Upvotes: 1

Views: 672

Answers (1)

RDS
RDS

Reputation: 526

You are using the old Tensorflow 1.x syntax but you have tensorflow 2 installed.

This is the new way to do elmo in TF2 Extracting ELMo features using tensorflow and convert them to numpy

Upvotes: 1

Related Questions