Author

Alejandro Alcalde

Data Scientist and Computer Scientist. Creator of this blog.

Alejandro Alcalde's posts | Porfolio

Table Of Contents

Lets suppose the following R snippet:

SGD <- function(...) {
  # Stochastic gradient descent
  #

  w <- matrix(rep(0,3))

  # ...

  update <- function(x) {
    # Here we need to modify w
  }

  while (above.tolerance) {
    w.old <- w
    apply(data, 1, update)
    # ...
  }
  w
}

The above code does not work, although update function can see the value of w, which is in SDG scope, can not modify its value, what update modifies its a local copy of w in its scope. For SDG to work, we need to update w for each point and that in SDG scope this new value is reflected.

For the code to work, at first I thought in declaring w as a global variable with <<- operator, which is a bad idea, because w would be global to the entire R program. In this case, we only that w can be modified by update function. So searching I found a workaround to create a local environment to the function SDG, and then use it inside update, here is the code:

SGD <- function(...) {
  # Stochastic gradient descent
  #

  w.env <- new.env()
  w.env$w <- matrix(rep(0,3))

  # ...

  update <- function(x) {
    # Here we need to modify w
    # Using w.env$w
  }

  while (above.tolerance) {
    w.old <- w.env$w
    apply(data, 1, update)
    # ...
  }
  w.env$w
}

With this little change, update is accessing and modifying w, updating it correctly in each apply iteration.

Hope it helps.

Bibliography

Spot a typo?: Help me fix it by contacting me or commenting below!

Categories:Tags: