tom:

p_temp = q_temp.clone().detach().cuda().requires_grad_()


@tom
I have changed the code accordingly, but still getting the same error. Even after broadcasting I am getting the same error
def forward(self,encode_output, centroids):

    # Calculate q_ij (Eqn-4 of the paper)
    q_temp = Variable(torch.zeros(size=(len(encode_output),len(centroids))).cuda())

    for j in range(len(centroids)):
        val = encode_output - centroids[j]
        nom = torch.pow(val.norm(2,dim=1),2)
        q_temp[:,j] = torch.pow((1+nom/Constants.MU),-(Constants.MU+1)/2)

    q = Variable(q_temp.data.clone(),requires_grad=True).cuda()

    col_sum = torch.sum(q_temp,dim=1)  # --
    for i in range(len(encode_output)):
        q[i] = q[i].clone()/col_sum[i]


    # Calculate p_ij (Eqn-5 of paper)
    p_temp = q.clone().detach().cuda().requires_grad_()
    p_temp = torch.pow(p_temp,2)
    row_sum = torch.sum(q,dim=0)     # ||

    p_temp /= row_sum

    p = p_temp.clone().detach().cuda().requires_grad_()
    col_sum = torch.sum(p_temp, dim=1)  # --
    for i in range(len(encode_output)):
        p[i] = p[i].clone()/col_sum[i]

    kl_div = (p * (p.clamp(min=1e-7).log()  - q.clamp(min=1e-7).log())).sum()
    return kl_div

What am I missing?  I could not really understand (I am new to pytorch)