Lua 函数返回值元组

Lua function returning tuple of values

我看过以下python代码:

W_grads, _ = backward_gradient(X, S, grad_out, wRec)

在函数 backward_gradient 中,我有以下 return 语句:

return (wx_grad, wRec_grad), grad_over_time

如何在lua中return一个类似于上面的元组,以便在torch中实现?

Return一个table:

return {wx_grad, wRec_grad}, grad_over_time

然后访问W_grads[1]W_grads[2]

您可以为此使用 table。

return {wx_grad, wRec_grad}, grad_over_time

更多信息请参考http://www.lua.org/manual/5.3/manual.html#2.1

Tables are the sole data-structuring mechanism in Lua; they can be used to represent ordinary arrays, sequences, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing a.name as syntactic sugar for a["name"]. There are several convenient ways to create tables in Lua (see §3.4.9).