def test(net: nn.Module, loss_fn: loss, x_test: np.array, y_test: np.array) -> Tuple[float, np.array]: """ Run the model on x_test and calculate the loss of the predictions. The model run on evaluation mode and without updating the computational graph (no_grad) """ net.eval() with torch.no_grad(): y_test_pred = net(x_test.float()) loss = loss_fn(input=y_test_pred.reshape(-1), target=y_test.float()) test_loss = loss.item() return test_loss, y_test_pred
def np2tensor(x: np.array) -> torch.Tensor: x = np.transpose(x, (2, 0, 1)) x = torch.from_numpy(x) with torch.no_grad(): while x.dim() < 4: x.unsqueeze_(0) x = x.float() / 255 return x
def forward(self, x: np.array): x = self.fc(x.float()) x = self.sigmoid(x) return x
def __init__(self, features: np.array, labels: np.array): assert features.shape[0] == labels.shape[0] self.features = features.float() self.labels = torch.Tensor(labels).long()