3
votes

I am using keras model.predict to predict sentiments. I am using universal sentence embeddings. While predicting, I am getting the error described below. Please provide your valuable insights. Regards.

I have run the code for two sets of inputs. For say, input1, the prediction is obtained. While its not working for input 2.

Input 1 is the form : {(a1,[sents1]),....}
Input 2:{((a1,a2),[sents11])),...}

The input for predicting is the [sents1], [sents11] etc. extracted from this.

I could see the related question in (Keras model.predict function giving input shape error). But I don't know whether its resolved. Further, input1 is working.

import tensorflow as tf
import keras.backend as K
from keras import layers
from keras.models import Model
import numpy as np

def UniversalEmbedding(x):
    return embed(tf.squeeze(tf.cast(x, tf.string)), signature="default", as_dict=True)["default"]

input_text = layers.Input(shape=(1,), dtype=tf.string)
embedding = layers.Lambda(UniversalEmbedding, output_shape=(embed_size,))(input_text)
dense = layers.Dense(256, activation='relu')(embedding)
pred = layers.Dense(category_counts, activation='softmax')(dense)
model = Model(inputs=[input_text], outputs=pred)
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
sents1=list(input2.items())

with tf.Session() as session:
      K.set_session(session)
      session.run(tf.global_variables_initializer())
      session.run(tf.tables_initializer())
#      model.load_weights(.//)
      for i,ch in enumerate(sents1):
          new_text=ch[1]
          if len(new_text)>1:
              new_text = np.array(new_text, dtype=object)[:, np.newaxis]
              predicts = model.predict(new_text, batch_size=32)

InvalidArgumentError: input must be a vector, got shape: [] [[{{node lambda_2/module_1_apply_default/tokenize/StringSplit}} = StringSplit[skip_empty=true, _device="/job:localhost/replica:0/task:0/device:CPU:0"](lambda_2/module_1_apply_default/RegexReplace_1, lambda_2/module_1_apply_default/tokenize/Const)]]

1
Could you also paste your model definition here? - keineahnung2345
The code segment is edited - user3568044
I get the error when the new_text.shape=(33,1). Can anyone please provide some insights. - user3568044
What is embed in UniversalEmbedding? - keineahnung2345
I get the same issue. new_text must have at least 2 elements. As a workaround I've added a 2nd dummy element to the array and ignore the result for it. - schwart

1 Answers

0
votes

Try removing trailing blanks at the start of the sentence. new_text.strip() USE preprocessed sentences by splitting on space, creating some empty lists from trailing spaces, which cannot be embedded. (Hope this answer is not too late)

Also could be some missing values in sentences, without text. Need to exclude these.