Pytorch加权张量

达尔邦

我正在将一些复杂的TF2代码移植到Pytorch。由于TF2不能区分Tensor和numpy数组,因此它很简单。但是,我感觉回到了TF1时代,当时我遇到几个错误,说“您不能在Pytorch中混合Tensor和numpy数组!”。这是原始的TF2代码:

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

还有我有问题的Pytorch代码:

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)

我可以保证张量/数组的尺寸顺序或形状没有问题。我收到的错误消息是

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

来自

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

有人可以帮我解决这个问题吗?这将不胜感激。

吉尔·平斯基

也许这会为您提供帮助,但是我不确定权重和weighted_imgs之间的最终乘积,因为它们即使在重塑后也可能不具有相同的形状。我不确定我是否正确理解您的逻辑:

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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

PyTorch张量的加权平均值

来自分类Dev

如何展开PyTorch张量?

来自分类Dev

pytorch,如何扩展张量

来自分类Dev

pytorch中的张量变换?

来自分类Dev

PyTorch中的左移位张量

来自分类Dev

一维张量的Pytorch CrossEntropyLoss

来自分类Dev

pytorch张量进行尺寸扩展

来自分类Dev

从Pytorch张量获取值的范围

来自分类Dev

在pytorch张量中过滤行

来自分类Dev

将多维pytorch张量拆分为“ n”个较小的张量

来自分类Dev

Pytorch从张量文件中读取张量(来自磁盘的流训练)

来自分类Dev

PyTorch:变量数据必须是张量——数据已经是张量

来自分类Dev

如何计算张量A在沿着张量流中由张量B指定的权重的轴上的加权平均值?

来自分类Dev

PyTorch C ++扩展:访问半张量的数据

来自分类Dev

在Pytorch中重复张量的特定列

来自分类Dev

重复pytorch张量而不复制内存

来自分类Dev

在PyTorch中添加多个张量

来自分类Dev

pytorch张量如何按特定键值排序?

来自分类Dev

Pytorch:快速创建分数张量方法

来自分类Dev

如何将Numba用于Pytorch张量?

来自分类Dev

pytorch:如何堆叠2张量

来自分类Dev

pytorch计算堆叠张量的单独损失

来自分类Dev

从`torch`或`numpy`向量创建pytorch张量

来自分类Dev

如何理解在PyTorch中创建叶张量?

来自分类Dev

pytorch中的张量幂和乘法

来自分类Dev

如何在pytorch中动态索引张量?

来自分类Dev

PyTorch中参数与张量之间的差异

来自分类Dev

打印 PyTorch 张量的精确值(浮点精度)

来自分类Dev

如何在 PyTorch 中获取张量的值?