Exemple #1
0
    def train_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        # TODO:
        #  Train the RNN model on one batch of data.
        #  - Forward pass
        #  - Calculate total loss over sequence
        #  - Backward pass: truncated back-propagation through time
        #  - Update params
        #  - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        # Forward pass
        h = self.hidden

        y_pred, h = self.model(x, h)
        # Backward pass
        self.optimizer.zero_grad()
        loss = self.loss_fn(torch.transpose(y_pred, 1, 2), y)

        loss.backward()
        # Weight updates
        self.optimizer.step()
        self.hidden = h.detach()
        # Calculate accuracy
        y_pred = torch.argmax(y_pred, dim=-1)
        num_correct = torch.sum(y_pred == y)
        # ========================

        # Note: scaling num_correct by seq_len because each sample has seq_len
        # different predictions.
        return BatchResult(loss.item(), num_correct.item() / seq_len)
    def test_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        with torch.no_grad():
            # TODO:
            #  Evaluate the RNN model on one batch of data.
            #  - Forward pass
            #  - Loss calculation
            #  - Calculate number of correct predictions
            # ====== YOUR CODE: ======
            self.model.to(self.device)

            y_pred, self.hidden = self.model(x, self.hidden)
            y_pred_fixed = torch.transpose(y_pred, 1, 2)
            loss = self.loss_fn(y_pred_fixed, y)

            # Prevent gradient vanishing or exploding
            self.hidden.detach_()
            self.hidden.requires_grad = False

            num_correct = 0
            for i in range(y_pred_fixed.shape[2]):
                num_correct = (num_correct + 1) if (torch.argmax(
                    y_pred_fixed[0, :, i]) == y[0, i]) else num_correct
            # ========================

        return BatchResult(loss.item(), num_correct / seq_len)
Exemple #3
0
    def train_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        # TODO:
        #  Train the RNN model on one batch of data.
        #  - Forward pass
        #  - Calculate total loss over sequence
        #  - Backward pass: truncated back-propagation through time
        #  - Update params
        #  - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        # raise NotImplementedError()
        # the regular order of train

        y_pred, self.hidden = self.model(x, self.hidden)
        #truncated back-propagation through time
        self.hidden.detach_()
        y_pred = y_pred.transpose(1, 2)
        loss = self.loss_fn(y_pred, y)
        self.optimizer.zero_grad()

        loss.backward()
        self.optimizer.step()

        #calculating what we need
        y_pred = y_pred.argmax(dim=1)  #[B, in_dim, S]-> [B, S]
        num_correct = torch.sum((y == y_pred))
        # ========================

        # Note: scaling num_correct by seq_len because each sample has seq_len
        # different predictions.
        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #4
0
    def test_batch(
        self,
        batch,
        **kw,
    ) -> BatchResult:
        if 'train_time_testing' in list(kw):
            x, y, idx = batch
        else:
            x, y, = batch
        x = x.to(
            self.device,
            dtype=torch.float,
        )
        y = y.to(
            self.device,
            dtype=torch.float,
        )

        with torch.no_grad():
            if self.backbone is not None:
                x = self.backbone(x)
            y_hat = self.model(x, )
            if 'train_time_testing' in list(kw):
                self.model.test_labels[idx] = torch.argmax(
                    y_hat.clone().detach().cpu(), -1).float()
            loss = self.loss_fn(
                y_hat,
                y,
            )
            y_pred = torch.argmax(y_hat, dim=-1)
            num_correct = torch.sum(y == y_pred)

        return BatchResult(loss.item(), num_correct.item() / len(y))
Exemple #5
0
    def test_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        with torch.no_grad():
            # TODO:
            #  Evaluate the RNN model on one batch of data.
            #  - Forward pass
            #  - Loss calculation
            #  - Calculate number of correct predictions
            # ====== YOUR CODE: ======
            #raise NotImplementedError()
            # the regular order of train
            y_pred, self.hidden = self.model(x, self.hidden)
            y_pred = y_pred.transpose(1, 2)
            loss = self.loss_fn(y_pred, y)

            #calculating what we need
            y_pred = y_pred.argmax(dim=1)  #[B, in_dim, S]-> [B, S]
            num_correct = torch.sum((y == y_pred))
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #6
0
    def train_batch(self, batch) -> BatchResult:
        X, y = batch
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        # TODO: Train the PyTorch model on one batch of data.
        #  - Forward pass
        #  - Backward pass
        #  - Optimize params
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        y_pred = self.model(X)
        loss = self.loss_fn(y_pred, y)

        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        _, y_pred_indices = torch.max(y_pred, dim=1)
        num_incorrect = len(torch.nonzero(y_pred_indices - y))
        num_correct = X.shape[0] - num_incorrect
        # ========================

        return BatchResult(loss, num_correct)
Exemple #7
0
    def test_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        with torch.no_grad():
            # TODO:
            #  Evaluate the RNN model on one batch of data.
            #  - Forward pass
            #  - Loss calculation
            #  - Calculate number of correct predictions
            # ====== YOUR CODE: ======
            h = self.hidden
            # forward pass
            y_pred, h = self.model(x, h)
            # loss function
            loss = self.loss_fn(torch.transpose(y_pred, 1, 2), y)

            # Calculate accuracy
            y_pred = torch.argmax(y_pred, dim=-1)
            num_correct = torch.sum(y_pred == y)

            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #8
0
    def train_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        # TODO:
        #  Train the RNN model on one batch of data.
        #  - Forward pass
        #  - Calculate total loss over sequence
        #  - Backward pass: truncated back-propagation through time
        #  - Update params
        #  - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        self.optimizer.zero_grad()

        self.h.detach_()
        y_output, self.h = self.model(x, self.h)

        loss = self.loss_fn(y_output.transpose(1, 2), y)
        loss.backward()
        self.optimizer.step()

        num_correct = (y == torch.argmax(y_output, 2)).sum()
        # raise NotImplementedError()
        # ========================

        # Note: scaling num_correct by seq_len because each sample has seq_len
        # different predictions.
        return BatchResult(loss.item(), num_correct.item() / seq_len)
    def test_batch(self,
                   batch,
                   batch_idx=None,
                   epoch_num=None,
                   dl=None) -> BatchResult:
        dimension = batch.shape[1] - 1
        # We train the autoencoder in an unsupervised fashion. Therefore, there are no labels.
        if self.autoencoder_training:
            X = batch
            y = X
        else:
            y = batch[:, dimension].unsqueeze(-1).long()
            X = batch[:, list(range(dimension))]
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        with torch.no_grad():
            if self.autoencoder_training:
                enc = self.model.encoder(X)
                output = self.model.decoder(enc)
                loss1 = self.loss_fn.forward(output, y)
                loss2 = self.loss_fn2.forward(enc.squeeze(-1),
                                              target(enc.squeeze(-1)))
                loss = loss1 + loss2
                num_correct = 0
            else:
                out = self.model.forward(X)
                loss = self.loss_fn.forward(out, y)
                num_correct = torch.sum(torch.max(out, dim=1)[1] - y == 0)
            # ========================

        return BatchResult(loss, num_correct)
    def test_batch(self, batch) -> BatchResult:
        x, _ = batch
        x = x.to(self.device)  # Image batch (N,C,H,W)

        with torch.no_grad():
            # TODO: Evaluate a VAE on one batch.
            # ====== YOUR CODE: ======
            N = x.shape[0]
            self.optimizer.zero_grad()
            for i in range(N):
                if i == 0:
                    image = x[i, :, :, :].unsqueeze(0)
                    image_rec, mu, log_sigma2 = self.model(image)
                    xr = image_rec
                else:
                    image = x[i, :, :, :].unsqueeze(0)
                    image_rec, curr_mu, curr_log_sigma2 = self.model(image)
                    xr = torch.cat([xr, image_rec], dim=0)
                    mu = torch.cat([mu, curr_mu], dim=0)
                    log_sigma2 = torch.cat([log_sigma2, curr_log_sigma2], dim=0)

            loss, data_loss, _ = self.loss_fn(x, xr, mu, log_sigma2)
            # ========================

        return BatchResult(loss.item(), 1/data_loss.item())
Exemple #11
0
    def train_batch(self, batch) -> BatchResult:
        X, y = batch

        # TODO: Train the Block model on one batch of data.
        #  - Forward pass
        #  - Backward pass
        #  - Optimize params
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        #raise NotImplementedError()
        # ========================
        N = X.shape[0]
        x = X.reshape(N, -1)

        class_scores = self.model(x)
        loss = self.loss_fn(class_scores, y).numpy()
        self.optimizer.zero_grad()

        dout = self.loss_fn.backward()
        self.model.backward(dout)
        self.optimizer.step()

        y_pred = torch.argmax(class_scores, dim=1)
        num_correct = int(torch.sum(y == y_pred).item())
        #num_correct = torch.sum(y == y_pred).numpy()

        return BatchResult(loss, num_correct)
Exemple #12
0
    def train_batch(self, batch) -> BatchResult:
        X, y = batch
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        # TODO: Train the PyTorch model on one batch of data.
        #  - Forward pass
        #  - Backward pass
        #  - Optimize params
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        # Forward pass
        out = self.model(X)

        # Compute loss
        loss = self.loss_fn(out, y)

        # Backward pass
        self.optimizer.zero_grad()
        loss.backward()

        # Optimization step
        self.optimizer.step()

        loss = loss.item()
        num_correct = (torch.argmax(out, dim=1) == y).sum().item()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #13
0
    def train_batch(self, batch) -> BatchResult:
        x, _ = batch
        x = x.to(self.device)  # Image batch (N,C,H,W)
        # TODO: Train a VAE on one batch.
        # ====== YOUR CODE: ======
        y, mu, log_sigma2 = self.model(x)
        loss, data_loss, kldiv_loss = self.loss_fn(x, y, mu, log_sigma2)
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        y, mu, log_sigma2 = self.model(x)
        loss, data_loss, kldiv_loss = self.loss_fn(x, y, mu, log_sigma2)
        # if self.hidden_state is None:
        #     out, hidden_state = self.model(x)
        # else:
        #     out, hidden_state = self.model(x, self.hidden_state.detach())
        # loss = self.loss_fn(out.reshape(batch_size*seq_len, out.shape[-1]), y.reshape(batch_size*seq_len))
        # self.optimizer.zero_grad()
        # loss.backward()
        # self.optimizer.step()
        # if self.hidden_state is None:
        #     out, _ = self.model(x)
        # else:
        #     out, _ = self.model(x, self.hidden_state.detach())
        # self.hidden_state = hidden_state
        # scores, indices = torch.max(out.reshape(batch_size*seq_len, out.shape[-1]), dim=len(out.shape)-2)
        # num_correct = (indices.reshape(y.shape) == y).sum()
        # ========================

        return BatchResult(loss.item(), 1 / data_loss.item())
    def train_batch(self, batch) -> BatchResult:
        X, y = batch
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        # TODO: Train the PyTorch model on one batch of data.
        #  - Forward pass
        #  - Backward pass
        #  - Optimize params
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        y_pred = self.model.forward(X)
        y_loss = self.loss_fn.forward(y_pred, y)
        self.optimizer.zero_grad()
        torch.autograd.backward(y_loss)
        _, pred_classes = torch.max(y_pred, dim=1)
        correct = torch.sum(pred_classes == y)
        self.optimizer.step()

        loss = y_loss.item()
        num_correct = correct.item()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #15
0
    def test_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]
        batch_size = y.shape[0]

        with torch.no_grad():
            # TODO:
            #  Evaluate the RNN model on one batch of data.
            #  - Forward pass
            #  - Loss calculation
            #  - Calculate number of correct predictions
            # ====== YOUR CODE: ======
            out, self.hidden_state = self.model(x, self.hidden_state)
            loss = self.loss_fn(
                out.reshape(batch_size * seq_len, out.shape[-1]),
                y.reshape(batch_size * seq_len))
            scores, indices = torch.max(out.reshape(batch_size * seq_len,
                                                    out.shape[-1]),
                                        dim=len(out.shape) - 2)
            num_correct = (indices.reshape(y.shape) == y).sum()
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #16
0
    def train_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]
        batch_size = y.shape[0]

        # TODO:
        #  Train the RNN model on one batch of data.
        #  - Forward pass
        #  - Calculate total loss over sequence
        #  - Backward pass: truncated back-propagation through time
        #  - Update params
        #  - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        # print("first")
        # if self.hidden_state is None:
        #     out, hidden_state = self.model(x)
        # else:
        #     out, hidden_state = self.model(x, self.hidden_state.detach())
        #     self.hidden_state = hidden_state
        # loss = self.loss_fn(out.squeeze(0), y.squeeze(0))
        #
        # self.optimizer.zero_grad()
        # # if is_first:
        # #     loss.backward(retain_graph=True)
        # # else:
        # loss.backward()
        # self.optimizer.step()
        #
        # out, _ = self.model(x, hidden_state)
        # score, indices = torch.max(out.squeeze(0), dim=1)
        # num_correct = (indices == y).sum()
        # if num_correct.item() / seq_len > 0.9:
        #     print("indices:", indices)
        #     print("y:", indices)
        # print("last")
        if self.hidden_state is None:
            out, hidden_state = self.model(x)
        else:
            out, hidden_state = self.model(x, self.hidden_state.detach())
        loss = self.loss_fn(out.reshape(batch_size * seq_len, out.shape[-1]),
                            y.reshape(batch_size * seq_len))
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()
        if self.hidden_state is None:
            out, _ = self.model(x)
        else:
            out, _ = self.model(x, self.hidden_state.detach())
        self.hidden_state = hidden_state
        scores, indices = torch.max(out.reshape(batch_size * seq_len,
                                                out.shape[-1]),
                                    dim=len(out.shape) - 2)
        num_correct = (indices.reshape(y.shape) == y).sum()
        # ========================

        # Note: scaling num_correct by seq_len because each sample has seq_len
        # different predictions.
        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #17
0
    def train_batch(self, batch) -> BatchResult:
        X, y = batch
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        # TODO: Train the PyTorch model on one batch of data.
        #  - Forward pass
        #  - Backward pass
        #  - Optimize params
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        #raise NotImplementedError()
        self.optimizer.zero_grad()
        res = self.model(X)
        loss = self.loss_fn(res, y)
        #         if self.device:
        #             res = res.to(self.device)
        #             loss = loss.to(self.device)
        loss.backward()
        self.optimizer.step()
        res = res.argmax(1)
        num_correct = (res == y).sum()
        # ========================

        return BatchResult(loss, num_correct)
    def test_batch(self, batch) -> BatchResult:
        x, y = batch
        x = x.to(self.device, dtype=torch.float)  # (B,S,V)
        y = y.to(self.device, dtype=torch.long)  # (B,S)
        seq_len = y.shape[1]

        with torch.no_grad():
            # TODO:
            #  Evaluate the RNN model on one batch of data.
            #  - Forward pass
            #  - Loss calculation
            #  - Calculate number of correct predictions
            # ====== YOUR CODE: ======
            output, hidden = self.model.forward(x, self.hidden)
            self.hidden = hidden
            num_correct = 0
            for i in range(seq_len):
                y_sequence = y[:, i]
                scores_sequence = output[:, i, :].to(self.device, dtype=torch.float)
                if i == 0:
                    loss = self.loss_fn(scores_sequence, y_sequence)
                else:
                    loss += self.loss_fn(scores_sequence, y_sequence)

                pred = scores_sequence.argmax(dim=1)
                num_correct += (y_sequence == pred).sum()
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #19
0
    def train_batch(self, batch) -> BatchResult:
        x, _ = batch
        x = x.to(self.device)  # Image batch (N,C,H,W)
        # TODO: Train a VAE on one batch.
        # ====== YOUR CODE: ======
        raise NotImplementedError()
        # ========================

        return BatchResult(loss.item(), 1 / data_loss.item())
Exemple #20
0
    def test_batch(self, batch) -> BatchResult:
        x, _ = batch
        x = x.to(self.device)  # Image batch (N,C,H,W)

        with torch.no_grad():
            # TODO: Evaluate a VAE on one batch.
            # ====== YOUR CODE: ======
            raise NotImplementedError()
            # ========================

        return BatchResult(loss.item(), 1 / data_loss.item())
Exemple #21
0
    def test_batch(self, batch) -> BatchResult:
        X, y = batch

        # TODO: Evaluate the Block model on one batch of data.
        #  - Forward pass
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        raise NotImplementedError()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #22
0
    def test_batch(self, batch) -> BatchResult:
        x, _ = batch
        x = x.to(self.device)  # Image batch (N,C,H,W)

        with torch.no_grad():
            # TODO: Evaluate a VAE on one batch.
            # ====== YOUR CODE: ======
            y, mu, log_sigma2 = self.model(x)
            loss, data_loss, kldiv_loss = self.loss_fn(x, y, mu, log_sigma2)
            # ========================

        return BatchResult(loss.item(), 1 / data_loss.item())
    def test_batch(self, batch) -> BatchResult:
        X, y = batch

        # TODO: Evaluate the Layer model on one batch of data.
        # ====== YOUR CODE: ======
        X = X.view(X.shape[0], -1)
        y_pred = self.model(X)
        loss = self.loss_fn(y_pred, y)
        y_pred = y_pred.argmax(dim=1)
        num_correct = int((y == y_pred).sum())
        # ========================

        return BatchResult(loss, num_correct)
Exemple #24
0
    def test_batch(self, batch) -> BatchResult:
        x, _ = batch
        x = x.to(self.device)  # Image batch (N,C,H,W)

        with torch.no_grad():
            # TODO: Evaluate a VAE on one batch.
            # ====== YOUR CODE: ======
            xr, z_mu, z_log_sigma2 = self.model(x)
            loss, data_loss, _ = self.loss_fn(x, xr, z_mu, z_log_sigma2)
            #raise NotImplementedError()
            # ========================

        return BatchResult(loss.item(), 1 / data_loss.item())
Exemple #25
0
    def test_batch(self, batch) -> BatchResult:
        X, y = batch

        # TODO: Evaluate the Block model on one batch of data.
        #  - Forward pass
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        out = self.model.forward(X)

        loss = self.loss_fn(out, y).item()
        num_correct = (torch.argmax(out, dim=1) == y).sum().item()
        # ========================

        return BatchResult(loss, num_correct)
    def test_batch(self, batch) -> BatchResult:
        X, y = batch

        # TODO: Evaluate the Block model on one batch of data.
        #  - Forward pass
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        y_prediction = self.model(X)
        loss = self.loss_fn(y_prediction, y)
        num_correct = torch.sum(
            y.eq(torch.argmax(y_prediction, dim=1)).float()).item()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #27
0
    def test_batch(self, batch) -> BatchResult:
        X, y = batch

        # TODO: Evaluate the Block model on one batch of data.
        #  - Forward pass
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        x_scores = self.model(X)
        loss = self.loss_fn(x_scores, y).item()
        y_hat = x_scores.argmax(dim=1)
        num_correct = int(sum(1 * (y_hat == y)))
        # ========================

        return BatchResult(loss, num_correct)
    def test_batch(self, batch) -> BatchResult:
        X, y = batch
        # TODO: Evaluate the Block model on one batch of data.
        #  - Forward pass
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        self.optimizer.zero_grad()
        class_scores = self.model.forward(X)
        loss = self.loss_fn.forward(class_scores, y=y)
        pred = class_scores.argmax(dim=1)
        num_correct = (y == pred).sum()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #29
0
    def test_batch(self, batch) -> BatchResult:
        X, y = batch
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        with torch.no_grad():
            # TODO: Evaluate the PyTorch model on one batch of data.
            #  - Forward pass
            #  - Calculate number of correct predictions
            # ====== YOUR CODE: ======
            raise NotImplementedError()
            # ========================

        return BatchResult(loss, num_correct)
Exemple #30
0
    def train_batch(self, batch) -> BatchResult:
        X, y = batch
        if self.device:
            X = X.to(self.device)
            y = y.to(self.device)

        # TODO: Train the PyTorch model on one batch of data.
        #  - Forward pass
        #  - Backward pass
        #  - Optimize params
        #  - Calculate number of correct predictions
        # ====== YOUR CODE: ======
        raise NotImplementedError()
        # ========================

        return BatchResult(loss, num_correct)