【问题标题】:Using Huggingface Transformer Models in R在 R 中使用 Huggingface Transformer 模型
【发布时间】:2023-01-28 06:06:51
【问题描述】:

我正在尝试在 R 中使用不同的 Huggingface 模型。 这通过 reticulate 导入 transformers 包来工作(谢谢,https://rpubs.com/eR_ic/transfoRmers

输入只需要一个字符串的模型对我有用。 有些模型需要一个列表或一个向量,我根本不知道从哪里获得有关如何准确调用模型的信息。

以这个模型为例。 https://huggingface.co/openai/clip-vit-base-patch32。 从 python 示例中我知道它需要一张照片和(我假设)可能类的字符向量。 Python 输入是:text=["a photo of a cat", "a photo of a dog"], images=image

library(reticulate)
library(here)
library(tidyverse)
transformers <- reticulate::import("transformers")
image_classification_zero_shot <- transformers$pipeline(task = "zero-shot-image-classification", model = "openai/clip-vit-base-patch32")
image_classification <- transformers$pipeline(task = "image-classification", model = "microsoft/beit-base-patch16-224-pt22k-ft22k")

image_url <- "http://images.cocodataset.org/val2017/000000039769.jpg"

只需要图像的模型就可以工作

image_classification(images = image_url)

还需要使用类输入字符的模型不起作用。

image_classification_zero_shot(text = c("cats", "dogs"), images = image_url)
image_classification_zero_shot(text = "[cats, dogs]", images = image_url)

> Error in py_call_impl(callable, dots$args, dots$keywords) : 
   TypeError: object of type 'NoneType' has no len()

View(image_classification_zero_shot) 不产生任何信息。

  1. 如何让零射击模型工作?
  2. 我通常如何获取有关如何在 R 中调用这些模型的信息?这是一个函数,难道我不能在某处(在 R 中或在 huggingface 中)找到有关其参数的信息吗?

    非常感谢你!

【问题讨论】:

    标签: r huggingface-transformers reticulate


    【解决方案1】:

    我遇到了另一个名为“jonas/sdg_classifier_osdg”的 huggingface 转换器的类似问题。

    Error in py_call_impl(callable, dots$args, dots$keywords) : 
    TypeError: linear(): argument 'input' (position 1) must be Tensor, not 
    BatchEncoding
    

    解决方案:

    1. 在 rmarkdown 笔记本中编写 python 代码
    2. 在控制台激活repl_python()然后编写python代码。

      相同的代码在用 Python 编写时会生成一个张量,但在用 R 编写时会生成一个字符串。

      进一步的问题:

      如何有效地将 R 字符串转换为 pytorch 可以理解的张量? (当我尝试将手电筒与网状结构一起加载时,R 崩溃了)

    【讨论】:

      猜你喜欢
      • 2021-12-18
      • 1970-01-01
      • 2021-12-06
      • 2020-10-07
      • 2021-11-08
      • 2020-04-02
      • 2021-02-20
      • 2021-07-16
      • 2022-11-08
      相关资源
      最近更新 更多