From 98634a497b38d1a2db8e9da7cbea65681530ea29 Mon Sep 17 00:00:00 2001 From: joserapa98 Date: Thu, 9 May 2024 22:49:30 +0200 Subject: [PATCH] Fix tests to avoid warnings --- tests/models/test_mps.py | 161 +++++++++++++++++++++------------------ 1 file changed, 87 insertions(+), 74 deletions(-) diff --git a/tests/models/test_mps.py b/tests/models/test_mps.py index d16b0bb..e43175e 100644 --- a/tests/models/test_mps.py +++ b/tests/models/test_mps.py @@ -649,18 +649,19 @@ def test_all_algorithms_diff_in_dim_bond_dim(self): def test_all_algorithms_marginalize(self): for n_features in [1, 2, 3, 4, 10]: for boundary in ['obc', 'pbc']: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - data = torch.randn(100, n_features // 2, 5) - - if example.numel() == 0: - example = None - data = None - in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) + + if example.numel() == 0: + example = None + data = None + mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -715,18 +716,19 @@ def test_all_algorithms_marginalize(self): def test_all_algorithms_marginalize_with_list_matrices(self): for n_features in [1, 2, 3, 4, 10]: for boundary in ['obc', 'pbc']: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - data = torch.randn(100, n_features // 2, 5) - - if example.numel() == 0: - example = None - data = None - in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) + + if example.numel() == 0: + example = None + data = None + mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -786,17 +788,19 @@ def test_all_algorithms_marginalize_with_list_matrices(self): def test_all_algorithms_marginalize_with_matrix(self): for n_features in [1, 2, 3, 4, 10]: for boundary in ['obc', 'pbc']: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - data = torch.randn(100, n_features // 2, 5) + in_features = torch.randint(low=0, + high=n_features, + size=(n_features // 2,)).tolist() + in_features = list(set(in_features)) + + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) if example.numel() == 0: example = None data = None - in_features = torch.randint(low=0, high=n_features, - size=(n_features // 2,)).tolist() - in_features = list(set(in_features)) - mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -856,18 +860,19 @@ def test_all_algorithms_marginalize_with_matrix_cuda(self): device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu') for n_features in [1, 2, 3, 4, 10]: for boundary in ['obc', 'pbc']: + in_features = torch.randint(low=0, + high=n_features, + size=(n_features // 2,)).tolist() + in_features = list(set(in_features)) + # batch x n_features x feature_dim - example = torch.randn(1, n_features // 2, 5, device=device) - data = torch.randn(100, n_features // 2, 5, device=device) + example = torch.randn(1, len(in_features), 5, device=device) + data = torch.randn(100, len(in_features), 5, device=device) if example.numel() == 0: example = None data = None - in_features = torch.randint(low=0, high=n_features, - size=(n_features // 2,)).tolist() - in_features = list(set(in_features)) - mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -928,18 +933,19 @@ def test_all_algorithms_marginalize_with_mpo(self): for n_features in [1, 2, 3, 4, 10]: for mps_boundary in ['obc', 'pbc']: for mpo_boundary in ['obc', 'pbc']: + in_features = torch.randint(low=0, + high=n_features, + size=(n_features // 2,)).tolist() + in_features = list(set(in_features)) + # batch x n_features x feature_dim - example = torch.randn(1, n_features // 2, 5) - data = torch.randn(100, n_features // 2, 5) + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) if example.numel() == 0: example = None data = None - in_features = torch.randint(low=0, high=n_features, - size=(n_features // 2,)).tolist() - in_features = list(set(in_features)) - mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -1011,17 +1017,18 @@ def test_all_algorithms_marginalize_with_mpo_cuda(self): for n_features in [1, 2, 3, 4, 10]: for mps_boundary in ['obc', 'pbc']: for mpo_boundary in ['obc', 'pbc']: - # batch x n_features x feature_dim - example = torch.randn(1, n_features // 2, 5, device=device) - data = torch.randn(100, n_features // 2, 5, device=device) - - if example.numel() == 0: - example = None - data = None - - in_features = torch.randint(low=0, high=n_features, - size=(n_features // 2,)).tolist() - in_features = list(set(in_features)) + in_features = torch.randint(low=0, + high=n_features, + size=(n_features // 2,)).tolist() + in_features = list(set(in_features)) + + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5, device=device) + data = torch.randn(100, len(in_features), 5, device=device) + + if example.numel() == 0: + example = None + data = None mps = tk.models.MPS(n_features=n_features, phys_dim=5, @@ -1099,18 +1106,19 @@ def test_all_algorithms_marginalize_with_mpo_cuda(self): def test_all_algorithms_no_marginalize(self): for n_features in [1, 2, 3, 4, 10]: for boundary in ['obc', 'pbc']: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - data = torch.randn(100, n_features // 2, 5) - - if example.numel() == 0: - example = None - data = None - in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) + + if example.numel() == 0: + example = None + data = None + mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -1163,17 +1171,19 @@ def test_all_algorithms_no_marginalize(self): def test_norm(self): for n_features in [1, 2, 3, 4, 10]: - for boundary in ['obc', 'pbc']: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - if example.numel() == 0: - example = None - + for boundary in ['obc', 'pbc']: in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) in_features.sort() + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + + if example.numel() == 0: + example = None + mps = tk.models.MPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -1790,18 +1800,19 @@ def test_all_algorithms(self): def test_all_algorithms_marginalize(self): for n_features in [1, 2, 3, 4, 10]: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - data = torch.randn(100, n_features // 2, 5) - - if example.numel() == 0: - example = None - data = None - in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) + + if example.numel() == 0: + example = None + data = None + mps = tk.models.UMPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -1845,18 +1856,19 @@ def test_all_algorithms_marginalize(self): def test_all_algorithms_no_marginalize(self): for n_features in [1, 2, 3, 4, 10]: - example = torch.randn(1, n_features // 2, 5) # batch x n_features x feature_dim - data = torch.randn(100, n_features // 2, 5) - - if example.numel() == 0: - example = None - data = None - in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + data = torch.randn(100, len(in_features), 5) + + if example.numel() == 0: + example = None + data = None + mps = tk.models.UMPS(n_features=n_features, phys_dim=5, bond_dim=2, @@ -1899,17 +1911,18 @@ def test_all_algorithms_no_marginalize(self): def test_norm(self): for n_features in [1, 2, 3, 4, 10]: - # batch x n_features x feature_dim - example = torch.randn(1, n_features // 2, 5) - if example.numel() == 0: - example = None - in_features = torch.randint(low=0, high=n_features, size=(n_features // 2,)).tolist() in_features = list(set(in_features)) in_features.sort() + # batch x n_features x feature_dim + example = torch.randn(1, len(in_features), 5) + + if example.numel() == 0: + example = None + mps = tk.models.UMPS(n_features=n_features, phys_dim=5, bond_dim=2,