gpt4 book ai didi

lua - Lua 中 ()() 语法有特殊含义吗

转载 作者:行者123 更新时间:2023-12-03 02:37:33 27 4
gpt4 key购买 nike

我在最近读的一些Lua源文件中经常看到这种类型的语法,这是什么意思,特别是第二对括号例如,第 8 行 https://github.com/karpathy/char-rnn/blob/master/model/LSTM.lua

local LSTM = {}
function LSTM.lstm(input_size, rnn_size, n, dropout)
dropout = dropout or 0

-- there will be 2*n+1 inputs
local inputs = {}
table.insert(inputs, nn.Identity()()) -- line 8
-- ...

nn.Identity的源代码 https://github.com/torch/nn/blob/master/Identity.lua

************更新****************

()() 模式在 torch 库“nn”中被大量使用。第一对括号创建容器/节点的对象,第二对括号引用依赖节点。

例如,y = nn.Linear(2,4)(x)表示x连接到y,并且从1*2到1*4的变换是线性的。我只是了解用法,它是如何接线的似乎可以通过下面的答案之一来回答。

无论如何,该界面的用法在下面有详细记录。 https://github.com/torch/nngraph/blob/master/README.md

最佳答案

不,()()在Lua中没有特殊含义,它只是两个调用运算符()在一起。

操作数可能是一个返回函数的函数(或者,一个实现call元方法的表)。例如:

function foo()
return function() print(42) end
end

foo()() -- 42

关于lua - Lua 中 ()() 语法有特殊含义吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30983354/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com