gpt4 book ai didi

python - 值错误 : Type must be a sub-type of ndarray type

转载 作者:行者123 更新时间:2023-12-04 13:13:32 27 4
gpt4 key购买 nike

当我尝试训练我的模型时"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)

最佳答案

Numpy 数组 view与火炬张量不同 view .
numpy :

ndarray.view([dtype][, type])
New view of array with the same data.
pytorch :
view(*shape) → Tensor
Returns a new tensor with the same data as the self tensor but of a different shape.
Numpy 的 view更改数组的数据类型,而不是形状。
如果要更改 numpy 中数组的形状,请使用 ndarray.reshape 反而。

关于python - 值错误 : Type must be a sub-type of ndarray type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62490720/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com