def test_early_stopping(tmpdir): model_params = { 'type': 'toy:DebugToyModel', 'model_dir': str(tmpdir / 'test_early_stopping/'), 'learning_rate': 0.01, 'num_classes': 2, 'num_steps_per_epoch': 1, 'hidden_neurons': 512 } # Initialize the model model = DebugToyModel(**model_params) # Read training data train_feeder = MemorySamplesFeeder( np.asarray(np.arange(0, 10).reshape(10, 1), dtype=np.float32), np.array([int(0 if i < 5 else 1) for i in range(10)], dtype=np.int64, ndmin=1), batch_size=10, feed_as_dict=False) # Read validation data validation_feeder = MemorySamplesFeeder( np.asarray(np.arange(10, 20).reshape(10, 1), dtype=np.float32), np.array([int(0 if i < 5 else 1) for i in range(10)], dtype=np.int64, ndmin=1), batch_size=10, feed_as_dict=False) # Initialize the Trainer trainer = Trainer() train_params = { 'epochs': 12, 'save_summary_steps': 1, 'save_checkpoint_steps': 1, 'early_stopping_metric': 'loss', 'early_stopping_steps_without_decrease': 3, 'early_stopping_min_steps': 1, 'early_stopping_hook_run_every_steps': 1 } # Start training process trainer.train(model=model, train_feeder=train_feeder, eval_feeder=validation_feeder, **train_params) # Grab global step from model.estimator object global_step = model.estimator().get_variable_value('global_step') assert global_step < train_params['epochs']
def test_dict_input_with_2d(self): x = { 'feature1': [[1, 11], [2, 12], [3, 13], [4, 14]] } y = { 'label': [4, 5, 6, 7] } feeder = MemorySamplesFeeder(x, y, shuffle=False, batch_size=2, feed_as_dict=True) input_fn = feeder.get_input_fn(1) results = read_iterator(input_fn().make_one_shot_iterator()) assert len(results) == 2 assert np.all(results[0][0]['feature1'] == np.array([[1, 11], [2, 12]])) assert np.all(results[0][1]['label'] == np.array([4, 5]))
def test_pandas_input_with_dict(self): x = pd.DataFrame(np.arange(0, 20).reshape(-1, 2), columns=['feature_1', 'feature_2']) y = pd.DataFrame(np.arange(100, 110)) feeder = MemorySamplesFeeder(x, y, shuffle=False, batch_size=2, feed_as_dict=True) input_fn = feeder.get_input_fn(1) results = read_iterator(input_fn().make_one_shot_iterator()) assert len(results) == 5 assert np.all(results[0][0]['feature_1'] == np.array([0, 2])) assert np.all(results[0][0]['feature_2'] == np.array([1, 3])) assert np.all(results[0][1][0] == np.array([100, 101]))
def test_repr(self): x = np.arange(0, 100) y = np.arange(1000, 1100) res = repr(MemorySamplesFeeder(x=x, y=y, batch_size=16, shuffle=False)) assert res == "MemorySamplesFeeder(x=(100, 1), y=(100, 1), batch_size=16, " \ "shuffle=False, as_dict=True)"
def test_python_single_list(self): x = list(range(20)) y = list(range(100, 120)) feeder = MemorySamplesFeeder( x=x, y=y, shuffle=False, batch_size=2, feed_as_dict=False) input_fn = feeder.get_input_fn(1) results = read_iterator(input_fn().make_one_shot_iterator()) expected = list(zip(batch_numpy_array(np.arange(20), 2), batch_numpy_array(np.arange(100, 120), 2))) assert equal_list_of_arrays(results, expected)
def test_np_with_batch_2(self): x = np.arange(0, 10) y = np.arange(0, 1).repeat(10) feeder = MemorySamplesFeeder( x=x, y=y, batch_size=2, feed_as_dict=False, shuffle=False ) input_fn = feeder.get_input_fn(1) results = read_iterator(input_fn().make_one_shot_iterator()) assert len(results) == 5 assert equal_list_of_arrays(results, list(zip(batch_numpy_array(x, 2), batch_numpy_array(y, 2)))) assert feeder.total_steps(1) == 5
def test_wrong_x_y_shape(self): x = np.arange(0, 101) y = np.arange(1000, 1100) with pytest.raises(ValueError): MemorySamplesFeeder(x=x, y=y)
def test_np_with_batch_1(self): x = np.arange(0, 10) y = np.arange(0, 1).repeat(10) feeder = MemorySamplesFeeder( x=x, y=y, batch_size=1, feed_as_dict=False, shuffle=False ) input_fn = feeder.get_input_fn(1) results = read_iterator(input_fn().make_one_shot_iterator()) assert len(results) == 10 assert results == list(zip(x, y)) assert feeder.total_steps(1) == 10
def test_wrong_x_y_dict_shape(self): x = { 'f1': np.arange(0, 12), 'f2': np.arange(20, 22), } y = np.arange(1000, 1100) with pytest.raises(ValueError): MemorySamplesFeeder(x=x, y=y)
def test_shuffle_with_epochs(self): x = np.arange(0, 100) y = np.arange(1000, 1100) feeder = MemorySamplesFeeder( x=x, y=y, batch_size=10, shuffle=True, feed_as_dict=False ) tf.set_random_seed(1) input_fn = feeder.get_input_fn(5) results = read_iterator(input_fn().make_one_shot_iterator()) assert len(results) == 50 for (batch_x, batch_y) in results: assert np.all(batch_x == batch_y - 1000) assert feeder.total_steps(5) == 50
def test_piecewise_learning_rate(tmpdir): model_params = { 'type': 'toy:DebugToyModel', 'model_dir': str(tmpdir / 'test_piecewise_learning_rate/'), 'num_batches_per_epoch': 1, 'piecewise_learning_rate_schedule': "0.1:3:0.01:6:0.001", 'piecewise_lr_boundaries': [2, 5], 'piecewise_lr_values': [0.1, 0.01, 0.001], 'num_classes': 2, 'num_steps_per_epoch': 1, 'hidden_neurons': 10 } train_params = { 'epochs': 10, 'save_summary_steps': 1, 'save_checkpoint_steps': 1 } assert model_params['num_batches_per_epoch'] == 1 assert train_params['epochs'] == 10 # Create the ground truth learning rate values ground_trouth_learning_rate_values = [0.1, 0.1, 0.1, 0.01, 0.01, 0.01, 0.001, 0.001, 0.001, 0.001] # Initialize the model model = DebugToyModel(**model_params) # Read training data Be careful for batch_size == Number of examples, because it affects num_steps_per_epoch train_feeder = MemorySamplesFeeder(np.asarray(np.arange(0, 10).reshape(10, 1), dtype=np.float32), np.array([int(0 if i < 5 else 1) for i in range(10)], dtype=np.int64, ndmin=1), batch_size=10, feed_as_dict=False) # Initialize the Trainer trainer = Trainer() # Start training process trainer.train(model=model, train_feeder=train_feeder, eval_feeder=None, **train_params) learning_rate_values = list() # Parse events out tensorboard file events_out_file = os.path.join(model_params['model_dir'], [str(f) for f in os.listdir(model_params['model_dir']) if f.startswith('events.out.tfevents')][0]) for e in tf.train.summary_iterator(events_out_file): for v in e.summary.value: if v.tag == 'learning_rate': learning_rate_values.append(float('{:.7f}' .format(v.simple_value))) # Testing condition assert ground_trouth_learning_rate_values == learning_rate_values
def test_np_with_batch_1_as_dict(self): x = np.arange(0, 10) y = np.arange(0, 1).repeat(10) feeder = MemorySamplesFeeder( x=x, y=y, batch_size=1, feed_as_dict=True, shuffle=False ) input_fn = feeder.get_input_fn(1) results = read_iterator(input_fn().make_one_shot_iterator()) expected = [ ({0: xi}, {0: yi}) for xi, yi in zip(x.reshape(-1, 1), y.reshape(-1, 1)) ] assert len(results) == 10 assert results == expected assert feeder.total_steps(1) == 10
def test_np_with_epochs(self): x = np.arange(0, 100) y = np.arange(1000, 1100) feeder = MemorySamplesFeeder( x=x, y=y, batch_size=10, feed_as_dict=False, shuffle=False ) input_fn = feeder.get_input_fn(5) results = read_iterator(input_fn().make_one_shot_iterator()) assert len(results) == 50 assert equal_list_of_arrays( results, list(zip(batch_numpy_array(x, 10), batch_numpy_array(y, 10)) ) * 5 ) assert feeder.total_steps(5) == 50
def test_total_steps(self): x = np.arange(0, 100) y = np.arange(1000, 1100) feeder = MemorySamplesFeeder( x=x, y=y, batch_size=10, shuffle=True, feed_as_dict=False ) assert feeder.total_steps(1) == 10 assert feeder.total_steps(2) == 20 feeder = MemorySamplesFeeder( x=x, y=y, batch_size=7, shuffle=True, feed_as_dict=False ) assert feeder.total_steps(1) == 15 assert feeder.total_steps(2) == 29 feeder = MemorySamplesFeeder( x=x, y=y, batch_size=10, shuffle=True, feed_as_dict=True ) assert feeder.total_steps(1) == 10 assert feeder.total_steps(2) == 20