def test_som_reduces_distances(): # SOM functions correctly if is moves neurons towards inputs input_matrix, target_matrix = datasets.get_xor() # Small initial weight range chosen so network isn't "accidentally" # very close to inputs initially (which could cause test to fail) som_ = som.SOM(2, 4, initial_weights_range=0.25) # Convenience function def min_distances(): all_closest = [] for inp_vec in input_matrix: distances = som_.activate(inp_vec) all_closest.append(min(distances)) return all_closest # Train SOM # Assert that distances have decreased all_closest = min_distances() som_.train(input_matrix, target_matrix, iterations=20) new_closest = min_distances() print all_closest print new_closest for old_c, new_c in zip(all_closest, new_closest): assert new_c < old_c
def test_mlp_convergence(): # Run until convergence # assert that network can converge model = mlp.MLP((2, 4, 2)) dataset = datasets.get_xor() model.train(*dataset, retries=5, error_break=0.002) assert validation.get_error(model, *dataset) <= 0.02
def test_rbf_convergence(): # Run until convergence # assert that network can converge model = rbf.RBF(2, 4, 2, scale_by_similarity=True) dataset = datasets.get_xor() model.train(*dataset, retries=5, error_break=0.002) assert validation.get_error(model, *dataset) <= 0.02
def test_pbnn_convergence(): # Run until convergence # assert that network can converge model = PBNN() dataset = datasets.get_xor() model.train(*dataset) assert validation.get_error(model, *dataset) <= 0.02
def test_mlp(): # Run for a couple of iterations # assert that new error is less than original model = mlp.MLP((2, 2, 2)) dataset = datasets.get_xor() error = validation.get_error(model, *dataset) model.train(*dataset, iterations=10) assert validation.get_error(model, *dataset) < error
def test_rbf(): # Run for a couple of iterations # assert that new error is less than original model = rbf.RBF(2, 4, 2, scale_by_similarity=True) dataset = datasets.get_xor() error = validation.get_error(model, *dataset) model.train(*dataset, iterations=10) assert validation.get_error(model, *dataset) < error
def test_mlp_classifier(): # Run for a couple of iterations # assert that new error is less than original model = mlp.MLP( (2, 2, 2), transfers=SoftmaxTransfer(), error_func=CrossEntropyError()) dataset = datasets.get_xor() error = validation.get_error(model, *dataset) model.train(*dataset, iterations=20) assert validation.get_error(model, *dataset) < error
def test_Model_custom_converged(): class ConvergeModel(helpers.SetOutputModel): def train_step(self, *args, **kwargs): self.converged = True dataset = datasets.get_xor() model = ConvergeModel([1, 0]) model.train(*dataset) assert model.converged assert model.iteration == 1
def test_post_pattern_callback(): dataset = datasets.get_xor() model = helpers.EmptyModel() inp_history = [] tar_history = [] def callback(model, input_vec, target_vec): inp_history.append(input_vec) tar_history.append(target_vec) model.train(*dataset, iterations=1, post_pattern_callback=callback) inp_history = numpy.array(inp_history) tar_history = numpy.array(tar_history) assert (dataset[0] == inp_history).all() assert (dataset[1] == tar_history).all()
def test_select_sample_size_none_few_samples(seed_random): # When number of samples is low, default to all samples input_matrix, target_matrix = datasets.get_xor() new_inp_matrix, new_tar_matrix = base.select_sample( input_matrix, target_matrix) assert new_inp_matrix.shape == input_matrix.shape assert new_tar_matrix.shape == target_matrix.shape for inp_vec in input_matrix: # all in assert inp_vec in new_inp_matrix for tar_vec in target_matrix: # all in assert tar_vec in new_tar_matrix assert not (new_inp_matrix == input_matrix).all() # Different order assert not (new_tar_matrix == target_matrix).all() # Different order
def test_select_random_size_none_few_samples(monkeypatch): # When number of samples is low, default to all samples # Monkeypatch so we know that random returns # randint always returns 0 monkeypatch.setattr(random, 'randint', lambda x, y: 0) input_matrix, target_matrix = datasets.get_xor() new_inp_matrix, new_tar_matrix = base.select_random( input_matrix, target_matrix) assert new_inp_matrix.shape == input_matrix.shape assert new_tar_matrix.shape == target_matrix.shape for inp_vec in new_inp_matrix: assert (inp_vec == input_matrix[0]).all() # Due to monkeypatch for tar_vec in new_tar_matrix: assert (tar_vec == target_matrix[0]).all() # Due to monkeypatch
def test_select_random(monkeypatch): # Monkeypatch so we know that random returns # randint always returns 0 monkeypatch.setattr(random, 'randint', lambda x, y: 0) input_matrix, target_matrix = datasets.get_xor() # Test size param new_inp_matrix, new_tar_matrix = base.select_random(input_matrix, target_matrix, size=2) assert new_inp_matrix.shape[0] == 2 assert new_tar_matrix.shape[0] == 2 for inp_vec in new_inp_matrix: assert (inp_vec == input_matrix[0]).all() # Due to monkeypatch for tar_vec in new_tar_matrix: assert (tar_vec == target_matrix[0]).all() # Due to monkeypatch
def test_select_sample(seed_random): input_matrix, target_matrix = datasets.get_xor() # Test size param new_inp_matrix, new_tar_matrix = base.select_sample(input_matrix, target_matrix, size=2) assert new_inp_matrix.shape[0] == 2 assert new_tar_matrix.shape[0] == 2 # No duplicates count = 0 for inp_vec in new_inp_matrix: if inp_vec in input_matrix: count += 1 assert count == 2 count = 0 for tar_vec in new_tar_matrix: if tar_vec in target_matrix: count += 1 assert count == 2
def test_som_reduces_distances_matrix(): # SOM functions correctly if is moves neurons towards inputs input_matrix, target_matrix = datasets.get_xor() # Small initial weight range chosen so network isn't "accidentally" # very close to inputs initially (which could cause test to fail) num_neurons = random.randint(2, 10) som_ = som.SOM(2, num_neurons, initial_weights_range=0.25) # Convenience function def min_distances(): distance_matrix = som_.activate(input_matrix) assert distance_matrix.shape == (len(input_matrix), num_neurons) return numpy.min(distance_matrix, axis=-1) # Train SOM # Assert that distances have decreased all_closest = min_distances() som_.train(input_matrix, target_matrix, iterations=20) new_closest = min_distances() print all_closest print new_closest for old_c, new_c in zip(all_closest, new_closest): assert new_c < old_c