我在特征矩阵和该矩阵的一行上使用了tensor.numpy()进行比较。我发现行输出和矩阵的相应行的值不相同。有人知道为什么吗?
def consine_distance_byrow(f1, f2):
feat1 = f1.view(-1, 1)
dists = torch.mm(f2, feat1)
dists = dists.numpy()
return dists
def consine_distance(f1, f2):
feat1 = torch.transpose(f1, 0, 1)
dists = torch.mm(f2, feat1)
dists = dists.numpy()
return dists
# load trainingset data
feature_path = '../../cnn/pytorch_result_train_val_' + str(self.feature_nums) + '.mat'
result = scipy.io.loadmat(feature_path)
train_feature = torch.FloatTensor(result['train_f']) # M x 512d
dists = consine_distance(train_feature, train_feature) # way1
for i in range(len(train_label)):
distsT = consine_distance_byrow(train_feature[i], train_feature) # way2
# here, dists[:,i] is slightly different with distsT
例如
dists[:0] = [ 1.0000001 -0.15086517 -0.08085391 ... -0.05950543 -0.03058994
0.10164267]
while i=0, distT = [[ 0.99999964]
[-0.15086518]
[-0.0808539 ]
...
[-0.05950541]
[-0.03058996]
[ 0.1016427 ]]
显然,两者之间略有不同。
这是在tensor.numpy()之后造成的,在此步骤之前,两个张量是相同的