It works now. But how I can understand the generated gradient is correct. For example my result is not the thing that I expected but I am nt sure is teh problem of gradient or totally I should change the loss function. I used this code
      
        zzz=torch.zeros(bbb.shape)
        zzz1=torch.zeros(bbb1.shape)
        
        for ii in range(bbb.shape[0]):
             for ii1 in range(bbb.shape[1]):
                  for ii2 in range(bbb.shape[2]):
                            if bbb[ii,ii1,ii2]>=0.98:
                                zzz[ii,ii1,ii2]=1
                            elif  bbb[ii,ii1,ii2]<0.98:
                                zzz[ii,ii1,ii2]=0
                
        for ii in range(bbb1.shape[0]):
            for ii1 in range(bbb1.shape[1]):
                for ii2 in range(bbb1.shape[2]):
                        if bbb1[ii,ii1,ii2]>=0.98:
                           zzz1[ii,ii1,ii2]=1
                        elif  bbb1[ii,ii1,ii2]<0.98:
                            zzz1[ii,ii1,ii2]=0

        L1=criterion2(zzz,zzz1)
        loss2=errG1+1*L1
        loss2.backward()


It create gradient but I don’t know is it true or not