From be4eef45ac7c85cc3394db093851158637ac58f0 Mon Sep 17 00:00:00 2001 From: lucasdelimanogueira Date: Tue, 21 May 2024 14:32:14 -0300 Subject: [PATCH] unittest unsqueeze and unsqueeze neg --- norch/__pycache__/tensor.cpython-38.pyc | Bin 18972 -> 18990 bytes norch/tensor.py | 5 ++++- test.py | 5 +++-- tests/test_operations.py | 13 ++++++++++++- 4 files changed, 19 insertions(+), 4 deletions(-) diff --git a/norch/__pycache__/tensor.cpython-38.pyc b/norch/__pycache__/tensor.cpython-38.pyc index 4b4c091ce96e47215b92ef1ae9ac4b5322700597..5e72f97e94ffc18be6e6f6d0dc458e6f2d6ad727 100644 GIT binary patch delta 400 zcmXAi&r6d59LC@0`&NW=@5HTMr44oQ&Nhj)euxcXn6s^jtvMnHRMZu;U1}LSw0S6u zh(?F+@E{Rp?Idl;mpVig-nt2e9)^EFBAN(-C##>{N)yV4y*Fo{sgGWzT*O6oqP0O z_{SUiBoSn+?SYDBcDlP|>x=;$7l+G6xWfsfSMGN#5gv=v-3vUDnAsaVcN=02C#BrB3vX z*yVQeXx}nEiT~;puql<`FhSvXC?d&g2Y^@Nq3eVpGl@#AzY~;Kxf=9|XP^u8?ueg; ZE5K)Q-$(;Lxfm(nk65A^K;zua@*y=OdOH9B delta 356 zcmXAhO(??w7>EDAcjR-MCLj4|Gd89SVMa;wE#HZov}_JrI}9Z?ZVoi3cL~YIK{>GE zRf>b{;EJN0963nHh08Gcot~$je&G-{Lr{;X)u~E+Uw6He?MLdMCT}oq0l45*rgZ@X z6p!Lfm@1ipi#V#uhl(FGW=_`jBu-JhG(kVhJD%0cn6MoMHj*i8#8Bd zD;t@9;9kHlU!8}0X<@xbjU90`ECA0EX&fVpk?5No1hY7sTL=T}Z0?d5hXFLF;%H;J i*jtN0!GB(C3j@1S+u;GudAoBS*HYN!1=4t_d+!%T*m7(D diff --git a/norch/tensor.py b/norch/tensor.py index a4e8569..26192e7 100644 --- a/norch/tensor.py +++ b/norch/tensor.py @@ -155,8 +155,11 @@ class Tensor: return result_data def unsqueeze(self, dim): + if dim < 0: + dim = self.ndim + dim + 1 + # Ensure the dimension is valid - if dim < 0 or dim > self.ndim: + if dim > self.ndim: raise ValueError("Dimension out of range (expected to be in range of [0, {0}], but got {1})".format(self.ndim, dim)) # Create the new shape with an extra dimension of size 1 diff --git a/test.py b/test.py index 5622e3d..182f3aa 100644 --- a/test.py +++ b/test.py @@ -45,14 +45,15 @@ for epoch in range(epochs): for idx, batch in enumerate(train_loader): x, target = batch - x = x.T + x = x + print(x.shape) target = target x = x.to(device) target = target.to(device) outputs = model(x) - print(outputs.shape, target.shape) + print(outputs.shape, target.shape, x.shape) loss = criterion(outputs, target) optimizer.zero_grad() diff --git a/tests/test_operations.py b/tests/test_operations.py index e141066..f5751dd 100644 --- a/tests/test_operations.py +++ b/tests/test_operations.py @@ -232,6 +232,18 @@ class TestTensorOperations(unittest.TestCase): torch_expected_2 = torch_tensor.unsqueeze(2) self.assertTrue(utils.compare_torch(torch_unsqueeze_2, torch_expected_2)) + # Unsqueeze at dim=-1 + norch_unsqueeze_neg_1 = norch_tensor.unsqueeze(-1) + torch_unsqueeze_neg_1 = utils.to_torch(norch_unsqueeze_neg_1).to(self.device) + torch_expected_neg_1 = torch_tensor.unsqueeze(-1) + self.assertTrue(utils.compare_torch(torch_unsqueeze_neg_1, torch_expected_neg_1)) + + # Unsqueeze at dim=-2 + norch_unsqueeze_neg_2 = norch_tensor.unsqueeze(-2) + torch_unsqueeze_neg_2 = utils.to_torch(norch_unsqueeze_neg_2).to(self.device) + torch_expected_neg_2 = torch_tensor.unsqueeze(-2) + self.assertTrue(utils.compare_torch(torch_unsqueeze_neg_2, torch_expected_neg_2)) + def test_transpose(self): """ Test transposition of a tensor: tensor.transpose(dim1, dim2) @@ -308,7 +320,6 @@ class TestTensorOperations(unittest.TestCase): torch_tensor = torch.tensor([[[1, 2], [3, -4]], [[5, 6], [7, 8]]]).to(self.device) torch_expected = torch.sum(torch_tensor, dim=1, keepdim=True) - print(torch_result, torch_expected) self.assertTrue(utils.compare_torch(torch_result, torch_expected)) def test_max(self):