Ejemplo n.º 1
0
 def predict(self, inp):
     """
     Capsuled prediction method.
     Only single model usage supported for now.
     """
     inp = torch.Tensor(inp).to(DEVICE)
     return self.model(inp)
Ejemplo n.º 2
0
    def evaluate(self, inp, out):
        """Prediction and error estimation for given input and output"""
        with torch.no_grad():
            # Switch to PyTorch's evaluation mode.
            # Some layers, which are used for regularization, e.g., dropout or batch norm layers,
            # behave differently, i.e., are turnd off, in evaluation mode
            # to prevent influencing the prediction accuracy.
            if isinstance(self.model, (list, np.ndarray)):
                for idx, __ in enumerate(self.model):
                    self.model[idx].eval()
            else:
                self.model.eval()
            pred_out = self.model(torch.Tensor(inp).to(DEVICE))
            pred_out = torch.sigmoid(pred_out)

            # RMSE is the default accuracy metric
            error = torch.sqrt(
                self.loss(pred_out,
                          torch.Tensor(out).to(DEVICE)))
            return pred_out, (error * 100.0)
Ejemplo n.º 3
0
 def evaluate(self, inp, out):
     """Predition and error estimation for given input and output."""
     with torch.no_grad():
         # Switch to PyTorch's evaluation mode.
         # Some layers, which are used for regularization, e.g., dropout or batch norm layers,
         # behave differently, i.e., are turnd off, in evaluation mode
         # to prevent influencing the prediction accuracy.
         self.model.eval()
         pred_out = self.predict(inp)
         # RMSE is the default accuracy metric
         error = torch.sqrt(
             self.loss(pred_out,
                       torch.Tensor(out).to(DEVICE)))
         return pred_out, (error * 100.0)
Ejemplo n.º 4
0
    def learn_from_epoch(self):
        """Training method"""
        epoch_loss = 0
        try:
            inp_batches, out_batches = self.get_batches_fn()
        except AttributeError:
            print("Error: No nb_batches_fn defined in preprocessor. "
                  "This attribute is required by the training routine.")
        for batch_idx in tqdm(range(len(inp_batches))):
            pred_out = self.model(
                torch.Tensor(inp_batches[batch_idx]).to(DEVICE))
            pred_out = torch.sigmoid(pred_out)
            batch_loss = self.loss(
                pred_out,
                torch.Tensor(out_batches[batch_idx]).to(DEVICE))

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

            epoch_loss += batch_loss.item()
        epoch_loss /= len(inp_batches)

        return epoch_loss
Ejemplo n.º 5
0
    def learn_from_epoch(self):
        """Training method."""
        epoch_loss = 0
        nb_scenarios = 0
        inp_batches, out_batches = self.get_batches_fn()

        for batch_idx in tqdm(range(len(inp_batches))):
            pred_out = self.predict(inp_batches[batch_idx])

            batch_loss = self.loss(
                pred_out,
                torch.Tensor(out_batches[batch_idx]).to(DEVICE))

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

            epoch_loss += batch_loss.item()

        epoch_loss /= nb_scenarios
        return epoch_loss