def solve(self, decaf_net, previous_net=None): """Solves the net.""" # first, run a pass to initialize all the parameters. logging.info('StochasticSolver: precomputing.') self._iter_idx = 0 self._decaf_net = decaf_net self._previous_net = previous_net initial_loss = decaf_net.forward_backward(self._previous_net) logging.info('StochasticSolver: initial loss: %f.', initial_loss) logging.info('(Under mpirun, the given loss will just be an estimate' ' on the root node.)') self.initialize_status() # the main iteration timer = Timer() logging.info('StochasticSolver: started.') for _ in range(self._max_iter): if mpi.SIZE > 1: loss = mpi.COMM.allreduce( decaf_net.forward_backward(self._previous_net)) / mpi.SIZE # we need to broadcast and average the parameters params = decaf_net.params() for param in params: diff = param.diff() diff_cache = diff.copy() mpi.COMM.Allreduce(diff_cache, diff) diff /= mpi.SIZE else: loss = decaf_net.forward_backward(self._previous_net) self.compute_update_value() decaf_net.update() if (mpi.is_root() and self._snapshot_interval > 0 and self._iter_idx > 0 and self._iter_idx % self._snapshot_interval == 0): # perform snapshot. self.snapshot() if (self.spec.get('disp', 0) and self._iter_idx % self.spec['disp']): logging.info('Iter %d, loss %f, elapsed %s', self._iter_idx, loss, timer.lap()) self.iter_callback(loss) self._iter_idx += 1 # perform last snapshot. if mpi.is_root() and 'folder' in self.spec: self.snapshot(True) mpi.barrier() logging.info('StochasticSolver: finished. Total time %s.', timer.total())
def testIsRoot(self): if mpi.RANK == 0: self.assertTrue(mpi.is_root()) else: self.assertFalse(mpi.is_root())