data2vecでtextをベクトルに変換したい(試行錯誤)
data2vec、どうやってloadするのかでloadする方法はわかった
Data2VecTextModel.from_pretrained
code:python
$ python -i models/data2vec_text.py
>> model = Data2VecTextModel.from_pretrained(".", "nlp_base.pt")
>> type(model)
<class 'fairseq.hub_utils.GeneratorHubInterface'>
__call__は実装されていない
code:python
>> model("Hello world!")
NotImplementedError
strを渡してencodeを呼ぶと未知語扱い
code:python
>> model.encode("Hello world!")
tensor(3, 3, 2)
>> model.decode(torch.tensor(3, 3, 2))
'<unk> <unk>'
READMEに、robertaのREADME参照と書かれているので、robertaを参考にしてみる
robertaのモデル定義に「adapt data2vec models」とコメントあり
https://github.com/pytorch/fairseq/commit/c71870f370455e6154c730e8822ea323b5f266f6#diff-76d8cc14455c33d0bd6e5111a9cdf3158506d11d766a417c13066c3401f5421d
RobertaModel.from_pretrained
dict.txtが必要(コマンドはdata2vec、どうやってloadするのか参照)
pip install requests
code: python
$ python
>> from fairseq.models.roberta import RobertaModel
>> model = RobertaModel.from_pretrained(".", "nlp_base.pt")
>> model.eval()
__call__は実装されていない
code:python
>> model("Hello world!")
NotImplementedError
strを渡してencodeを呼ぶとLongTensorが返る
code:python
>> model.encode("Hello world!")
tensor( 0, 31414, 232, 328, 2)
>> model.encode("Hello world") # !は328とわかる
tensor( 0, 31414, 232, 2)
>> import torch
>> model.decode(torch.tensor(0, 31414, 232, 328, 2))
'Hello world!'
roberta/README.mdでextract_featuresメソッドを見つけた 👉 data2vecでtextをベクトルに変換