2

I am a student, and I am trying to implement text classification using elmo on keras. I imported elmo layer from tensorflow-hub.

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

url = "https://tfhub.dev/google/elmo/2"
embed = hub.Module(url)

Model :


inpt = Input(shape=(max_len,), dtype = tf.string)
emb_layer = Lambda(ELMoEmbedding, output_shape=(max_len,1024))(inpt)  
bdlstm1 = Bidirectional(LSTM(1024))
drp = Dropout(0.5)(bdlstm1)
dns1 = Dense(2, activation='relu')(drp)
dns2 = Dense(no_labels, activation='softmax')(dns1)
model = Model(inpt, dns2)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x, y, batch_size = 100, epochs=10)

x and y are int32 numpy arrays

x = [[0,0,1,2,3],[0,0,4,5,6]]
y = [[0,1],[1,0]]

On implementing above code I am getting following error

c_api.TF_GetCode(self.status.status))

InternalError: Unable to get element as bytes.

Updated code

import tensorflow as tf
import tensorflow_hub as hub
from tensorflow.keras.layers import Input, Bidirectional, Lambda, Dropout, Dense, LSTM
from tensorflow.keras.models import Model
def ELMoEmbedding(x):
    return embed(inputs={ "tokens": tf.squeeze(tf.cast(x, tf.string)), "sequence_len": tf.constant(100*[max_len])}, signature="tokens", as_dict=True)["elmo"]

url = "https://tfhub.dev/google/elmo/2"
embed = hub.Module(url)

inpt = Input(shape=(max_len,))
emb_layer = Lambda(ELMoEmbedding, output_shape=(max_len,1024))(inpt)  
bdlstm1 = Bidirectional(LSTM(1024))
drp = Dropout(0.5)(bdlstm1)
dns1 = Dense(2, activation='softmax')(drp)
model = Model(inpt, dns1)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(x, y, batch_size = 100, epochs=10)

New Error

TypeError: Failed to convert object of type <class 'tensorflow.python.keras.layers.wrappers.Bidirectional'> to Tensor. Contents: <tensorflow.python.keras.layers.wrappers.Bidirectional object at 0x7fc724d042d0>. Consider casting elements to a supported type.
hR 312
  • 824
  • 1
  • 9
  • 22
  • When tried to reproduce your error, I was getting different Errors. Can you share the complete code so that we can help you. Thanks! –  Feb 28 '20 at 09:49
  • Hi @TensorflowSupport I have updated the code and the new error that I am receiving. – hR 312 Feb 28 '20 at 10:29
  • This is the correction, `bdlstm1 = Bidirectional(LSTM(1024))(emb_layer)`. Also, what is the value of `max_len` –  Feb 28 '20 at 10:52
  • It's 1240, what input is required for elmo embedding layer? – hR 312 Mar 02 '20 at 10:05
  • You can go through this link, https://tfhub.dev/google/elmo/2 for more info about Elmo. –  Mar 02 '20 at 12:33
  • Hi hR312, https://tfhub.dev/google/elmo/2 is deprecated. You can try using https://tfhub.dev/google/elmo/3. –  Mar 04 '20 at 09:03

0 Answers0