Example #1
0
    def validate(self):
        self.model.is_good_mode = True
        good_val_loss, good_val_acc = validate(self.model, self.train_dataloader, self.criterion)
        self.model.is_good_mode = False
        bad_val_loss, bad_val_acc = validate(self.model, self.train_dataloader, self.criterion)
        self.model.is_good_mode = True

        self.writer.add_scalar('good/val/loss', good_val_loss, self.num_epochs_done)
        self.writer.add_scalar('good/val/acc', good_val_acc, self.num_epochs_done)
        self.writer.add_scalar('bad/val/loss', bad_val_loss, self.num_epochs_done)
        self.writer.add_scalar('bad/val/acc', bad_val_acc, self.num_epochs_done)
        self.writer.add_scalar('diff/val/loss', good_val_loss - bad_val_loss, self.num_epochs_done)
        self.writer.add_scalar('diff/val/acc', good_val_acc - bad_val_acc, self.num_epochs_done)

        self.plot_all_weights_histograms()

        if self.num_epochs_done > self.config.get('val_acc_stop_threshold_num_warmup_epochs', -1):
            if good_val_acc < self.config.get('good_val_acc_stop_threshold', 0.):
                self.stop(f'Good val accuracy is too low (epoch #{self.num_epochs_done}): {good_val_acc}')
            elif bad_val_acc > self.config.get('bad_val_acc_stop_threshold', 1.):
                self.stop(f'Bad val accuracy is too high (epoch #{self.num_epochs_done}): {bad_val_acc}')
            else:
                pass

        if self.num_epochs_done > self.config.get('diff_threshold_num_warmup_epochs', -1):
            if good_val_acc - bad_val_acc < self.config.get('good_and_bad_val_acc_diff_threshold', float('-inf')):
                self.stop(f'Difference between good and val accuracies is too small '\
                          f'(epoch #{self.num_epochs_done}): {good_val_acc} - {bad_val_acc} = {good_val_acc - bad_val_acc}')
Example #2
0
    def test_already_published_response_means_aok(self):
        "bot-lax coerces already-published error responses to successful 'published' responses"
        lax_resp = {
            'status': conf.ERROR,
            'message': 'mock',
            'token': 'a',
            'id': 'b',
            'code': 'already-published'
        }

        # coercion happens
        resp = adapt.mkresponse(**lax_resp)
        self.assertEqual(resp['status'], conf.PUBLISHED)

        # coercion doesn't result in an invalid response
        utils.validate(resp, conf.RESPONSE_SCHEMA)
    def test_already_published_response_means_aok(self):
        "bot-lax coerces already-published error responses to successful 'published' responses"
        lax_resp = {
            'status': conf.ERROR,
            'message': 'mock',
            'token': 'a',
            'id': 'b',

            'code': 'already-published'
        }

        # coercion happens
        resp = adapt.mkresponse(**lax_resp)
        self.assertEqual(resp['status'], conf.PUBLISHED)

        # coercion doesn't result in an invalid response
        utils.validate(resp, conf.RESPONSE_SCHEMA)
Example #4
0
 def from_file(self, lines):
     """Creating treasure map as list 5 by 5 from file"""
     for i in range(5):
         line = lines[i].split()
         row = list(map(int, line))
         if validate(row):
             self.grid[i] = [Cell(value) for value in row]
         else:
             input("Error: your file has incorrect values. For exit press Enter")
Example #5
0
 def from_keyboard(self):
     """Creating treasure map as list 5 by 5 from keyboard"""
     counter = 0
     while counter < 5:
         line = input()
         line = line.strip().split()
         row = list(map(int, line))
         if validate(row):
             self.grid[counter] = [Cell(value) for value in row]
             counter += 1
Example #6
0
def treasure_map_from_keyboard():
    """Creating treasure map as list 5 by 5 from keyboard"""
    treasure_map = []
    while len(treasure_map) < 5:
        line = input()
        line = line.strip().split()
        row = list(map(int, line))
        if validate(row):
            treasure_map.append(row)
    return treasure_map
Example #7
0
def treasure_map_from_file(lines):
    """Creating treasure map as list 5 by 5 from file"""
    treasure_map = []
    for line in lines:
        line = line.split()
        row = list(map(int, line))
        if validate(row):
            treasure_map.append(row)
        else:
            return False
    return treasure_map
Example #8
0
def main(ix):
    args = utils.initialize()
    tr_dl, vd_dl, ts_dl, e_ix_ln, r_ix_ln, t_ix_ln, tp_ix, tp_rix = utils.data(
        args)
    mdl, opt, lr_sc, ls_f, st_e, bst_ls = utils.prepare(
        args, e_ix_ln, r_ix_ln, t_ix_ln)
    tb_sw = SummaryWriter() if utils.is_master(args) else None
    if not args.test:
        ls_mtr = utils.BestMetric() if utils.is_master(args) else None
        for e in range(st_e, args.epochs + 1):
            utils.train(args, e, mdl, opt, ls_f, tr_dl, tb_sw)
            if e % args.validation_frequency == 0 or e == args.epochs:
                utils.validate(args, e, mdl, opt, ls_f, vd_dl, tp_ix, tp_rix,
                               ls_mtr, tb_sw)
            lr_sc.step()
    else:
        utils.test(args, mdl, ts_dl, tp_ix, tp_rix, tb_sw)
    if utils.is_master(args):
        tb_sw.flush()
        tb_sw.close()
Example #9
0
                for yhat, y, emb in zip(outputs, labels, embedds)
            ]

        for l in losses:
            ag.backward(l)

        trainer.step(batch_size)
        acc_mtc.update(labels, outputs)
        loss_mtc.update(0, losses)

        if (it % save_period) == 0 and it != 0:
            _, train_loss = loss_mtc.get()
            _, train_acc = acc_mtc.get()
            toc = time.time()

            logger.info(
                '\n[epoch % 2d] [it % 3d] train loss: %.6f, train_acc: %.6f | time: %.6f'
                % (epoch, it, train_loss, train_acc, toc - tic))
            logger.info("Radius {}".format(
                loss.R.data(ctx=mx.gpu(0)).asscalar()))
            results = validate(net, ctx, val_datas, targets)
            for result in results:
                logger.info('{}'.format(result))
            loss_mtc.reset()
            acc_mtc.reset()
            tic = time.time()
            net.save_parameters("./models/mobilefacenet-ring-it-%d.params" %
                                it)
        it += 1
    epoch += 1
        print(f'Start epoch {epoch}')
        model.train()
        epoch_train_loss = train_epoch(model=model,
                                       optimizer=optimizer,
                                       loader=train_loader,
                                       device=device,
                                       criterion=criterion,
                                       epoch_num=epoch,
                                       log_step=args.log_step,
                                       use_batches=-1)
        train_loss.extend(epoch_train_loss)
        model.eval()
        print('Start validation')
        val_loss, val_ppl = validate(model=model,
                                     loader=valid_loader,
                                     device=device,
                                     criterion=criterion,
                                     use_batches=-1)
        best_checkpoint = make_checkpoint(model,
                                          val_ppl,
                                          checkpoint_dir=args.checkpoint_dir,
                                          current_checkpoint=best_checkpoint,
                                          prefix=args.prefix,
                                          metric_name=args.metric_name)
        valid_loss.append(val_loss)
        valid_ppl.append(val_ppl)

    fig, axs = plt.subplots(3, figsize=(10, 10))

    axs[0].plot(train_loss, '-*')
    axs[0].grid()
Example #11
0
    def validate(self):
        self.model.eval()
        loss, acc = validate(self.model, self.val_dataloader, self.criterion)

        self.writer.add_scalar('Val/loss', loss.item(), self.num_iters_done)
        self.writer.add_scalar('Val/acc', acc.item(), self.num_iters_done)
def test_validate_negative(capsys, test_input, expected):
    validate(test_input)
    captured = capsys.readouterr()
    assert captured.out == ERROR_MESSAGE
def test_validate(test_input, expected):
    assert validate(test_input) == expected
Example #14
0
def apply_validation(ctx, new, prev):
    validate(prev, new, ctx.nlp)
    return (ctx, new)
 def test_incoming_messages(self):
     "each message generated is a valid request"
     inst = fs_adaptor.IncomingQueue(self.ingest_dir, 'ingest')
     list(map(lambda req: utils.validate(req, conf.REQUEST_SCHEMA), list(inst))) # raises ValidationException if invalid
 def test_incoming_messages(self):
     "each message generated is a valid request"
     inst = fs_adaptor.IncomingQueue(self.ingest_dir, 'ingest')
     list(
         map(lambda req: utils.validate(req, conf.REQUEST_SCHEMA),
             list(inst)))  # raises ValidationException if invalid