微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在多会话中使用 rTorch 进行 furrr

如何解决在多会话中使用 rTorch 进行 furrr


我想在 furrr “循环”中使用 rTorch一个最小的例子似乎是:

library(rTorch)
torch_it <- function(i) {
  #.libPaths("/User/homes/mreichstein/.R_libs_macadamia4.0/")
  #require(rTorch)
  cat("torch is: "); print(torch)
  out  <- torch$tensor(1)
  out
}

library(furrr)
plan(multisession,workers=8) # this works with sequential or multicore
result <- future_map(1:5,torch_it)

我得到的输出

torch is: <environment: 0x556b57f07990>
attr(,"class")
[1] "python.builtin.module" "python.builtin.object"
Error in torch$tensor(1) : attempt to apply non-function

当我使用多核或顺序时,我得到了预期的输出

torch is: Module(torch)
torch is: Module(torch)
torch is: Module(torch)
torch is: Module(torch)
torch is: Module(torch)

... 结果是预期的张量列表。 取消注释函数中的第一行,假设新会话“需要这个”没有帮助。

更新:我在 torch_it 函数添加torch <- import("torch")。然后它运行没有错误,但我得到的结果是一个空指针列表(?):

> result
[[1]]
<pointer: 0x0>
[[2]]
<pointer: 0x0>
[[3]]
<pointer: 0x0>
[[4]]
<pointer: 0x0>
[[5]]
<pointer: 0x0>

那么,如何将 rTorch 正确链接到每个多会话? 提前致谢!

信息:

> R.version
               _                           
platform       x86_64-redhat-linux-gnu     
arch           x86_64                      
os             linux-gnu                   
system         x86_64,linux-gnu           
status                                     
major          4                           
minor          0.3                         
year           2020                        
month          10                          
day            10                          
svn rev        79318                       
language       R                           
version.string R version 4.0.3 (2020-10-10)
nickname       Bunny-Wunnies Freak Out

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。