7

I am trying some examples for Universal sentence encoder the code below:

sentences_list = [
# phone related
'My phone is slow',
'My phone is not good',
'I need to change my phone. It does not work well',
'How is your phone?',

# age related
'What is your age?',
'How old are you?',
'I am 10 years old',

# weather related
'It is raining today',
'Would it be sunny tomorrow?',
'The summers are here.'

]

with tf.Session() as session:

    session.run([tf.global_variables_initializer(), 
    tf.tables_initializer()])
    sentences_embeddings = session.run(embed.signatures['default'] (sentences_list))

But get the error:

ValueError: All inputs to ConcreteFunctions must be Tensors; on invocation of pruned, the 0-th input (['My phone is slow', 'My phone is not good', 'I need to change my phone. It does not work well', 'How is your phone?', 'What is your age?', 'How old are you?', 'I am 10 years old', 'It is raining today', 'Would it be sunny tomorrow?', 'The summers are here.']) was not a Tensor.

std
  • 71
  • 1
  • 5

2 Answers2

4

Since tensorflow works with Tensors only, so it will not accept a python list as input and as the error also says, you need to convert the list to a Tensor and then feed it.

What you can do is define the list as numpy array with something like

np_list = np.asarray(sentence_list) and then convert it to tensor using

tensor_list = tf.convert_to_tensor(np_list).

Read more about them here, np.asarray and convert_to_tensor

Rishabh Sahrawat
  • 2,437
  • 1
  • 15
  • 32
  • Thanks! I am getting another error now but I have initialized the variables: FailedPreconditionError: [_Derived_] Attempting to use uninitialized value Encoder_en/DNN/ResidualHidden_0/weights [[{{node Encoder_en/DNN/ResidualHidden_0/weights/read}}]] [[StatefulPartitionedCall_17]] – std Oct 17 '19 at 11:01
  • Can you share where exactly you are seeing this error in the code? Because this error is not from the input but somewhere inside the Network code. – Rishabh Sahrawat Oct 17 '19 at 11:19
  • the error points to this line ---> sentences_embeddings = session.run(embed.signatures['default'](tensor_list)) – std Oct 17 '19 at 11:22
  • I can not say exactly the problem in this but maybe you can relate to the issue [here](https://stackoverflow.com/questions/34001922/failedpreconditionerror-attempting-to-use-uninitialized-in-tensorflow). – Rishabh Sahrawat Oct 17 '19 at 11:45
1

It says that you are passing a variable that is not a tensot, obviously. What you are missing is sentences_list should be passed through tf.constant or tf.placeholder depends on how you want to use it.

For tf.constant use: x = tf.constant(sentences_list)

And pass the x to the embed.signatures['default']

Ilya
  • 730
  • 4
  • 16