def _test_epoch_metric(self, pred, true, sklearn_metrics, with_sample_weight, *, kwargs=None, names=None): epoch_metric = SKLearnMetrics(sklearn_metrics, kwargs=kwargs, names=names) if with_sample_weight: loader = self._get_data_loader(pred, (true, self.sample_weight)) sample_weight = self.sample_weight.numpy() else: loader = self._get_data_loader(pred, true) sample_weight = None true = true.numpy() pred = pred.numpy() if not isinstance(sklearn_metrics, list): sklearn_metrics = [sklearn_metrics] if kwargs is not None and not isinstance(kwargs, list): kwargs = [kwargs] kwargs = kwargs if kwargs is not None else repeat({}) if names is not None and not isinstance(names, list): names = [names] names = [func.__name__ for func in sklearn_metrics] if names is None else names expected = { name: f(true, pred, sample_weight=sample_weight, **kw) for name, f, kw in zip(names, sklearn_metrics, kwargs) } with torch.no_grad(): for y_pred, y_true in loader: epoch_metric(y_pred, y_true) actual = epoch_metric.get_metric() self.assertEqual(expected, actual)
test_x = np.random.randn(num_test_samples, num_features).astype('float32') test_y = np.random.randn(num_test_samples, 1).astype('float32') cuda_device = 0 device = torch.device("cuda:%d" % cuda_device if torch.cuda.is_available() else "cpu") # Define the network network = nn.Sequential( nn.Linear(num_features, hidden_state_size), nn.ReLU(), nn.Linear(hidden_state_size, 1), ) # Train model = Model( network, 'sgd', 'mse', batch_metrics=['l1'], epoch_metrics=[SKLearnMetrics(r2_score)], device=device, ) model.fit( train_x, train_y, validation_data=(valid_x, valid_y), epochs=5, batch_size=32, )