Esempio n. 1
0
    def test_classifier(self):
        device = 'cuda:0' if self._args.cuda else 'cpu'
        # Iterate over test dataset
        dataset = self._test_iterator
        # loss
        #loss_func = nn.BCEWithLogitsLoss()
        loss_func = nn.CrossEntropyLoss().to(device) 
        # progress bar
        test_bar = tqdm(desc='split=test', total=len(dataset), position=0)   
        
        running_loss = 0.0
        running_acc = 0.0
        self._classifier.eval()
        test_bar.reset()
        
        for batch_index, batch_data in enumerate(dataset):
            # the validate routine is these 4 steps:
            # step 0. get data
            x_data, target_y = batch_data.text, batch_data.label
            # step 1. compute the output
            y_pred = self._classifier(x_data=x_data)
            y_pred = y_pred.squeeze()
            # step 2. compute the loss
            loss = loss_func(y_pred, target_y)
            loss_t = loss.item()
            running_loss += (loss_t - running_loss) / (batch_index + 1)
            # setp 3. compute the accuracy
            acc_t = compute_accuracy_multi(y_pred, target_y.to(device))
            running_acc += (acc_t - running_acc) / (batch_index + 1)

            # update bar
            test_bar.set_postfix(loss=running_loss, acc=running_acc)
            test_bar.update()

        return running_loss, running_acc
Esempio n. 2
0
    def train_one_epoch(self, epoch_index):
        # Iterate over training dataset
        self._dataset.set_split('train')
        self._train_ds = DataLoader(dataset=self._dataset, batch_size=self._args.batch_size, shuffle=True, drop_last=True)
        # loss
        #loss_func = nn.BCEWithLogitsLoss()
        loss_func = nn.CrossEntropyLoss()
        # progress bar
        train_bar = tqdm(desc='split=train', total=len(self._train_ds), position=0)   
        
        running_loss = 0.0
        running_acc = 0.0
        self._classifier.train()
        train_bar.reset()
    
        device = 'cuda:0' if self._args.cuda else 'cpu'
        for batch_index, batch_data in enumerate(self._train_ds):
            # step 0. get data
            x_data, x_source_len, target_y = batch_data
            
            # setp 0.0 sort data by lens
            x_data, x_source_len, target_y = self.sort_by_len(x_data, x_source_len, target_y)
            
            # step 1. zero the gradients
            self._optimizer.zero_grad()

            # step 2. compute the output
            y_pred = self._classifier(x_data=x_data.to(device), x_len=x_source_len.to(device))
            y_pred = y_pred.squeeze()
            # step 3. compute the loss
            loss = loss_func(y_pred, target_y.to(device))
            loss_t = loss.item()
            running_loss += (loss_t - running_loss) / (batch_index + 1)

            # step 4. use loss to produce gradients
            loss.backward()

            # step 5. use _ to take gradient step
            self._optimizer.step()
            
            # compute the accuracy
            acc_t = compute_accuracy_multi(y_pred, target_y.to(device))
            running_acc += (acc_t - running_acc) / (batch_index + 1)

            # update bar
            train_bar.set_postfix(loss=running_loss, acc=running_acc, epoch=epoch_index)
            train_bar.update()

        return running_loss, running_acc
    def train_one_epoch(self, epoch):
        device = 'cuda:0' if self._args.cuda else 'cpu'
        # Iterate over training dataset
        dataset = self._train_iterator
        # loss
        # loss_func = nn.BCEWithLogitsLoss()
        loss_func = nn.CrossEntropyLoss().to(device)
        # progress bar
        train_bar = tqdm(desc='split=train', total=len(dataset), position=0)

        running_loss = 0.0
        running_acc = 0.0
        self._classifier.train()
        train_bar.reset()

        for batch_index, batch_data in enumerate(dataset):
            # the training routine is these 6 steps:
            # step 0. get data
            x_data, target_y = batch_data.text, batch_data.label
            # step 1. zero the gradients
            self._optimizer.zero_grad()
            # step 2. compute the output
            y_pred = self._classifier(x_data=x_data)
            y_pred = y_pred.squeeze()
            # step 3. compute the loss
            loss = loss_func(y_pred, target_y)
            loss_t = loss.item()
            running_loss += (loss_t - running_loss) / (batch_index + 1)
            # step 4. use loss to produce gradients
            loss.backward()
            # step 5. use optimizer to take gradient step
            self._optimizer.step()
            # -----------------------------------------
            # compute the accuracy
            acc_t = compute_accuracy_multi(y_pred, target_y.to(device))
            running_acc += (acc_t - running_acc) / (batch_index + 1)

            # update bar
            train_bar.set_postfix(loss=running_loss,
                                  acc=running_acc,
                                  epoch=epoch)
            train_bar.update()

        return running_loss, running_acc
Esempio n. 4
0
    def test_one_epoch(self):
        # Iterate over validate dataset
        self._dataset.set_split('test')
        self._test_ds = DataLoader(dataset=self._dataset,
                                   batch_size=self._args.batch_size,
                                   shuffle=True,
                                   drop_last=True)
        # loss
        loss_func = nn.CrossEntropyLoss()
        # test bar
        test_bar = tqdm(desc='split=test',
                        total=len(self._test_ds),
                        position=0)

        running_loss = 0.
        running_acc = 0.
        self._classifier.eval()
        test_bar.reset()
        device = 'cuda:0' if self._args.cuda else 'cpu'

        for batch_index, batch_data in enumerate(self._test_ds):
            # data
            x_data, x_source_len, target_y = batch_data
            x_data, x_source_len, target_y = self.sort_by_len(
                x_data, x_source_len, target_y)

            # compute the output
            y_pred = self._classifier(x_data=x_data.to(device),
                                      x_len=x_source_len.to(device))
            y_pred = y_pred.squeeze()
            # compute the loss
            loss = loss_func(y_pred, target_y.to(device))
            loss_t = loss.item()
            running_loss += (loss_t - running_loss) / (batch_index + 1)
            # compute the accuracy
            acc_t = compute_accuracy_multi(y_pred, target_y.to(device))
            running_acc += (acc_t - running_acc) / (batch_index + 1)

            test_bar.set_postfix(loss=running_loss, acc=running_acc)
            test_bar.update()
            #test_bar.refresh()  # something may not update last position

        return running_loss, running_acc