dalbom
dalbom

Reputation: 65

Pytorch weighted Tensor

I'm porting a little bit complex TF2 code to Pytorch. Since TF2 does not distinguish Tensor and numpy array, it was straightforward on it. However, I feel like I came back to the TF1 era when I encountered several errors saying 'you cannot mix Tensor and numpy array here in Pytorch!'. Here is the original TF2 code:

def get_weighted_imgs(points, centers, imgs):
  weights = np.array([[tf.norm(p - c) for c in centers] for p in points], dtype=np.float32)
  weighted_imgs = np.array([[w * img for w, img in zip(weight, imgs)] for weight in weights])

  weights = tf.expand_dims(1 / tf.reduce_sum(weights, axis=1), axis=-1)
  weighted_imgs = tf.reshape(tf.reduce_sum(weighted_imgs, axis=1), [len(weights), 64*64*3])

  return weights * weighted_imgs

And my problematic Pytorch code:

def get_weighted_imgs(points, centers, imgs):
  weights = torch.Tensor([[torch.norm(p - c) for c in centers] for p in points])
  weighted_imgs = torch.Tensor([[w * img for w, img in zip(weight, imgs)] for weight in weights])

  weights = torch.unsqueeze(1 / torch.sum(weights, dim=1), dim=-1)
  weighted_imgs = torch.sum(weighted_imgs, dim=1).view([len(weights), 64*64*3])

  return weights * weighted_imgs

def reproducible():
  points = torch.Tensor(np.random.random((128, 5)))
  centers = torch.Tensor(np.random.random((10, 5)))
  imgs = torch.Tensor(np.random.random((10, 64, 64, 3)))

  weighted_imgs = get_weighted_imgs(points, centers, imgs)

I can guarantee that there is no issue with the dimension order or shape of the tensors/arrays. The error message I got is

ValueError: only one element tensors can be converted to Python scalars

which comes from

weighted_imgs = torch.Tensor([[w * img for w, img in zip(weight, imgs)] for weight in weights])

Could someone help me to solve this problem? That would be greatly appreciated.

Upvotes: 1

Views: 1223

Answers (1)

Gil Pinsky
Gil Pinsky

Reputation: 2493

Perhaps this will help you, but I'm not sure about your final multiplication between weights and weighted_imgs since they don't have the same shape, even after reshaping as you probably wanted. I am not sure I understood correctly your logic:

import torch
def get_weighted_imgs(points, centers, imgs):
  weights = torch.Tensor([[torch.norm(p - c) for c in centers] for p in points])
  
  imgs = imgs.unsqueeze(0).repeat(weights.shape[0],1,1,1,1)
  dims_to_rep = list(imgs.shape[-3:])
  weights = weights.unsqueeze(-1).unsqueeze(-1).unsqueeze(-1).repeat(1,1,*dims_to_rep)
  weights /= torch.sum(weights[...,0:1,0:1,0:1],dim=1, keepdim=True)
  weighted_imgs =  torch.sum(imgs * weights, dim=1).view(weights.shape[0], -1)
  
  return weighted_imgs #weights.view(weighted_imgs.shape[0],-1) *\
         #weighted_imgs # Shapes are torch.Size([128, 122880]) and torch.Size([128, 12288])

def reproducible():
  points = torch.Tensor(np.random.random((128, 5)))
  centers = torch.Tensor(np.random.random((10, 5)))
  imgs = torch.Tensor(np.random.random((10, 64, 64, 3)))

  weighted_imgs = get_weighted_imgs(points, centers, imgs)
#Test:
reproducible()

Upvotes: 2

Related Questions