安裝Transformers與ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length.報錯


此篇博客內容為短暫存留記錄(項目使用過程還未記錄),后續將會更新完整學習流程。。

1.根據官網上的鏈接安裝有兩種方式:

(1)pip直接安裝

pip install transformers  # 安裝時間長,出現Timeout Error的可能性大,個人不建議

(2)GIthub下載源包

git clone https://github.com/huggingface/transformers  # 下載時間長一點(網盤自取)
cd transformers
pip install .

transformers下載:

鏈接:https://pan.baidu.com/s/1Z9Pwdzwa671JNmQPzYNlUQ
提取碼:dsei

2.安裝之后需要進行測試

 python -c "from transformers import pipeline; print(pipeline('sentiment-analysis')('I hate you'))"  # 官網給的測試代碼,運行出來結果就是安裝成功了

安裝成功:

 

 報錯

一開始,測試代碼運行后出現了報錯,如下:

 報錯內容如下:

AttributeError: 'Tensor' object has no attribute 'ndim'

ValueError: Unable to create tensor, you should probably activate truncation and/or padding with 'padding=True' 'truncation=True' to have batched tensors with the same length.

明明操作都按照官網上的來,最后百思不得其解自己改了從官網下載下來的源碼,這里報錯的文件是指向了D:\Anaconda3\Lib\site-packages\transformers下的tokenization_utils_base.py文件,找到具體報錯那一行的代碼,如下:

 獲取tensor的維度信息dims,正確代碼應該是len(list(tensor.size()))。

再次執行測試代碼,成功出來結果!

transformers加載后緩存文件是:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM