繁体   English   中英

InvalidArgumentError:输入必须是向量,得到形状:[]

[英]InvalidArgumentError: input must be a vector, got shape: []

我正在尝试使用universal sentence encoder在 Pandas 数据帧新列中保存文本数据的嵌入,但出现错误。

这就是我想要做的。

module_url = "https://tfhub.dev/google/universal-sentence-encoder/4" #@param ["https://tfhub.dev/google/universal-sentence-encoder/4", "https://tfhub.dev/google/universal-sentence-encoder-large/5"]
model = thub.load(module_url)
print ("module %s loaded" % module_url)
def embed(input):
    return model(input)

然后

for t in list(df['title'].str.strip().iteritems()):
     df['new'] = np.array(embed(t[1]))

这是最终转换 df['title'] 列值,这些列值是这里的文本,并将其嵌入到字典中。 { 'how are you?' : embedding } 但两者都做不到。

得到标题中的错误。

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-26-79969d6e031c> in <module>
      1 for t in list(df['title'].str.strip().iteritems()):
----> 2      df['new'] = np.array(embed(t[1]))
      3 

<ipython-input-7-c4fca4bebab0> in embed(input)
      3 print ("module %s loaded" % module_url)
      4 def embed(input):
----> 5     return model(input)

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\saved_model\load.py in _call_attribute(instance, *args, **kwargs)
    436 
    437 def _call_attribute(instance, *args, **kwargs):
--> 438   return instance.__call__(*args, **kwargs)
    439 
    440 

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\def_function.py in __call__(self, *args, **kwds)
    566         xla_context.Exit()
    567     else:
--> 568       result = self._call(*args, **kwds)
    569 
    570     if tracing_count == self._get_tracing_count():

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\def_function.py in _call(self, *args, **kwds)
    604       # In this case we have not created variables on the first call. So we can
    605       # run the first trace but we should fail if variables are created.
--> 606       results = self._stateful_fn(*args, **kwds)
    607       if self._created_variables:
    608         raise ValueError("Creating variables on a non-first call to a function"

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\function.py in __call__(self, *args, **kwargs)
   2361     with self._lock:
   2362       graph_function, args, kwargs = self._maybe_define_function(args, kwargs)
-> 2363     return graph_function._filtered_call(args, kwargs)  # pylint: disable=protected-access
   2364 
   2365   @property

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\function.py in _filtered_call(self, args, kwargs)
   1609          if isinstance(t, (ops.Tensor,
   1610                            resource_variable_ops.BaseResourceVariable))),
-> 1611         self.captured_inputs)
   1612 
   1613   def _call_flat(self, args, captured_inputs, cancellation_manager=None):

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\function.py in _call_flat(self, args, captured_inputs, cancellation_manager)
   1690       # No tape is watching; skip to running the function.
   1691       return self._build_call_outputs(self._inference_function.call(
-> 1692           ctx, args, cancellation_manager=cancellation_manager))
   1693     forward_backward = self._select_forward_and_backward_functions(
   1694         args,

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\function.py in call(self, ctx, args, cancellation_manager)
    543               inputs=args,
    544               attrs=("executor_type", executor_type, "config_proto", config),
--> 545               ctx=ctx)
    546         else:
    547           outputs = execute.execute_with_cancellation(

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\tensorflow_core\python\eager\execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
     65     else:
     66       message = e.message
---> 67     six.raise_from(core._status_to_exception(e.code, message), None)
     68   except TypeError as e:
     69     keras_symbolic_tensors = [

c:\users\sujee\desktop\environments\projectnlp\lib\site-packages\six.py in raise_from(value, from_value)

InvalidArgumentError:  input must be a vector, got shape: []
     [[{{node StatefulPartitionedCall/StatefulPartitionedCall/text_preprocessor/tokenize/StringSplit/StringSplit}}]] [Op:__inference_restored_function_body_5286]

Function call stack:
restored_function_body

tensorflow的新手,所以不知道如何解决这个问题。

这里有一些 numpy 数组值( embeddings ),它们是在打印print(np.array(embed(t[1])))

https://paste.pythondiscord.com/pigaqumuha.py

使用universal sentence encoderpandas dataframe New列中保存文本数据embeddings的代码以及输出如下所示:

import tensorflow_hub as hub
import tensorflow as tf

embed = hub.load("https://tfhub.dev/google/universal-sentence-encoder-large/5")
embeddings = embed([ "The quick brown fox jumps over the lazy dog.", "I am a sentence for which I would like to get its embedding"])
print(embeddings)

import pandas as pd

data = [ ["The quick brown fox jumps over the lazy dog."], ["I am a sentence for which I would like to get its embedding"]]

df = pd.DataFrame(data, columns = ['Title'])
print(df)

df['New'] = list(tf.keras.backend.eval(embeddings))
print(df)

输出如下所示:

tf.Tensor(
[[ 0.01305107  0.02235125 -0.03263275 ... -0.00565093 -0.0479303
  -0.11492757]
 [ 0.05833393 -0.0818501   0.06890941 ... -0.00923877 -0.08695354
  -0.01415738]], shape=(2, 512), dtype=float32)

在此处输入图片说明

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM