lisa-lab/pylearn2

[enhancement] tear_down() method for train extensions

Opened this issue · 2 comments

TNick commented

TrainExtension has a setup() but not a tear_down() / end() / terminate() / whatever(). That's probably because it is not needed most of the time.

One use case for it would be with the LiveMonitoring that binds to ports but never unbinds. It can be easily re-factored to bind in setup() and unbind in tear_down().
(one may argue that, in an yaml file, LiveMonitoring instance can be shared).

The implementation would change train.py:

    def tear_down(self):
        """
        Called at the end of main loop.
        """
        self.tear_down_extensions()
        # Does the algorithm also needs one?
        #if self.algorithm is not None:
        #    self.algorithm.tear_down()

    def tear_down_extensions(self):
        """ Calls tear_down on all extensions."""
        for ext in self.extensions:
            ext.tear_down(self.model, self.dataset, self.algorithm)

self.tear_down() would then be called at the end of main loop.


Bonus points

How would you feel about wrapping the main loop in a try ... except like so:

    def main_loop(self, time_budget=None):
        t0 = datetime.now()
        self.setup()
        try:
            if self.algorithm is None:

            # ...

            if self.save_freq > 0:
                self.save()
        except:
            raise
        finally:
            self.tear_down()

?

Yes, I think this would make sense, although in most cases, the process stops after the end of the main loop. Wouldn't the ports be unbinded then?
It may be a good idea to ignore some of the errors that may happen during tear_down if an exception had been raised.

TNick commented

The ports get un-binded when the instance gets garbage-collected, I suppose.
pylearn2 never explicitly unbinds them. LiveMonitoring exists as long as the Train object exists.

I'll get to it, then.
I'll wrap the content of def tear_down(self): in a try ... except Exception: and report with log.debug(...)