def test_sgd_sequential(): # tests that requesting train_iteration_mode = 'sequential' # works dim = 1 batch_size = 3 m = 5 * batch_size dataset = ArangeDataset(m) model = SoftmaxModel(dim) learning_rate = 1e-3 batch_size = 5 visited = [False] * m def visit(X): assert X.shape[1] == 1 assert np.all(X[1:] == X[0:-1] + 1) start = int(X[0, 0]) if start > 0: assert visited[start - 1] for i in xrange(batch_size): assert not visited[start + i] visited[start + i] = 1 data_specs = (model.get_input_space(), model.get_input_source()) cost = CallbackCost(visit, data_specs) # We need to include this so the test actually stops running at some point termination_criterion = EpochCounter(5) algorithm = SGD(learning_rate, cost, batch_size=5, train_iteration_mode='sequential', monitoring_dataset=None, termination_criterion=termination_criterion, update_callbacks=None, init_momentum=None, set_batch_size=False) algorithm.setup(dataset=dataset, model=model) algorithm.train(dataset) assert all(visited)
def test_determinism(): # Verifies that running SGD twice results in the same examples getting visited # in the same order for mode in _iteration_schemes: dim = 1 batch_size = 3 num_batches = 5 m = num_batches * batch_size dataset = ArangeDataset(m) model = SoftmaxModel(dim) learning_rate = 1e-3 batch_size = 5 visited = [[-1] * m] def visit(X): mx = max(visited[0]) counter = mx + 1 for i in X[:, 0]: i = int(i) assert visited[0][i] == -1 visited[0][i] = counter counter += 1 data_specs = (model.get_input_space(), model.get_input_source()) cost = CallbackCost(visit, data_specs) # We need to include this so the test actually stops running at some point termination_criterion = EpochCounter(5) def run_algorithm(): unsupported_modes = ['random_slice', 'random_uniform'] algorithm = SGD(learning_rate, cost, batch_size=5, train_iteration_mode=mode, monitoring_dataset=None, termination_criterion=termination_criterion, update_callbacks=None, init_momentum=None, set_batch_size=False) algorithm.setup(dataset=dataset, model=model) raised = False try: algorithm.train(dataset) except ValueError: print mode assert mode in unsupported_modes raised = True if mode in unsupported_modes: assert raised return True return False if run_algorithm(): continue visited.insert(0, [-1] * m) del model.monitor run_algorithm() for v in visited: assert len(v) == m for elem in range(m): assert elem in v assert len(visited) == 2 print visited[0] print visited[1] assert np.all(np.asarray(visited[0]) == np.asarray(visited[1]))