拥抱的脸从pytorch变为numpy

时间:2020-10-03 04:10:57

标签: python numpy huggingface-transformers

我有以下代码:

  text = "Some string about 5000 characters long"

  tokenizer = TransfoXLTokenizerFast.from_pretrained('transfo-xl-wt103', cache_dir=my_local_dir, local_files_only=True)
  model = TransfoXLModel.from_pretrained("transfo-xl-wt103", cache_dir=my_local_dir, local_files_only=True)

  encoded_input = tokenizer(text, return_tensors='pt') 
  output = model(**encoded_input)

当我将return_tensors='pt'更改为return_tensors='np'时,我期望向量的返回类型将从pytorch更改为numpy。

我从this documentation理解到这应该是可行的,并且虽然input_id被here覆盖了,但我希望它能够返回。但是我得到了:

    output = model(**encoded_input)
  File "/home/user/w/default/lib/python3.7/site-packages/torch/nn/modules/module.py", line 550, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/user/w/default/lib/python3.7/site-packages/transformers/modeling_transfo_xl.py", line 795, in forward
    input_ids = input_ids.transpose(0, 1).contiguous()
AttributeError: 'numpy.ndarray' object has no attribute 'contiguous'

发生了什么事?当我要返回numpy向量与pytorch向量时,应该如何准备**encoded_input

0 个答案:

没有答案