找不到在 Torch Lua 代码中定义 addmm 函数的位置
Can't find where addmm function is defined in Torch Lua code
我正在了解在 Torch Lua 中实现的神经网络。在向后通过线性层期间,它调用一个名为 Linear:updateGradInput(https://github.com/torch/nn/blob/master/Linear.lua#L75 )
的函数
function Linear:updateGradInput(input, gradOutput)
if self.gradInput then
local nElement = self.gradInput:nElement()
self.gradInput:resizeAs(input)
if self.gradInput:nElement() ~= nElement then
self.gradInput:zero()
end
if input:dim() == 1 then
self.gradInput:addmv(0, 1, self.weight:t(), gradOutput)
elseif input:dim() == 2 then
self.gradInput:addmm(0, 1, gradOutput, self.weight)
end
return self.gradInput
end
end
在该函数中,通过调用名为 addmm(https://github.com/torch/nn/blob/master/Linear.lua#L86) 的函数执行基本的矩阵乘法运算。我找不到这个 addmm 函数的定义位置。
TH 库 (https://github.com/torch/torch7/blob/master/lib/TH/generic/THTensorMath.c#L1282) 中定义了一个 addmm 函数,但我不确定 Lua 代码如何连接到 C 中的此代码。
刚刚弄清楚Lua代码和C代码之间的联系。 Lua 代码中对 addmm 的调用指向此函数 (https://github.com/torch/torch7/blob/master/TensorMath.lua#L487-L510) and this in turn calls the addmm function defined in the C Torch Library defined here(https://github.com/torch/torch7/blob/master/lib/TH/generic/THTensorMath.c#L1282)。
这很棘手,因为 Lua 通过字符串构造对 C 函数的调用。
我正在了解在 Torch Lua 中实现的神经网络。在向后通过线性层期间,它调用一个名为 Linear:updateGradInput(https://github.com/torch/nn/blob/master/Linear.lua#L75 )
的函数function Linear:updateGradInput(input, gradOutput)
if self.gradInput then
local nElement = self.gradInput:nElement()
self.gradInput:resizeAs(input)
if self.gradInput:nElement() ~= nElement then
self.gradInput:zero()
end
if input:dim() == 1 then
self.gradInput:addmv(0, 1, self.weight:t(), gradOutput)
elseif input:dim() == 2 then
self.gradInput:addmm(0, 1, gradOutput, self.weight)
end
return self.gradInput
end
end
在该函数中,通过调用名为 addmm(https://github.com/torch/nn/blob/master/Linear.lua#L86) 的函数执行基本的矩阵乘法运算。我找不到这个 addmm 函数的定义位置。
TH 库 (https://github.com/torch/torch7/blob/master/lib/TH/generic/THTensorMath.c#L1282) 中定义了一个 addmm 函数,但我不确定 Lua 代码如何连接到 C 中的此代码。
刚刚弄清楚Lua代码和C代码之间的联系。 Lua 代码中对 addmm 的调用指向此函数 (https://github.com/torch/torch7/blob/master/TensorMath.lua#L487-L510) and this in turn calls the addmm function defined in the C Torch Library defined here(https://github.com/torch/torch7/blob/master/lib/TH/generic/THTensorMath.c#L1282)。
这很棘手,因为 Lua 通过字符串构造对 C 函数的调用。