Exemple #1
0
    def train(self, epochs, learning_rate, kernel_size, hidden_size, model_cls,
              interaction, dropout):
        if model_cls == "cnn":
            model = CNN(embedding=self.data_train.vocab_embedding,
                        embedding_size=self.data_train.vocab_embedding_size,
                        lengths=self.data_train.lengths(),
                        kernel_size=kernel_size,
                        hidden_size=hidden_size,
                        interaction=interaction,
                        dropout=dropout)
        else:
            model = GRU(embedding=self.data_train.vocab_embedding,
                        embedding_size=self.data_train.vocab_embedding_size,
                        encoding_size=hidden_size,
                        interaction=interaction,
                        dropout=dropout)
        if self.use_gpu:
            model = model.cuda()

        loader = self.data_train.get_loader()
        loss_fn = torch.nn.CrossEntropyLoss()
        optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)

        losses = []
        accuracies = []
        for epoch in range(1, epochs + 1):
            e_loss = []
            print("\nStarting epoch {}".format(epoch))
            for i, (s1, s2, labels) in enumerate(loader):
                if self.use_gpu:
                    s1, s2, labels = s1.cuda(), s2.cuda(), labels.cuda()
                model.train()
                optimizer.zero_grad()

                # Forward pass
                logits = model(s1, s2)
                instance_loss = loss_fn(logits, labels)

                # Backward and optimize
                instance_loss.backward()
                optimizer.step()

                losses.append(instance_loss.item())
                e_loss.append(instance_loss.item())

                # validate every 100 iterations
                if i > 0 and i % 100 == 0:
                    val_acc = self.validate(model)
                    accuracies.append(val_acc)
                    print(
                        'Epoch: [{}/{}]\tStep: [{}/{}]\tValidation Acc: {:.4f}'
                        .format(epoch, epochs, i, len(loader), val_acc))
#             self.analyzer.plot_live_lr(e_loss, title="Epoch {}".format(epoch))

        avg_acc = sum(accuracies[-5:]) / 5

        self.analyzer.record(model.cpu(),
                             losses,
                             epochs=epochs,
                             accuracies=accuracies,
                             learning_rate=learning_rate,
                             hidden_size=hidden_size,
                             kernel_size=kernel_size,
                             validation_accuracy=avg_acc,
                             model_name=model_cls,
                             dropout=dropout,
                             interaction=interaction,
                             data_length=32 * len(loader))
        self.analyzer.print_validation_results(self, model_cls, model)
        print("Final Accuracy: {}".format(avg_acc))
Exemple #2
0
        else:
            decomposer.replace_layer(keys=args.key, type=args.type)

        path_o = f'models/{args.model}_{args.factorization}_{args.key}_{args.type}.pth'
        torch.save(decomposer.model, path_o)
        logger.info('===========saved============')
        logger.info(f'saved to {path_o}')

    elif args.eval:
        path = f'models/{args.model}.pth'

        logger.info(f'model: {path}')
        model = torch.load(path)
        model.eval()
        device_name = args.device
        if device_name == 'cpu':
            model.cpu()
            use_cuda = False

        else:
            model.cuda()
            use_cuda = True

        device = torch.device(device_name)
        summary(model, (3, 224, 224), device=device_name)

        labels = ['cat', 'dog']
        test_loader = load_test_dataset(args.test_path)
        accuracy = predict_loader(model, test_loader, device, args.verbose)
        print('accuracy:', accuracy)