mhaut / AngularGrad

Pytorch implementation of AngularGrad: A New Optimization Technique for Angular Convergence of Convolutional Neural Networks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ans1[0] == "False" is always False

hd132620 opened this issue · comments

Hi, I'm a machine learning engineer working in medical data company in south korea.

I found your paper and thought that it was state-of-the-art. I was totally impressed by your brilliant idea, so I wanted to make them available in Tensorflow. Now, I'm implementing it as tensorflow optimizer. In order to do so, I had to fully understand and analyze your codes. While reading codes, a question came to mind during the analysis and I decided to post an issue.

Whether its type is boolean or tensor is not important. ans1[0] == "False" cannot be True and always be False. This consequence makes codes below not approchable.

except:
    if (ans1[0] == "False"):
        min = angle
        diff = abs(previous_grad - grad)
        final_cos_theta = cos_theta.clone()

I'm not familiar with Pytorch. There could be possibility that I misunderstood the codes so i would appreciated it if you could explain this codes.

@hd132620 thank you very much for your appreciation in our work and kudos to your endeavour for making this code available in TensorFlow. Let me answer your query.The line

ans1, count = torch.unique(ans, return_counts= True)

returns a tensor of size torch.Size([2]) or torch.Size([1]) depending on the values of the tensor "ans" being combination of "True" and "False" or only either all "True" or all "False". The next code segment

try:
    if(count[1] < count[0]):
        min = angle
        diff = abs(previous_grad - grad)
        final_cos_theta = cos_theta.clone()
except:
    if(ans1[0] == "False"):
        min = angle
        diff = abs(previous_grad - grad)
        final_cos_theta = cos_theta.clone()

tries to find the minimum angle based on the values in the "count" tensor. The except block comes into play when torch.unique returns tensor of size torch.Size([1]). That means when the "ans" tensor only contained values either all "True" or all "False".

@purbayankar Thank you for your detailed explanation. It helped a lot. For now, I guess I correctly understood your exact intentions.
So, ans1 is the tensor with unique values of 'ans'. Is that right? If true, what is the actual 'type' of ans1 elements? As this code suggests, the values of ans1 are literally "True" and "False"(str type) but I think str type of boolean can't be calculated in torch.unique.

I tested codes below and it turned out that the result of comparing str and tensor value is always the False value.

Code

angle = torch.atan(tan_theta) * (180 / 3.141592653589793238)
ans = torch.gt(angle, min)
ans1, count = torch.unique(ans, return_counts=True)
if state['step'] == 1:
    print(ans1[0], ans1[0] == "False", ans1[0] == False)

Result

...
tensor(True, device='cuda:0') False tensor(False, device='cuda:0')
tensor(False, device='cuda:0') False tensor(True, device='cuda:0')
...

@hd132620 Thank you very much for pointing out the issue. The following change:

if(ans1[0].item() == False):

solves the problem.

It won't affect the results reported in the paper.