def test_calculate_loss_sets_final_output_derivative_on_success(self): pred = np.asarray([[randint(0, 3) for i in range (2)] for j in range(3)]) # make sure there is no loss lab = pred n = Net(MSE) n.calculate_loss(pred, lab) assert (n.loss_derivative == 0).all()
def test_sigmoid(self): rand = [[randint(-10, 10) for i in range(50)] for i in range(30)] torch_func = TorchSigmoid() my_func = Sigmoid(Net(MeanSquaredError)) torch_result = torch_func(torch.DoubleTensor(rand)) self.assertTrue((torch_result == torch.from_numpy( my_func.call(rand)).double()).all())
def test_relu(self): rand = [[randint(-10, 10) for i in range(50)] for i in range(30)] relu = ReLu(Net(MeanSquaredError)) torch_result = F.relu(Variable(torch.DoubleTensor(rand))) t = torch_result.data i = torch.from_numpy(relu.call(rand)) for x, y in zip(t, i): for i, j in zip(x, y): assert round(i.item(), 10) == round(j.item(), 10)
def test_old_weights_are_updated_on_second_pass(self): input = randint(5, 10) output = randint(5, 10) weights = np.asarray([[randint(0, 2) for j in range(input)] for i in range(output)]) n = Net(Sigmoid) linear = Linear(n, input, output, weights) linear.forward(input) sh = np.asarray(linear.network.saved_weights).shape linear.forward(input) assert sh == np.asarray(linear.network.saved_weights).shape
def test_saves_inputs_outputs_and_weights_to_network(self): input = randint(5, 10) output = randint(5, 10) weights = np.asarray([[randint(0, 2) for j in range(input)] for i in range(output)]) n = Net(Sigmoid) linear = Linear(n, input, output, weights) linear.forward(input) assert len(linear.network.saved_inputs) > 0 assert len(linear.network.saved_outputs) > 0 assert len(linear.network.saved_weights) > 0
def test_softmax(self): rand = np.asarray([[randint(0, 10) for i in range(2)] for i in range(3)]) softmax = SoftMax(Net(MeanSquaredError)) torch_result = F.softmax(Variable(torch.DoubleTensor(rand)), dim=1) t = torch_result.data i = torch.from_numpy(softmax.call(rand)) for x, y in zip(t, i): for i, j in zip(x, y): assert round(i.item(), 10) == round(j.item(), 10)
def test_convolution_output_with_biases(self): torch.random.manual_seed(42) inp = np.arange(96).reshape(3, 2, 4, 4) torch_layer = nn.Conv2d(in_channels=2, out_channels=2, kernel_size=2) torch_output = torch_layer(torch.tensor(inp).float()) kernel, biases = list(torch_layer.parameters()) # create using kernel generated by pytorch neurose_layer = Conv2D(Net(None), input_channels=1, kernel_amount=2, kernel_size=2, use_biases=True, initial_weights=kernel.detach().numpy(), initial_biases=biases.detach().numpy()) my_output = neurose_layer.forward_pass(inp) self.assertTrue(np.allclose(my_output, torch_output.detach().numpy()))
def test_convolution_output_with_one_kernel_more_channels(self): torch.random.manual_seed(42) in_channels = 3 batch_size = 3 kernel_size = 2 kernel_amount = 1 inp = np.arange(36).reshape(batch_size, in_channels, 2, 2) torch_layer = nn.Conv2d(in_channels=in_channels, out_channels=kernel_amount, kernel_size=kernel_size, bias=False) torch_output = torch_layer(torch.tensor(inp).float()) kernel = list(torch_layer.parameters())[0].detach().numpy() # create using kernel generated by pytorch neurose_layer = Conv2D(Net(None), input_channels=in_channels, kernel_amount=kernel_amount, kernel_size=kernel_size, initial_weights=kernel) my_output = neurose_layer.forward_pass(inp) self.assertTrue(np.allclose(my_output, torch_output.detach().numpy()))
def test_raises_error_if_biases_not_right_dimension(self): input = randint(5, 10) output = randint(5, 10) # biases should be the dimension of output biases = np.ndarray(output + 2) self.assertRaises(ValueError, Linear, Net(Sigmoid), input, output, np.ndarray(0), biases)
def test_raises_error_if_weights_not_right_dimension(self): input = randint(5, 10) output = randint(5, 10) weights = np.ndarray((output + 1, input)) self.assertRaises(ValueError, Linear, Net(Sigmoid), input, output, weights)
def test_biases_are_initialized_correctly(self): input = randint(5, 10) output = randint(5, 10) biases = np.asarray([randint(1, 5) for i in range(output)]) linear = Linear(Net(Sigmoid), input, output, np.ndarray(0), biases) self.assertTrue(np.array_equal(linear.biases, biases))
def test_weights_are_initialized_correctly(self): input = randint(5, 10) output = randint(5, 10) weights = np.asarray([[randint(0, 2) for j in range(input)] for i in range(output)]) linear = Linear(Net(Sigmoid), input, output, weights) self.assertTrue((linear.weights == weights).all())
def test_linear_layer_output_correct_shape(self): input = randint(5, 10) output = randint(5, 10) a = [randint(0, 5) for i in range(input)] linear = Linear(Net(Sigmoid), input, output) self.assertTrue(len(linear.forward(a)) == output)
def test_update_weights_raises_error_if_backpropagate_not_called(self): pred = np.asarray([[randint(0, 3) for i in range (2)] for j in range(3)]) lab = np.asarray([[randint(0, 3) for i in range (2)] for j in range(3)]) n = Net(MSE) self.assertRaises(ValueError, n.update_weights)
def test_backpropagation_raises_error_if_calculate_loss_not_called(self): pred = np.asarray([[randint(0, 3) for i in range (2)] for j in range(3)]) lab = np.asarray([[randint(0, 3) for i in range (2)] for j in range(3)]) n = Net(MSE) self.assertRaises(ValueError, n.backpropagate)
def test_calculate_loss_raises_error_if_wrong_dimensions(self): pred = np.asarray([[i for i in range (2)] for j in range(3)]) lab = np.asarray([[i for i in range (3)] for j in range(2)]) n = Net(MSE) self.assertRaises(ValueError, n.calculate_loss, pred, lab)