Skip to content
This repository was archived by the owner on Nov 1, 2021. It is now read-only.
This repository was archived by the owner on Nov 1, 2021. It is now read-only.

autograd.optim wrapper does not update state value #121

@hbqjzj

Description

@hbqjzj

It seems that autograd.optim wrapper doesn't update "state" value. It outputs "states", but it is impossible to use it iteratively.

local function wrap(optimfn)
   return function(fn, state, params)
      local states = { }
      local flatParams = util.sortedFlatten(params)
      for i = 1, #flatParams do
         states[i] = util.deepCopy(state)   --this is a deep copy of state, so it is not updated
      end
      return function(...)
         local out = {fn(params, ...)}
         local grads, loss = out[1], out[2]
         local flatGrads = util.sortedFlatten(grads)
         for i = 1, #flatGrads do
            local grad = flatGrads[i]
            optimfn(function()
               return loss, grad
            end, flatParams[i], states[i])
         end
         return table.unpack(out)
      end, states   --now, states is a table of states, which is impossible to pass back to the wrapper
   end
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions