Wednesday, May 11, 2016

Using the optim and dp packages together in Torch 7

When I first started learning about Torch I immediately wanted to use the dp package. It provides many common data sets and a nice framework for quickly running experiments. I was also interested in the optim package because it contains an implementation of rprop, which is what I wanted to experiment with.

With many things to learn, I was hoping to find a quick example of how to use these two packages together so that I could get started right away. I found plenty of examples of how to use each package on its own, but I couldn't find one that combined the two. It turns out that it's actually pretty easy, but when you're just getting started, don't know lua, and have a bunch of source to wade through to figure out how things interact, it is not very intuitive.


A typical optim example looks like this:


Looping, batch generation, and evaluation of the network and criterion are done by hand. Progress display, early stopping, and network saving also would need to be implemented by hand. This is Torch, in its raw form. The dp package provides all of these things for you. A typical dp example looks like this:


So how do we integrate these two patterns? We need to call our optim function of choice as part of the dp.Optimizer callback, since that is where network parameters are adjusted. But since we aren't making the usual forward and backward calls, where do we get the loss? The optimizer itself stores the loss after processing each batch. It is accessible as train.err in the example above. This can be seen in propigator.lua, in the function Propigator:forward(batch).

Putting these examples together, we get a callback function that looks like this:


I have uploaded a complete working example to github to get you started. The network is pretty terrible, but it will train quickly on just a CPU. Besides, you want to build your own network, that's why you're here, right?

No comments:

Post a Comment