def test_y_range(): """Tests whether setting a y range works correctly""" for _ in range(100): val1 = random.random() - 3.0*random.random() val2 = random.random() + 2.0*random.random() lower_bound = min(val1, val2) upper_bound = max(val1, val2) CNN_instance = CNN(layers_info=[["conv", 2, 2, 1, 2], ["adaptivemaxpool", 2, 2], ["linear", 5]], hidden_activations="relu", y_range=(lower_bound, upper_bound), initialiser="xavier", input_dim=(1, 20, 20)) random_data = torch.randn((10, 1, 20, 20)) out = CNN_instance.forward(random_data) assert torch.sum(out > lower_bound).item() == 10*5, "lower {} vs. {} ".format(lower_bound, out) assert torch.sum(out < upper_bound).item() == 10*5, "upper {} vs. {} ".format(upper_bound, out)
def test_check_input_data_into_forward_once(): """Tests that check_input_data_into_forward_once method only runs once""" CNN_instance = CNN(layers_info=[["conv", 2, 2, 1, 2], ["adaptivemaxpool", 2, 2], ["linear", 6]], hidden_activations="relu", input_dim=(4, 2, 5), output_activation="relu", initialiser="xavier") data_not_to_throw_error = torch.randn((1, 4, 2, 5)) data_to_throw_error = torch.randn((1, 2, 20, 20)) with pytest.raises(AssertionError): CNN_instance.forward(data_to_throw_error) with pytest.raises(RuntimeError): CNN_instance.forward(data_not_to_throw_error) CNN_instance.forward(data_to_throw_error)
def test_output_activation(): """Tests whether network outputs data that has gone through correct activation function""" RANDOM_ITERATIONS = 20 input_dim = (5, 100, 100) for _ in range(RANDOM_ITERATIONS): data = torch.randn((1, *input_dim)) CNN_instance = CNN(layers_info=[["conv", 2, 2, 1, 2], ["adaptivemaxpool", 2, 2], ["linear", 50]], hidden_activations="relu", input_dim=input_dim, output_activation="relu", initialiser="xavier") out = CNN_instance.forward(data) assert all(out.squeeze() >= 0) CNN_instance = CNN(layers_info=[["conv", 2, 20, 1, 0], ["linear", 5]], hidden_activations="relu", input_dim=input_dim, output_activation="relu", initialiser="xavier") out = CNN_instance.forward(data) assert all(out.squeeze() >= 0) CNN_instance = CNN(layers_info=[["conv", 5, 20, 1, 0], ["linear", 5]], hidden_activations="relu", input_dim=input_dim, output_activation="relu", initialiser="xavier") out = CNN_instance.forward(data) assert all(out.squeeze() >= 0) CNN_instance = CNN(layers_info=[["conv", 5, 20, 1, 0], ["linear", 22]], hidden_activations="relu", input_dim=input_dim, output_activation="sigmoid", initialiser="xavier") out = CNN_instance.forward(data) assert all(out.squeeze() >= 0) assert all(out.squeeze() <= 1) assert round(torch.sum(out.squeeze()).item(), 3) != 1.0 CNN_instance = CNN(layers_info=[["conv", 2, 2, 1, 2], ["adaptivemaxpool", 2, 2], ["linear", 5]], hidden_activations="relu", input_dim=input_dim, output_activation="softmax", initialiser="xavier") out = CNN_instance.forward(data) assert all(out.squeeze() >= 0) assert all(out.squeeze() <= 1) assert round(torch.sum(out.squeeze()).item(), 3) == 1.0 CNN_instance = CNN(layers_info=[["conv", 2, 2, 1, 2], ["adaptivemaxpool", 2, 2], ["linear", 5]], hidden_activations="relu", input_dim=input_dim, initialiser="xavier") out = CNN_instance.forward(data) assert not all(out.squeeze() >= 0) assert not round(torch.sum(out.squeeze()).item(), 3) == 1.0