Search code examples
pythonluatorch

Lua function returning tuple of values


I have seen the following python code:

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

And in the function backward_gradient, I have the following return statement:

return (wx_grad, wRec_grad), grad_over_time

How can I return a tuple similar to the above in lua so that it can be implemented in torch?


Solution

  • You can use a table for that.

    return {wx_grad, wRec_grad}, grad_over_time
    

    Refer to for more information 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).