Exemple #1
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: ======
        N = X.shape[0]
        x = X.reshape(N, -1)
        if self.device:
            x = x.to(self.device)
            y = y.to(self.device)

        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 = torch.sum(y == y_pred).numpy()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #2
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: ======
            y_scores_list, self.hidden_state = self.model(
                x, hidden_state=self.hidden_state)

            # Make it work for CrossEntropy.
            y_scores_list = torch.transpose(y_scores_list, 1, 2)
            loss = self.loss_fn(y_scores_list, y)

            y_predictions = torch.argmax(y_scores_list, dim=1)

            num_correct = torch.sum(y == y_predictions)
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #3
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: ======
        self.optimizer.zero_grad()
        y_pred = self.model(X)
        loss = self.loss_fn(y_pred, y)
        loss.backward()
        self.optimizer.step()

        indices = torch.argmax(y_pred, dim=1)
        num_correct = torch.eq(indices, y).view(-1).sum().item()

        loss = loss.data.tolist()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #4
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 (BPTT)
        # - Update params
        # - Calculate number of correct char predictions
        # ====== YOUR CODE: ======

        self.optimizer.zero_grad()
        pred, hidden_state = self.model(x, self.hidden_state)
        self.hidden_state = hidden_state.detach()
        pred = pred.view((-1, x.shape[-1]))
        y = y.view((-1))
        loss = self.loss_fn(pred, y)
        loss.backward()
        self.optimizer.step()
        preds = torch.argmax(pred, 1)
        num_correct = torch.sum((y == preds))

        # 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)
Exemple #5
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: ======
        self.model.zero_grad()
        res = self.model.forward(X)  # run fw pass
        loss = self.loss_fn(res, y)  # calc loss (also to prep grad)
        loss.backward()  # calc grad through network
        loss = loss.item()
        self.optimizer.step()  # optimize
        #Done training, now report progress
        _, pred = res.max(
            dim=1)  # when specifying dim, gives a tuple with index
        num_correct = torch.eq(y, pred).sum().item()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #6
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 (BPTT)
        # - Update params
        # - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        self.h.detach_()
        self.optimizer.zero_grad()
        y_pred, temp_h = self.model(x, self.h)
        y_pred = y_pred.transpose(1, 2)
        loss = self.loss_fn(y_pred, y)
        loss.backward()
        self.optimizer.step()
        y_max = y_pred.argmax(1)
        num_correct = torch.sum((y_max == y)).float()
        self.h = temp_h
        # ========================

        # 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 #7
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 (BPTT)
        # - Update params
        # - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        y_scores, self.hidden_state = self.model(
            x, hidden_state=self.hidden_state)
        # need to transpose to match the shape the loss function
        # (CrossEntropy in the notebook) expects
        y_scores = torch.transpose(y_scores, 1, 2)
        loss = self.loss_fn(y_scores, y)
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        self.hidden_state = self.hidden_state.detach()
        self.hidden_state.requires_grad = False

        y_pred = torch.argmax(y_scores, dim=1)
        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 #8
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 #9
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 #10
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 #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()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #12
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: ======
        class_scores = self.model(X)
        loss = self.loss_fn(class_scores, y).numpy()
        y_pred = torch.argmax(class_scores, dim=1)
        num_correct = torch.sum(y == y_pred).numpy()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #13
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
        # self.model.train(False)
        logits = self.model(X)
        loss = self.loss_fn(logits, y)
        pred = logits.argmax(dim=1)
        num_correct = (pred == y).sum().item()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #14
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: ======
        res = self.model(X)
        loss = self.loss_fn(res)
        _, pred = res.max(
            dim=1)  # when specifying dim, gives a tuple with index
        num_correct = torch.eq(y, pred).sum()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #15
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 #16
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()
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #17
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: ======
            class_scores = self.model(X)
            loss = self.loss_fn(class_scores, y).numpy()
            y_pred = torch.argmax(class_scores, dim=1)
            num_correct = torch.sum(y == y_pred).numpy()
            # ========================

        return BatchResult(loss, num_correct)
Exemple #18
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: ======
            x_scores = self.model(X)
            loss = self.loss_fn(x_scores, y)
            _, y_predicted = torch.max(x_scores, 1)
            num_correct = (y_predicted == y).sum().item()
            # ========================

        return BatchResult(loss, num_correct)
Exemple #19
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: ======
            res = self.model.forward(X)
            loss = self.loss_fn(res, y).item()
            _, pred = res.max(
                dim=1)  # when specifying dim, gives a tuple with index
            num_correct = torch.eq(y, pred).sum().item()
            # ========================

        return BatchResult(loss, num_correct)
Exemple #20
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 (BPTT)
        # - Update params
        # - Calculate number of correct char predictions
        # ====== YOUR CODE: ======
        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)
Exemple #21
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: ======
            y_pred = self.model(X)
            loss = self.loss_fn(y_pred, y).data.tolist()

            indices = torch.argmax(y_pred, dim=1)
            num_correct = torch.eq(indices, y).view(-1).sum().item()
            # ========================

        return BatchResult(loss, num_correct)
Exemple #22
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: ======
            y_pred, self.h = self.model(x, self.h)
            y_pred = y_pred.transpose(1, 2)
            loss = self.loss_fn(y_pred, y)
            y_max = y_pred.argmax(1)
            num_correct = torch.sum((y_max == y)).float()
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #23
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: ======
        res = self.model(X)  #run fw pass
        loss = self.loss_fn(res, y)  #calc loss (also to prep grad)
        grad = self.loss_fn.backward()  # calc grad through network
        self.model.backward(grad)
        self.optimizer.step()  #optimize
        _, pred = res.max(
            dim=1)  # when specifying dim, gives a tuple with index
        num_correct = torch.eq(y, pred).sum()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #24
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: ======
        self.optimizer.zero_grad()
        x_scores = self.model.forward(X)
        loss = self.loss_fn(x_scores, y)
        loss.backward()
        self.optimizer.step()
        _, y_predicted = torch.max(x_scores, dim=1)
        num_correct = (y_predicted == y).sum().item()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #25
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: ======
        class_scores = self.model(X)
        self.optimizer.zero_grad()
        loss = self.loss_fn(class_scores, y)
        loss.backward()
        self.optimizer.step()
        y_pred = torch.argmax(class_scores, dim=1)
        num_correct = torch.sum(y == y_pred).to('cpu').detach().numpy()
        loss = loss.to('cpu').detach().numpy().tolist()
        # ========================

        return BatchResult(loss, num_correct)
Exemple #26
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: ======

            pred, self.hidden_state = self.model(x, self.hidden_state)
            pred = logits.view((-1, x.shape[-1]))
            y = y.view((-1))
            loss = self.loss_fn(logits, y)
            num_correct = torch.sum(torch.argmax(logits, dim=1) == y)

            # raise NotImplementedError()
            # ========================

        return BatchResult(loss.item(), num_correct.item() / seq_len)
Exemple #27
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
        # self.model.train(True)
        logits = self.model(X)
        self.optimizer.zero_grad()
        loss = self.loss_fn(logits, y)
        # todo check if should be loss.backward
        back_loss = self.loss_fn.backward()
        self.model.backward(back_loss)
        # loss.backward()
        self.optimizer.step()
        pred = logits.argmax(dim=1)

        num_correct = (pred == y).sum().item()

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

        return BatchResult(loss, num_correct)