Get data from tensor and calculate loss

shashimalcse: <p>I am trying to calculate a loss using vector return from model. To do that i have get the <strong>xyzk</strong> from model output and using this vector i have calculate nearest point in a point cloud. (this point cloud has created using depth image that return from model.) loss is calculate using this nearest point and ground truth label. xyzk is only use for get the data to calculate nearest point. my problem is, is this update the network? because i am not using xyzk for loss calculation. if not how to do that</p>
<pre><code class="lang-auto">        for i, (img, face, location_channel ,heads,gt_labels,lenght) in tqdm(enumerate(train_data_loader), total=len(train_data_loader)) :
            img =  img.cuda()
            face = face.cuda()
            optimizer.zero_grad()
            depthk,xyzk = model(img,face,location_channel,heads,lenght)
            depth = depthk.squeeze(1)
            depth = depth.detach().cpu().data.numpy()
            xyzz = xyzk.detach().cpu().data.numpy()
            centers = location_channel[0,:,:]
            space = depth[0,:,:]
            head = np.array([heads[0,0],heads[0,1],space[heads[0,1],heads[0,0]]])
            gt_label = np.array([gt_labels[0,0],gt_labels[0,1],space[gt_labels[0,1],gt_labels[0,0]]])
            xyz = xyzz[0,:]
            distance = np.array([d([int(centers[x,0].item()),int(centers[x,1].item()),space[int(centers[x,1].item()),int(centers[x,0].item())]],head,xyz) for x in list(range(lenght[0]))])
            predict_center_index = np.argmax(distance)
            predict_center = torch.from_numpy(np.array([int(centers[predict_center_index,0].item()),int(centers[predict_center_index,1].item()),space[int(centers[predict_center_index,1].item()),int(centers[predict_center_index,0].item())]]))
            real_center = torch.from_numpy(gt_label)
            predict_center.requires_grad_(True)
            loss = criterion(predict_center,real_center)
            for i in range(1,depthk.shape[0]):
                centers = location_channel[i,:,:]
                space = depth[i,:,:]
                head = np.array([heads[i,0],heads[i,1],space[heads[i,1],heads[i,0]]])
                gt_label = np.array([gt_labels[i,0],gt_labels[i,1],space[gt_labels[i,1],gt_labels[i,0]]])
                xyz = xyzz[i,:]
                distance = np.array([d([int(centers[x,0].item()),int(centers[x,1].item()),space[int(centers[x,1].item()),int(centers[x,0].item())]],head,xyz) for x in list(range(lenght[i]))])
                predict_center_index = np.argmax(distance)
                predict_center = torch.from_numpy(np.array([int(centers[predict_center_index,0].item()),int(centers[predict_center_index,1].item()),space[int(centers[predict_center_index,1].item()),int(centers[predict_center_index,0].item())]]))
                real_center = torch.from_numpy(gt_label)
                predict_center.requires_grad_(True)
                loss += criterion(predict_center,real_center)
</code></pre>

Rahul_Chand: <p><a class="mention" href="/u/shashimalcse">@shashimalcse</a></p>
<p>The way you have written the code your model’s parameter won’t update, since you are <strong>detaching</strong> the <code>xyzk</code> which means the gradient won’t flow past it. Rather than converting <code>xyzk</code> to a numpy array, keep it as a <code>torch.Tensor</code> &amp; change the other variables to a <code>torch.Tensor</code> too.</p>