🚀 UniXcoder-baseモデルカード
UniXcoderは、マルチモーダルデータ(コードコメントやASTなど)を活用してコード表現を事前学習する、統一されたクロスモーダル事前学習モデルです。このモデルは、コード関連の様々なタスクに利用できます。
🚀 クイックスタート
依存関係のインストール
pip install torch
pip install transformers
クラスのダウンロード
wget https://raw.githubusercontent.com/microsoft/CodeBERT/master/UniXcoder/unixcoder.py
モデルの初期化
import torch
from unixcoder import UniXcoder
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = UniXcoder("microsoft/unixcoder-base")
model.to(device)
✨ 主な機能
- 多様なコード関連タスクに対応:コード検索、コード補完、関数名予測、API推薦、コード要約など
- マルチモーダルデータを利用した事前学習:コード表現を効果的に学習
📦 インストール
依存関係のインストール
pip install torch
pip install transformers
クラスのダウンロード
wget https://raw.githubusercontent.com/microsoft/CodeBERT/master/UniXcoder/unixcoder.py
💻 使用例
基本的な使用法
import torch
from unixcoder import UniXcoder
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = UniXcoder("microsoft/unixcoder-base")
model.to(device)
高度な使用法
エンコーダーのみのモード(コード検索)
func = "def f(a,b): if a>b: return a else return b"
tokens_ids = model.tokenize([func],max_length=512,mode="<encoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
tokens_embeddings,max_func_embedding = model(source_ids)
func = "def f(a,b): if a<b: return a else return b"
tokens_ids = model.tokenize([func],max_length=512,mode="<encoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
tokens_embeddings,min_func_embedding = model(source_ids)
nl = "return maximum value"
tokens_ids = model.tokenize([nl],max_length=512,mode="<encoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
tokens_embeddings,nl_embedding = model(source_ids)
print(max_func_embedding.shape)
print(max_func_embedding)
デコーダーのみのモード(コード補完)
context = """
def f(data,file_path):
# write json data into file_path in python language
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<decoder-only>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=True, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print(context+predictions[0][0])
エンコーダー-デコーダーモード(関数名予測、API推薦、コード要約)
context = """
def <mask0>(data,file_path):
data = json.dumps(data)
with open(file_path, 'w') as f:
f.write(data)
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print([x.replace("<mask0>","").strip() for x in predictions[0]])
context = """
def write_json(data,file_path):
data = <mask0>(data)
with open(file_path, 'w') as f:
f.write(data)
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print([x.replace("<mask0>","").strip() for x in predictions[0]])
context = """
# <mask0>
def write_json(data,file_path):
data = json.dumps(data)
with open(file_path, 'w') as f:
f.write(data)
"""
tokens_ids = model.tokenize([context],max_length=512,mode="<encoder-decoder>")
source_ids = torch.tensor(tokens_ids).to(device)
prediction_ids = model.generate(source_ids, decoder_only=False, beam_size=3, max_length=128)
predictions = model.decode(prediction_ids)
print([x.replace("<mask0>","").strip() for x in predictions[0]])
📚 ドキュメント
モデル詳細
属性 |
详情 |
モデルタイプ |
特徴量エンジニアリング |
開発者 |
Microsoftチーム |
共有元 |
Hugging Face |
言語 |
英語 |
ライセンス |
Apache-2.0 |
親モデル |
RoBERTa |
詳細情報リソース |
関連論文 |
🔧 技術詳細
UniXcoderは、マルチモーダルデータを活用してコード表現を事前学習する統一されたクロスモーダル事前学習モデルです。このモデルは、コードコメントやASTなどのデータを利用して、コード関連の様々なタスクに対応できます。
📄 ライセンス
このモデルは、Apache-2.0ライセンスの下で提供されています。
参考文献
もしこのコードやUniXcoderを使用する場合は、以下の文献を引用してください。
@article{guo2022unixcoder,
title={UniXcoder: Unified Cross-Modal Pre-training for Code Representation},
author={Guo, Daya and Lu, Shuai and Duan, Nan and Wang, Yanlin and Zhou, Ming and Yin, Jian},
journal={arXiv preprint arXiv:2203.03850},
year={2022}
}