在多会话中使用 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 (将#修改为@)