ValueError:类型必须是 ndarray 类型的子类型

See*_*ney 5 python

当我尝试训练我的模型时,

"ValueError: Type must be a sub-type of ndarray type"

出现于line x_norm=(np.power(x,2)).sum(1).view(-1,1)

代码 :

def pairwise_distances(x, y=None):
  
  x_norm = (np.power(x,2)).sum(1).view(-1, 1)

   if y is not None:
   y_t = torch.transpose(y, 0, 1)
   y_norm = (y**2).sum(1).view(1, -1)
  else:
   y_t = torch.transpose(x, 0, 1)
   y_norm = x_norm.view(1, -1)

  dist = x_norm + y_norm - 2.0 * torch.mm(x, y_t)
  # Ensure diagonal is zero if x=y
  # if y is None:
  #     dist = dist - torch.diag(dist.diag)
  return torch.clamp(dist, 0.0, np.inf)
Run Code Online (Sandbox Code Playgroud)

Kal*_*zvx 9

Numpy 数组view与 torch 张量不同view
\n在numpy中:

\n
ndarray.view([dtype][, type])\n    New view of array with the same data.\n
Run Code Online (Sandbox Code Playgroud)\n

火炬中:

\n
view(*shape) \xe2\x86\x92 Tensor\n    Returns a new tensor with the same data as the self tensor but of a different shape.\n
Run Code Online (Sandbox Code Playgroud)\n

Numpyview更改数组的数据类型,而不是形状。\n如果要更改 numpy 中数组的形状,请改用ndarray.reshape

\n