Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
train_final_batch_missing_samples = 7
train_size = train_real_steps_per_epoch * train_batch_size - \
train_final_batch_missing_samples
train_x = torch.rand(train_size, 1)
train_y = torch.rand(train_size, 1)
train_dataset = TensorDataset(train_x, train_y)
train_generator = DataLoader(train_dataset, train_batch_size)
valid_real_steps_per_epoch = 10
valid_batch_size = 15
valid_final_batch_missing_samples = 3
valid_size = valid_real_steps_per_epoch * valid_batch_size - \
valid_final_batch_missing_samples
valid_x = torch.rand(valid_size, 1)
valid_y = torch.rand(valid_size, 1)
valid_dataset = TensorDataset(valid_x, valid_y)
valid_generator = DataLoader(valid_dataset, valid_batch_size)
logs = self.model.fit_generator(train_generator,
valid_generator,
epochs=ModelTest.epochs,
steps_per_epoch=None,
validation_steps=None,
callbacks=[self.mock_callback])
params = {'epochs': ModelTest.epochs, 'steps': train_real_steps_per_epoch}
self._test_fitting(params, logs)
def test_evaluate_data_loader_multi_input(self):
x1 = torch.rand(ModelTest.evaluate_dataset_len, 1)
x2 = torch.rand(ModelTest.evaluate_dataset_len, 1)
y = torch.rand(ModelTest.evaluate_dataset_len, 1)
dataset = TensorDataset((x1, x2), y)
generator = DataLoader(dataset, ModelTest.batch_size)
loss, metrics, pred_y = self.multi_input_model.evaluate_generator(generator, return_pred=True)
self.assertEqual(type(loss), float)
self.assertEqual(type(metrics), np.ndarray)
self.assertEqual(metrics.tolist(), [some_metric_1_value, some_metric_2_value, some_constant_epoch_metric_value])
self._test_predictions_for_evaluate_and_predict_generator(pred_y)
def test_list_of_tensors(self):
range20 = np.expand_dims(np.arange(20), 1)
dataset = TensorDataset((range20, range20 * 2), range20 * 3)
self.assertEqual(len(dataset), 20)
self.assertEqual(type(dataset[0]), tuple)
self.assertEqual(type(dataset[0][0]), tuple)
self.assertEqual(type(dataset[0][-1]), np.ndarray)
for i in range(20):
self.assertEqual(dataset[i][0][0], i)
self.assertEqual(dataset[i][0][1], i * 2)
self.assertEqual(dataset[i][1], i * 3)
dataset = TensorDataset((range20, range20 * 2), (range20 * 3, range20 * 4))
self.assertEqual(len(dataset), 20)
self.assertEqual(type(dataset[0]), tuple)
self.assertEqual(type(dataset[1]), tuple)
self.assertEqual(type(dataset[0][0]), tuple)
self.assertEqual(type(dataset[0][1]), tuple)
def test_evaluate_data_loader_multi_output(self):
x = torch.rand(ModelTest.evaluate_dataset_len, 1)
y1 = torch.rand(ModelTest.evaluate_dataset_len, 1)
y2 = torch.rand(ModelTest.evaluate_dataset_len, 1)
dataset = TensorDataset(x, (y1, y2))
generator = DataLoader(dataset, ModelTest.batch_size)
loss, metrics, pred_y = self.multi_output_model.evaluate_generator(generator, return_pred=True)
self.assertEqual(type(loss), float)
self.assertEqual(type(metrics), np.ndarray)
self.assertEqual(metrics.tolist(), [some_metric_1_value, some_metric_2_value, some_constant_epoch_metric_value])
self._test_predictions_for_evaluate_and_predict_generator(pred_y, multi_output=True)
def test_one_tensor(self):
range20 = np.expand_dims(np.arange(20), 1)
dataset = TensorDataset(range20)
self.assertEqual(len(dataset), 20)
for i in range(20):
self.assertEqual(dataset[i], np.array([i]))
def test_evaluate_data_loader(self):
x = torch.rand(ModelTest.evaluate_dataset_len, 1)
y = torch.rand(ModelTest.evaluate_dataset_len, 1)
dataset = TensorDataset(x, y)
generator = DataLoader(dataset, ModelTest.batch_size)
loss, metrics, pred_y = self.model.evaluate_generator(generator, return_pred=True)
self.assertEqual(type(loss), float)
self.assertEqual(type(metrics), np.ndarray)
self.assertEqual(metrics.tolist(), [some_metric_1_value, some_metric_2_value, some_constant_epoch_metric_value])
self._test_predictions_for_evaluate_and_predict_generator(pred_y)
def test_fitting_with_data_loader(self):
train_real_steps_per_epoch = 30
train_batch_size = ModelTest.batch_size
train_final_batch_missing_samples = 7
train_size = train_real_steps_per_epoch * train_batch_size - \
train_final_batch_missing_samples
train_x = torch.rand(train_size, 1)
train_y = torch.rand(train_size, 1)
train_dataset = TensorDataset(train_x, train_y)
train_generator = DataLoader(train_dataset, train_batch_size)
valid_real_steps_per_epoch = 10
valid_batch_size = 15
valid_final_batch_missing_samples = 3
valid_size = valid_real_steps_per_epoch * valid_batch_size - \
valid_final_batch_missing_samples
valid_x = torch.rand(valid_size, 1)
valid_y = torch.rand(valid_size, 1)
valid_dataset = TensorDataset(valid_x, valid_y)
valid_generator = DataLoader(valid_dataset, valid_batch_size)
logs = self.model.fit_generator(train_generator,
valid_generator,
epochs=ModelTest.epochs,
steps_per_epoch=None,
def _dataloader_from_data(self, args, batch_size):
args = numpy_to_torch(args)
dataset = TensorDataset(*args) if len(args) > 1 else args[0]
generator = DataLoader(dataset, batch_size)
return generator