I believe the following lines are causing the error:
    ...
    arr[j]=n
...
arr2[i] = a
...

You could append n and a to a list and creating a tensor from it.
Have a look at this modified code:
def my_loss(outputs, labels):
    
    """
    Args:
        outputs - output of network ([batch size, rank+1]) 
        labels  - desired coefficients  ([batch size, rank+1])
    """
    
    loss = torch.zeros(1, dtype=torch.float, requires_grad=True)
    loss = loss.to(device)
    arr2 = []

    for i in range(0,4):
        x=0
        arr = []
        for j in range(0,10):
            x = x + 0.1*j
            y1=outputs[i,0]
            y2=labels[i,0]
            n=torch.abs(y1-y2)
            arr.append(n)
        arr = torch.stack(arr)
       
        a=torch.max(arr)
        arr2.append(a)
        
    arr2 = torch.stack(arr2)
    loss=(torch.sum(arr2))/4
    print(loss)
                                  
    return loss

Note that I had to move arr into the inner loop.
Would that work for you? I’m not familiar with your loss function, so could you check, if the values make sense?