def test_assign_tuple_keys(): a = Duck() a['training'] = Duck() a['testing', next]=4 b = a.arrayify_axis(axis=1, subkeys='testing') b = b.arrayify_axis(axis=1, subkeys='training', inplace=True).to_struct() a['training', next, ...] = OrderedDict([('ops', OrderedDict([(('a', 'b'), 1), (('a', 'c'), 2)]))]) a['training', next, ...] = OrderedDict([('ops', OrderedDict([(('a', 'b'), 3), (('a', 'c'), 4)]))]) a.arrayify_axis(axis=1, subkeys='training').to_struct() a['training', next, ...] = OrderedDict([('ops', OrderedDict([(('a', 'b'), 1), (('a', 'c'), 2)]))]) pass
def test_next_elipsis_assignment(): a = Duck() a['a', next, ...] = OrderedDict([('b', [1, 2])]) a['a', next, ...] = OrderedDict([('b', [3, 4])]) # Should lead to form: # 'a' # 0 # 'b' # 0: 1 # 1: 2 # 1 # 'b' # 0: 3 # 1: 4 # If we arrayify axis, 1, we expect struct: # Should lead to form: # 'a' # 'b' # 0: [1, 3] # 1: [2, 4] assert np.array_equal( a.arrayify_axis(axis=1, subkeys='a')['a', 'b'], [[1, 3], [2, 4]]) a = Duck() a['a', next, ...] = OrderedDict([('b', np.array([1, 2]))]) a['a', next, ...] = OrderedDict([('b', np.array([3, 4]))]) # Now, a is no longer broken into ... So we have to be careful with these elipses! # 'a' # 0 # 'b': [1, 2] # 1 # 'b': [3, 4] # If we arrayify axis, 1, we expect struct: # Should lead to form: # 'a' # 'b' # 0: [1, 2] # 1: [3, 4] assert np.array_equal( a.arrayify_axis(axis=1, subkeys='a')['a', 'b'], [[1, 2], [3, 4]])
def test_assign_tuple_keys(): a = Duck() a['training'] = Duck() a['testing', next] = 4 b = a.arrayify_axis(axis=1, subkeys='testing') b = b.arrayify_axis(axis=1, subkeys='training', inplace=True).to_struct() a['training', next, ...] = OrderedDict([('ops', OrderedDict([(('a', 'b'), 1), (('a', 'c'), 2)]))]) a['training', next, ...] = OrderedDict([('ops', OrderedDict([(('a', 'b'), 3), (('a', 'c'), 4)]))]) a.arrayify_axis(axis=1, subkeys='training').to_struct() a['training', next, ...] = OrderedDict([('ops', OrderedDict([(('a', 'b'), 1), (('a', 'c'), 2)]))]) pass
def test_next_elipsis_assignment(): a = Duck() a['a', next, ...] = OrderedDict([('b', [1, 2])]) a['a', next, ...] = OrderedDict([('b', [3, 4])]) # Should lead to form: # 'a' # 0 # 'b' # 0: 1 # 1: 2 # 1 # 'b' # 0: 3 # 1: 4 # If we arrayify axis, 1, we expect struct: # Should lead to form: # 'a' # 'b' # 0: [1, 3] # 1: [2, 4] assert np.array_equal(a.arrayify_axis(axis=1, subkeys='a')['a', 'b'], [[1, 3], [2, 4]]) a = Duck() a['a', next, ...] = OrderedDict([('b', np.array([1, 2]))]) a['a', next, ...] = OrderedDict([('b', np.array([3, 4]))]) # Now, a is no longer broken into ... So we have to be careful with these elipses! # 'a' # 0 # 'b': [1, 2] # 1 # 'b': [3, 4] # If we arrayify axis, 1, we expect struct: # Should lead to form: # 'a' # 'b' # 0: [1, 2] # 1: [3, 4] assert np.array_equal(a.arrayify_axis(axis=1, subkeys='a')['a', 'b'], [[1, 2], [3, 4]])
def test_arrayify_axis_demo(): a = Duck() a[0, 'x'] = 1 a[0, 'y'] = 2 a[1, 'x'] = 3 a[1, 'y'] = 4 b = a.arrayify_axis(axis=0) assert np.array_equal(b['x'], [1, 3]) assert np.array_equal(b['y'], [2, 4])
def test_slice_on_start(): a = Duck() a['testing'] = Duck() b = a.arrayify_axis(axis=1, subkeys='testing') assert np.array_equal(b['testing'], [])
def train_and_test_predictor( f_train, f_predict, losses, training_data_gen, test_data_gen_constructors, n_training_iters = None, n_test_iters = None, test_checkpoints = ('lin', 1000), collapse_loss = 'mean', progress_update_period = '5s', in_test_callback = None, post_test_callback = None, post_train_callback = None, save_train_return = False, measures = None, iterations_to_end = False ): """ :param f_train: :param f_predict: :param losses: :param training_data_gen: :param test_data_gen_constructors: :param samples_count: :param n_training_iters: :param n_test_iters: :param test_checkpoints: :param collapse_loss: :param progress_update_period: :param in_test_callback: :param post_test_callback: :param post_train_callback: :param save_train_return: :param measures: :return: If yield_array_data is False, an ArrayStruct with fields: 'training' Results recorded during training callbacks training_iter The iteration of the training callback 'testing' Results recorded during tests test_iter The index of the test 'iter' The number of training iterations finished at the time that the test is run 'time' The time at which the test is run 'samples' The number of samples seen so far 'results' A structure containing results subset_name The name of the testing subset e.g. 'train', 'test' (you give subset names in test_data_gen_constructors) 'losses' loss_name The name of the loss function (you provide this in losses) n_tests The numner of tests that were run for this subset 'time' The time, in seconds, that it took to test on this subset. Otherwise, if true, a structure the same object but training_iter and test_iter pushed to the leaf-position """ if measures is None: measures = Duck() if 'training' not in measures: measures[Keys.TRAINING] = Duck() if 'testing' not in measures: measures[Keys.TESTING] = Duck() is_test_time = Checkpoints(test_checkpoints) if not isinstance(test_checkpoints, Checkpoints) else test_checkpoints pi = ProgressIndicator(n_training_iters, "Training", update_every=progress_update_period) for inputs, targets in training_data_gen: if is_test_time(): this_test_measures = measures[Keys.TESTING].open(next) this_test_measures[Keys.ITER] = pi.get_iterations() this_test_measures[Keys.TIME] = pi.get_elapsed() this_test_measures[Keys.RESULTS] = do_test( test_subset_generators={subset_name: constructor() for subset_name, constructor in test_data_gen_constructors.items()}, f_predict=f_predict, loss_dict=losses, n_test_iters=n_test_iters, collapse_loss = collapse_loss, in_test_callback = in_test_callback, ) if post_test_callback is not None: return_val = post_test_callback(this_test_measures) if return_val is not None: this_test_measures[Keys.CALLBACK, ...] = return_val if iterations_to_end: measures_to_yield = measures.arrayify_axis(axis=1, subkeys=Keys.TRAINING) measures_to_yield = measures_to_yield.arrayify_axis(axis=1, subkeys=Keys.TESTING, inplace=True) yield measures_to_yield.to_struct() else: yield measures train_return = f_train(inputs, targets) pi.print_update() if save_train_return: measures[Keys.TRAINING, Keys.RETURNS] = train_return if post_train_callback: return_val = post_train_callback(inputs=inputs, targets=targets, iter=pi.get_iterations()) if return_val is not None: measures[Keys.TRAINING, Keys.CALLBACK, next, ...] = return_val
def train_and_test_predictor(f_train, f_predict, losses, training_data_gen, test_data_gen_constructors, n_training_iters=None, n_test_iters=None, test_checkpoints=('lin', 1000), collapse_loss='mean', progress_update_period='5s', in_test_callback=None, post_test_callback=None, post_train_callback=None, save_train_return=False, measures=None, iterations_to_end=False): """ :param f_train: :param f_predict: :param losses: :param training_data_gen: :param test_data_gen_constructors: :param samples_count: :param n_training_iters: :param n_test_iters: :param test_checkpoints: :param collapse_loss: :param progress_update_period: :param in_test_callback: :param post_test_callback: :param post_train_callback: :param save_train_return: :param measures: :return: If yield_array_data is False, an ArrayStruct with fields: 'training' Results recorded during training callbacks training_iter The iteration of the training callback 'testing' Results recorded during tests test_iter The index of the test 'iter' The number of training iterations finished at the time that the test is run 'time' The time at which the test is run 'samples' The number of samples seen so far 'results' A structure containing results subset_name The name of the testing subset e.g. 'train', 'test' (you give subset names in test_data_gen_constructors) 'losses' loss_name The name of the loss function (you provide this in losses) n_tests The numner of tests that were run for this subset 'time' The time, in seconds, that it took to test on this subset. Otherwise, if true, a structure the same object but training_iter and test_iter pushed to the leaf-position """ if measures is None: measures = Duck() if 'training' not in measures: measures['training'] = Duck() if 'testing' not in measures: measures['testing'] = Duck() is_test_time = Checkpoints(test_checkpoints) if not isinstance( test_checkpoints, Checkpoints) else test_checkpoints pi = ProgressIndicator(n_training_iters, "Training", update_every=progress_update_period) for inputs, targets in training_data_gen: if is_test_time(): this_test_measures = measures['testing'].open(next) this_test_measures['iter'] = pi.get_iterations() this_test_measures['time'] = pi.get_elapsed() this_test_measures['results'] = do_test( test_subset_generators={ subset_name: constructor() for subset_name, constructor in test_data_gen_constructors.items() }, f_predict=f_predict, loss_dict=losses, n_test_iters=n_test_iters, collapse_loss=collapse_loss, in_test_callback=in_test_callback, ) if post_test_callback is not None: post_test_callback(this_test_measures) if iterations_to_end: measures_to_yield = measures.arrayify_axis(axis=1, subkeys='training') measures_to_yield = measures_to_yield.arrayify_axis( axis=1, subkeys='testing', inplace=True) yield measures_to_yield.to_struct() else: yield measures train_return = f_train(inputs, targets) pi.print_update() if save_train_return: measures['training', 'returns'] = train_return if post_train_callback: return_val = post_train_callback(inputs=inputs, targets=targets, iter=pi.get_iterations()) if return_val is not None: measures['training', 'callback', next, ...] = return_val