Hi everyone
I am swithcing from programin in matlab to pytorch and yet there are alot to learn for me.
I have a function as following, it takes an input in 4D, bachsize x channels x dimention1 x dimention2 and
do some operations on it and gives the output in the same size.
I will really appreciate it if you guys can help me and tell me if there is a more efficient and simple way to write this function in pytorch (so i can get rid of the loops) 
Thanks in advance …

def EdgeDetection(Input_Temp):
K1 = torch.FloatTensor([[1,0,0],[0,-1,0],[0,0,0]]).to(device)
K2 = torch.FloatTensor([[0,1,0],[-1,0,0],[0,0,0]]).to(device)

# print "The input size is" , Input_Temp.size() ,  "\n" #The input size is for example (128, 3, 32, 32)

D = 0 # D is responsible for number of Batches  or the 0st dimention
for BatchNum in range(Input_Temp.size(0)):
    C = 0 # C is responsible for number of channels or the 1st dimention 
    for ChannelNum in range(Input_Temp.size(1)):

        TempImg = Input_Temp[BatchNum,ChannelNum,:,:].clone()
        TempImgNormalize = TempImg

        # Makeing the input to have size of d x c x m x m so it can be used in conv2d
        A1 = torch.unsqueeze(torch.unsqueeze(TempImgNormalize,0),0).to(device)
        Gx = F.conv2d(A1, torch.unsqueeze(torch.unsqueeze(K1,0),0),padding= K1.size(0)/2 )
        Gy = F.conv2d(A1, torch.unsqueeze(torch.unsqueeze(K2,0),0),padding= K2.size(0)/2 )
        GM = (Gx**2 + Gy**2)**0.5


        # concatinating the results, concatenating along the 1st dimention:
        if C == 0:
            APP = GM 
        else:
            APP = torch.cat((APP,GM),1)
        C += 1

    # concatinating the results, concatenating along the 0st dimention:
    if D == 0:
        Final = APP
        Final = Final.to(device)
    else:
        Final = torch.cat((Final,APP),0)
    D = D + 1

return Final