Typeerror: 'Autotrackable' Object is not callable

Asked

Viewed 627 times

0

I am trying to use an imported tensorflow_hub model as shown below:

    #%%
    from sklearn.model_selection import train_test_split
    import tensorflow as tf
    import tensorflow_hub as hub
    from keras import backend
    X_tr, X_te, y_tr, y_te = train_test_split(new_X, y, test_size=0.3, random_state=2020)
    sess = tf.keras.Sequential()
    module_url = "https://tfhub.dev/google/elmo/3"
    elmo_model = hub.load(module_url)

    #%%
    batch_size = 32
    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"]

    #%%
    from keras.models import Model, Input
    from keras.layers.merge import add
    from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Lambda
    input_text = Input(shape=(max_len,), dtype=tf.string)
    embedding = Lambda(ElmoEmbedding, output_shape=(max_len, 1024))(input_text)
    x = Bidirectional(LSTM(units=512, return_sequences=True,
                           recurrent_dropout=0.2, dropout=0.2))(embedding)
    x_rnn = Bidirectional(LSTM(units=512, return_sequences=True,
                               recurrent_dropout=0.2, dropout=0.2))(x)
    x = add([x, x_rnn])  # residual connection to the first biLSTM
    out = TimeDistributed(Dense(n_tags, activation="softmax"))(x)
    model = Model(input_text, out)
    model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])

But I’m getting the following error:

TypeError                                 Traceback (most recent call last)
c:\Users\rodrigo\OneDrive\Documentos\VS code\DeepNER\DeepNER.py in 
      3 from keras.layers import LSTM, Embedding, Dense, TimeDistributed, Dropout, Bidirectional, Lambda
      4 input_text = Input(shape=(max_len,), dtype=tf.string)
----> 5 embedding = Lambda(ElmoEmbedding, output_shape=(max_len, 1024))(input_text)
      6 x = Bidirectional(LSTM(units=512, return_sequences=True,
      7                        recurrent_dropout=0.2, dropout=0.2))(embedding)

C:\ProgramData\Anaconda3\lib\site-packages\keras\backend\tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
     73         if _SYMBOLIC_SCOPE.value:
     74             with get_graph().as_default():
---> 75                 return func(*args, **kwargs)
     76         else:
     77             return func(*args, **kwargs)

C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\base_layer.py in __call__(self, inputs, **kwargs)
    487             # Actually call the layer,
    488             # collecting output(s), mask(s), and shape(s).
--> 489             output = self.call(inputs, **kwargs)
    490             output_mask = self.compute_mask(inputs, previous_mask)
    491 

C:\ProgramData\Anaconda3\lib\site-packages\keras\layers\core.py in call(self, inputs, mask)
    714         else:
    715             self._input_dtypes = K.dtype(inputs)
--> 716         return self.function(inputs, **arguments)
    717 
    718     def compute_mask(self, inputs, mask=None):

c:\Users\nataly\OneDrive\Documentos\VS code\DeepNER\DeepNER.py in ElmoEmbedding(x)
      4                               "sequence_len": tf.constant(batch_size*[max_len])},
      5                               signature="tokens",
----> 6                               as_dict=True)["elmo"]

TypeError: 'AutoTrackable' object is not callable

NOTE: This error appeared when I was trying to modify the algorithm for tensorflow summer 2.0.

1 answer

-1

Try to use the .signatures at the end to use TF1 models

detector = hub.load(module_handle).signatures['default']

Browser other questions tagged

You are not signed in. Login or sign up in order to post.