def _create_job(self):
     self.job_id = self.db.execute("""
     INSERT INTO ppdl.job (id) VALUES (DEFAULT) RETURNING id;
     """,
                                   returning=True)
     log.debug("created job with id {}".format(self.job_id))
     self.db.commit()
 def _create_cycle(self):
     assert self.job_id is not None
     self.cycle_id = self.db.execute("""
     INSERT INTO ppdl.cycle(job_id, starts_at, finishes_at) (
         SELECT %s, now(), now() + interval '{}' sec
     ) RETURNING id
     """.format(self.cycle_time), (self.job_id, ),
                                     returning=True)
     log.debug("created cycle with id {}".format(self.cycle_id))
     self.db.commit()
 def upload(self, request):
     log.debug("Client {} uploading".format(request.clientId.txt))
     # TODO don't let a client upload twice per cycle
     if self.phase != PHASE_TRAIN:
         raise self.Exception("cannot upload except in the training phase")
     if self.cycle_id != request.cycleId.num:
         raise self.Exception(
             "upload is for cycle {}, but server is on cycle {}".format(
                 request.cycleId.num, self.cycle_id))
     parameters = {p.index: p.value for p in request.deltas.parameters}
     self._add_parameters(parameters, request.clientId.txt)
     return pb.UploadResponse()
    def _run_loop(self):
        # set initial parameters in first cycle
        self._create_cycle()
        parameters = self.initial_parameters_f()
        self._add_parameters(parameters, ADMIN_USER)

        while True:
            log.debug("Starting cycle {}".format(self.cycle_id))
            self._create_cycle()
            self.clock = self.cycle_time
            log.info("Entering download/train phase")
            self._train_phase()
            log.info("Entering update phase")
            self._update_phase()
 def download(self, request):
     log.debug("Client {} downloading".format(request.clientId.txt))
     if self.phase != PHASE_TRAIN:  # TODO maybe only allow downloads if enough time remaining
         raise self.Exception(
             "cannot download except in the training phase")
     # download a random subset of the parameters
     parameters_l = self._get_last_parameters()
     parameters = [
         pb.IndexedValue(index=i, value=val) for i, val in random.sample(
             list(parameters_l.items()),
             int(len(parameters_l) * self.dropout_ratio))
     ]
     log.debug(
         "all parameters = {}, parameters being downloaded = {}".format(
             parameters_l, parameters))
     parameters = pb.Parameters(parameters=parameters)
     return pb.DownloadResponse(
         cycleId=pb.CycleId(num=self.cycle_id),
         parameters=parameters,
     )