Search code examples
pythontensorflowkerastext-classification

ValueError : Call arguments received: • inputs=tf.Tensor(shape=(None, 1), dtype=float32) • training=None


I get the described error with the Input layer and I can't seem to pinpoint the problem. I'm working on a text classification dataset and wanted to use the universal sentence encoder model for embeddings but it doesn't seem to work here. When I created my own embeddings using the embedding layer and the text vectorization layer it worked flawlessly.

use = hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4',trainable=False,dtype=tf.string,input_shape=[])
    class CnnModel(keras.Model):
        def __init__(self,channels):
            super(CnnModel,self).__init__()
            self.conversion = keras.Sequential([
                Input(shape=(1,)),
                use
            ])
            self.computation = keras.Sequential([
                Conv1D(filters=channels,kernel_size=2,strides=1,padding='valid'),
                MaxPool1D(pool_size=2,strides=1,padding='valid'),
                Conv1D(filters=channels,kernel_size=2,strides=1,padding='same'),
            ])
            self.dense = keras.Sequential([
                GlobalMaxPooling1D(),
                Dense(units=1,activation='sigmoid')
            ])
        def call(self,input_tensor):
            print(input_tensor.shape)
            x = self.conversion(input_tensor)
            x = self.computation(x)
            x = self.dense(x)
            return x
    model = CnnModel(16)

I can't even instantiate this class and get this error:

    ValueError                                Traceback (most recent call last)
c:\Users\gupta\OneDrive\Desktop\GIT\Repo\rough.ipynb Cell 6 in <cell line: 25>()
     23         x = self.dense(x)
     24         return x
---> 25 model = CnnModel(16)

c:\Users\gupta\OneDrive\Desktop\GIT\Repo\rough.ipynb Cell 6 in CnnModel.__init__(self, channels)
      4 def __init__(self,channels):
      5     super(CnnModel,self).__init__()
----> 6     self.conversion = keras.Sequential([
      7         Input(shape=(1,)),
      8         use
      9     ])
     10     self.computation = keras.Sequential([
     11         Conv1D(filters=channels,kernel_size=2,strides=1,padding='valid'),
     12         MaxPool1D(pool_size=2,strides=1,padding='valid'),
     13         Conv1D(filters=channels,kernel_size=2,strides=1,padding='same'),
     14     ])
     15     self.dense = keras.Sequential([
     16         GlobalMaxPooling1D(),
     17         Dense(units=1,activation='sigmoid')
     18     ])

File c:\Users\gupta\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow\python\training\tracking\base.py:629, in no_automatic_dependency_tracking.<locals>._method_wrapper(self, *args, **kwargs)
...


Call arguments received:
  • inputs=tf.Tensor(shape=(None, 1), dtype=float32)
  • training=None

I also tried making this model using Sequential API and I managed to localise the same error to this:

(this also gives the exact same error)

 ann = keras.Sequential([
        Input(shape=(1,)),
        use
    ])

Solution

  • I tried to build model for text classification and it worked for me. Providing the shape as blank and mentioning data type as string in the Input layer worked for me as we are dealing with text data.

    keras.Input(shape=[], dtype = tf.string)
    

    Example Code Snippet:

    use = hub.KerasLayer('https://tfhub.dev/google/universal-sentence-encoder/4',trainable=False,dtype=tf.string,input_shape=[])
    
    # build model: Sequential
    ann = keras.Sequential([
          keras.Input(shape=[], dtype = tf.string),
          use,
          keras.layers.Dense(1, activation = "sigmoid")
        ])
    
    # compile model
    ann.compile(Adam(2e-5), loss='binary_crossentropy', metrics=['accuracy'])
    ann.summary()
    
    #fit model
    ann.fit(train_dataset, epochs=1,
                        validation_data=test_dataset,
                        validation_steps=3)