详细内容或原文请订阅后点击阅览
来自 R 的最先进的 NLP 模型
如今,微软、谷歌、Facebook 和 OpenAI 正在分享自然语言处理领域的许多先进模型。然而,关于如何使用 R 中的这些模型的资料却很少。在这篇文章中,我们将展示 R 用户如何访问和受益于这些模型。
来源:RStudio AI博客简介
“Hugging Face” 的 Transformers 存储库包含大量可立即使用的先进模型,可直接下载并使用 Tensorflow 和 Keras 进行微调。
Transformers “拥抱脸”为此,用户通常需要获取:
- 模型本身(例如 Bert、Albert、RoBerta、GPT-2 等) tokenizer 对象模型的权重
在这篇文章中,我们将研究经典的二元分类任务,并在 3 个模型上训练我们的数据集:
但是,读者应该知道,Transformers 可以用于各种下游任务,例如:
- 特征提取情绪分析文本分类问答总结翻译等等。
先决条件
我们的第一项工作是通过 reticulate 安装 transformers 包。
transformersreticulate
reticulate::py_install('transformers', pip = TRUE)
reticulate::py_install('transformers', pip = TRUE)
reticulate::py_install('transformers', pip = TRUE)
reticulate
::
py_install
py_install
(
'transformers'
=
TRUE
)
然后,像往常一样,加载标准‘Keras’、‘TensorFlow’>= 2.0 和一些来自 R 的经典库。
请注意,如果在 GPU 上运行 TensorFlow,可以指定以下参数以避免内存问题。
physical_devices = tf$config$list_physical_devices('GPU')tf$config$experimental$set_memory_growth(physical_devices[[1]],TRUE)tf$keras$backend$set_floatx('float32')
physical_devices = tf$config$list_physical_devices('GPU')tf$config$experimental$set_memory_growth(physical_devices[[1]],TRUE)tf$keras$backend$set_floatx('float32')
physical_devices = tf$config$list_physical_devices('GPU')
physical_devices
=
tf
$
配置
$
list_physical_devices
(
'GPU'
)
tf$config$experimental$set_memory_growth(physical_devices[[1]],TRUE)
$