模型:
huggingface/CodeBERTa-small-v1
CodeBERTa是在GitHub上的 CodeSearchNet 数据集上训练的类似于RoBERTa的模型。
支持的语言:
"go" "java" "javascript" "php" "python" "ruby"
分词器是使用Hugging Face tokenizers对该语料库进行训练的字节级BPE分词器。
因为它是在代码语料库上训练的(而不是自然语言),所以它可以高效地编码语料库(与gpt2/roberta对同一语料库进行分词相比,序列长度可以缩短33%至50%)。
该(小型)模型是一个6层、8400万参数的RoBERTa-like Transformer模型 - 和DistilBERT具有相同数量的层和头 - 从默认初始化设置中初始化,并在完整语料库(~2M函数)上进行了5个epochs的训练。
PHP_CODE = """
public static <mask> set(string $key, $value) {
if (!in_array($key, self::$allowedKeys)) {
throw new \InvalidArgumentException('Invalid key given');
}
self::$storedValues[$key] = $value;
}
""".lstrip()
from transformers import pipeline
fill_mask = pipeline(
"fill-mask",
model="huggingface/CodeBERTa-small-v1",
tokenizer="huggingface/CodeBERTa-small-v1"
)
fill_mask(PHP_CODE)
## Top 5 predictions:
#
' function' # prob 0.9999827146530151
'function' #
' void' #
' def' #
' final' #
PYTHON_CODE = """
def pipeline(
task: str,
model: Optional = None,
framework: Optional[<mask>] = None,
**kwargs
) -> Pipeline:
pass
""".lstrip()
结果:
'framework', 'Framework', ' framework', 'None', 'str'
这个程序可以自动完成自己! 😱
fill_mask("My name is <mask>.")
# {'sequence': '<s> My name is undefined.</s>', 'score': 0.2548016905784607, 'token': 3353}
# {'sequence': '<s> My name is required.</s>', 'score': 0.07290805131196976, 'token': 2371}
# {'sequence': '<s> My name is null.</s>', 'score': 0.06323737651109695, 'token': 469}
# {'sequence': '<s> My name is name.</s>', 'score': 0.021919190883636475, 'token': 652}
# {'sequence': '<s> My name is disabled.</s>', 'score': 0.019681859761476517, 'token': 7434}
这(在某种程度上)可行,因为代码中包含注释(注释中包含自然语言)。
当然,计算机科学家最常见的名字肯定是未定义的 🤓。
请查看模型卡片以了解 huggingface/CodeBERTa-language-id 🤯。
@article{husain_codesearchnet_2019,
title = {{CodeSearchNet} {Challenge}: {Evaluating} the {State} of {Semantic} {Code} {Search}},
shorttitle = {{CodeSearchNet} {Challenge}},
url = {http://arxiv.org/abs/1909.09436},
urldate = {2020-03-12},
journal = {arXiv:1909.09436 [cs, stat]},
author = {Husain, Hamel and Wu, Ho-Hsiang and Gazit, Tiferet and Allamanis, Miltiadis and Brockschmidt, Marc},
month = sep,
year = {2019},
note = {arXiv: 1909.09436},
}