Skip to main content
 首页 » 编程设计

python之Lua 函数返回值的元组

2025年05月04日25qq号

我看过下面的python代码:

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

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

return (wx_grad, wRec_grad), grad_over_time 

如何在lua中返回类似上述的元组,以便在torch中实现?

请您参考如下方法:

您可以为此使用表格。

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).