debugging and improvement of the python code

47 Views Asked by At

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!

1

There are 1 best solutions below

3
Florent Monin On

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