Anshuman Verma
Anshuman Verma

Reputation: 124

Using tf.hub.KerasLayer in TF 2.12 Functional API throws ValueError: Python inputs incompatible with input_signature:

I was following a youtube tutorial for text classification using tensorflow This is my code:-

hub_handle = "https://tfhub.dev/google/nnlm-en-dim50/2"
hub_layer = hub.KerasLayer(hub_handle, input_shape=[], dtype=tf.string)
hub_layer(list(df_tf)[0][0])

I get the following error:-

ValueError: Exception encountered when calling layer 'keras_layer_8' (type KerasLayer). Python inputs incompatible with input_signature: inputs: ( tf.Tensor( [[b'I need assistance with setting up a trailing stop order'] [b'What is the procedure for updating my account preferences?'], shape=(32, 1), dtype=string))

input_signature: ( TensorSpec(shape=(None,), dtype=tf.string, name=None)). Call arguments received by layer 'keras_layer_8' (type KerasLayer): • inputs=tf.Tensor(shape=(32, 1), dtype=string) • training=None

I am new to tensorflow, any help or a resource that would point me in the right direction will be highly appreciated !

Upvotes: 0

Views: 125

Answers (1)

TF_Chinmay
TF_Chinmay

Reputation: 151

The first two lines look good.

However in the third line, the input to the module should be a batch of sentences in a 1-D tensor of strings, as per the TensorFlow Hub nnlm-en-dim50 documentation.

Please refer to this code and your issue would be resolved :

import numpy as np
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds

Download the IMDB Dataset and perform the train-test split.

train_data, validation_data, test_data = tfds.load(
    name="imdb_reviews", 
    split=('train[:60%]', 'train[60%:]', 'test'),
    as_supervised=True)

Explore the data

train_examples_batch, train_labels_batch = next(iter(train_data.batch(10)))

Check the train examples

train_examples_batch

Check the train labels

train_labels_batch

Create a Keras layer that uses a TensorFlow Hub model to embed the sentences,

embedding = "https://tfhub.dev/google/nnlm-en-dim50/2"
hub_layer = hub.KerasLayer(embedding, input_shape=[], 
                           dtype=tf.string, trainable=True)
hub_layer(train_examples_batch[:3])

You can find the complete code as per the gist.

Upvotes: 0

Related Questions