【问题标题】:Call lua function (with require 'nn') in c++在c ++中调用lua函数(需要'nn')
【发布时间】:2016-08-02 07:43:14
【问题描述】:

我有一个 Lua 函数:

require 'nn'
require 'image'
require 'torch'
require './lib/data_augmentation'
function predict (x) do
  model = torch.load("trained.t7")
  img = image.load(x)
  img_tensor = torch.DoubleTensor(2, 3, 32, 32)
  img_tensor[1]:copy(img)
  x = data_augmentation(img_tensor[1])
  preprocessing(x,params)
  preds = torch.Tensor(4):zero()
  step = 64
  for j = 1, x:size(1), step do
     batch = torch.Tensor(step, x:size(2), x:size(3), x:size(4)):zero()
     n = step
     if j + n > x:size(1) then
        n = 1 + n - ((j + n) - x:size(1))
     end
     batch:narrow(1, 1, n):copy(x:narrow(1, j, n))
     z = model:forward(batch):float()
     for k = 1, n do
       preds = preds + z[k]
     end
  end
  preds:div(x:size(1))
  confidences, indices = torch.sort(preds,true)
  return indices[1]
end
end

我想在 C++ 中调用这个函数,但是我得到一个错误:

PANIC: unprotected error in call to Lua API (attempt to call a nil value)

因为需要'...'

我应该怎么做,C++ 才能识别所有的包或其他 Lua 脚本(如 data_augmentation)?

【问题讨论】:

  • 您需要在所有库组件上调用 luaL_dofile 之类的东西。
  • 我了解如何为我创建的脚本(如 data_augmentation)执行此操作。但是对于'nn'、'optim'、'image'和'torch',我应该写什么呢?我假设我必须用我的编译命令给出包的路径,对吧?提前谢谢你。
  • 我认为您需要在执行此脚本之前加载package 库。所以创建新的 Lua stet,至少打开 basepackage 库。最好打开所有标准库。
  • 寻找答案,我意识到我必须使用:luaL_requiref(L,"nn",luaopen_nn,1);在 luaL_openlibs(); 之后。问题是我应该在编译命令中包含包“nn”的路径。我正在使用 -l:nn.so.1 但它不起作用。有什么帮助吗?

标签: c++ lua torch


【解决方案1】:

我在我的 mac 上遇到了同样的问题,并用

解决了它
source ~/.profile

然后运行 ​​lua 脚本。

在这里找到完整的说明:torch.ch

# On Linux with bash
source ~/.bashrc
# On Linux with zsh
source ~/.zshrc
# On OSX or in Linux with none of the above.
source ~/.profile

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-15
    • 2017-07-22
    • 2014-09-01
    • 1970-01-01
    • 2015-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多