0

I have the following code and when I execute the code nothing happens. I wonder why no error occurs.

# weights_dict = dictionary with multiple tensors (matrices) als values
for i in range(0, len(weights_dict)-1):
    for j in range(1, len(weights_dict)-2):
        
        # test, if the shape of two successive tensors are matching
        x = weights_dict[f'weights_{i}'].shape == weights_dict[f'weights_{j}'].shape
        
        # if the shapes doesn't match pad zeros and update the dict
        if x is False:
            print(f'no match between \'weights_{i}\' and \'weights_{j}\': ', weights_dict[f'weights_{i}'].shape, 'and', weights_dict[f'weights_{j}'].shape)
            weights_dict.update({f'weights_{j}':F.pad(input=weights_dict[f'weights_{j}'], pad=(0,272,0,502), mode='constant', value=0)})
        
        # if the shapes match do nothing
        else:
            print(f'match between \'weights_{i}\' and \'weights_{j}\': ', weights_dict[f'weights_{i}_init'].shape, 'and', weights_dict[f'weights_{j}'].shape)
        
        # after the padding, check if the shapes match this time
        y = weights_dict[f'weights_{i}'].shape == weights_dict[f'weights_{j}'].shape
        if y is False:
            print(f'no match between \'weights_{i}_init\' and \'weights_{j}\': ', weights_dict[f'weights_{i}'].shape, 'and', weights_dict[f'weights_{j}'].shape)
        else:
            print(f'match between \'weights_{i}\' and \'weights_{j}\': ', weights_dict[f'weights_{i}_init'].shape, 'and', weights_dict[f'weights_{j}'].shape)

        # more code will follow

I think that in the line where the padding takes place, the entry of the dictionary cannot be recognised correctly because of the variable in the name. Since the weights are all different and their order is important and I want to distinguish them, I have given them ascending numbers (dictionary keys).

Is there a more efficient and error-free way to do this?`

The dictionary looks like this:

{'weights_0': tensor([[-0.0262,  0.0310,  0.0067,  ..., -0.0162,  0.0241,  0.0181],
         [-0.0299,  0.0230, -0.0328,  ...,  0.0084, -0.0042, -0.0162],
         [ 0.0150,  0.0003, -0.0052,  ...,  0.0046,  0.0110,  0.0019],
         ...,
         [-0.0346, -0.0283,  0.0035,  ...,  0.0010,  0.0279, -0.0162],
         [-0.0166, -0.0165, -0.0339,  ..., -0.0101, -0.0346,  0.0035],
         [ 0.0146,  0.0320,  0.0009,  ...,  0.0065,  0.0058,  0.0288]]),
 'weights_1': tensor([[-6.2551e-03,  1.6126e-02,  3.9450e-02,  ...,  1.7971e-05,
           2.4612e-02, -4.0139e-02],
         [-3.0003e-02, -1.6719e-03, -2.3985e-02,  ...,  4.3558e-02,
          -1.9130e-02,  2.3564e-02],
         [ 2.9886e-02,  3.2086e-02, -4.1213e-02,  ..., -2.4083e-02,
           2.7199e-02, -4.3203e-02],
         ...,
         [ 2.7709e-02, -2.3003e-02,  4.4214e-03,  ...,  2.7394e-02,
          -1.6083e-02, -1.7070e-02],
         [ 3.7920e-02,  5.7346e-03, -2.7768e-02,  ...,  2.0152e-02,
           2.6525e-02, -1.8638e-02],
         [ 1.9585e-02, -5.5044e-03,  2.6463e-02,  ..., -3.2142e-02,
          -2.2696e-02,  1.6047e-02]])}

Thanks for your help!

7
  • 1
    what is the length of weights_dict? When you say "nothing happens", do you mean that nothing gets printed at all? Commented Oct 13, 2022 at 20:26
  • Hard to say without data that demonstrates the problem. But I can guess. Do you think you are comparing every combination of two distinct items from a collection? But on closer examination you have if else where they both print something, so perhaps at least one of the loops doesn't get entered. That's why Florent asked about the length of weights_dict. If you iterate over a length of zero it won't enter the loop at all. Commented Oct 13, 2022 at 23:24
  • Yes, nothing gets printed at all. In this case the length is 2, but it should also work for a much bigger length. Commented Oct 14, 2022 at 6:57
  • I don't understand the i and j in your loop, you're comparing some items to themselves and you're not even looking at some items. And they're both iterating over the same container. Do you want all pairs or all adjacent items? My best guess is you need two separate loops (not nested) for two tasks: find the biggest shape, pad everything else to match. Commented Oct 14, 2022 at 15:12
  • also weights_dict is just a list with extra steps Commented Oct 14, 2022 at 15:13

1 Answer 1

0

weights_dict is a list of length 2, from what you have said, so

for j in range(1, len(weights_dict)-2):

is the same thing as range(1, 0), which doesn't contain anything. Hence, your second loop is not doing anything.

This is why nothing is printing, because the code doesn't even enter the loop.

If you want to iterate of a list my_list in python, you can do use range(len(my_list). No need to put 0 as the first parameter, since it is the default starting value. For the second loop, I'm not sure what you are trying to achieve. If you want to compare all pairs of values in weight_dicts, then you will want something like this:

for i in range(len(weight_dicts)):
    for j in range(i+1, len(weight_dicts)):
        ...

If, as the comment

if the shape of two successive tensors are matching would suggest that you want to compare all successive pairs. In this case, you only need one loop:

for i in range(len(weight_dicts)-1):
    weights_1 = weight_dicts[i]
    weights_2 = weight_dicts[i+1]
    ...

Notice that, in the second case, since I'm accessing weight_dicts[i+1], I'm only going up to len(weight_dicts)-1 in the loop index, otherwise we would get an IndexError

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. Unfortunately, the shapes of the weights don't always match. So I have to test for equality and adjust the shape if necessary. I use the two loops to reference the distinct neighbourhood of the weights. The order of the weights is important for my following considerations. About your code: If I have not only two but 100 weights, how can I write them efficiently into the loop without writing 100 lines of code?
I'm not sure I understand what you mean. Can you show on a toy example (3 or 4 tensors) which ones you need to compare/work with?
Thanks for your help, I found a solution. It would be to difficult to explain it any further.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.