gpt4 book ai didi

python - 如何从多维张量中提取值而不丢失后向信息 - PyTorch

转载 作者:行者123 更新时间:2023-12-01 00:15:41 25 4
gpt4 key购买 nike

我正在尝试提取多维张量的一些特定列。我可以将这些值放在多维访问的概念中;但是,当我创建新值时,我会丢失张量的“grad_fn”选项。

我实际的多维张量 x 是:

tensor([[[-8.5780, -7.1091, -8.9204,  ..., -8.0616, -8.4115, -7.6345],
[-7.9776, -7.3767, -8.2914, ..., -7.9634, -9.1003, -7.5687],
[-7.7192, -7.4307, -8.4294, ..., -7.8605, -8.1345, -7.0781],
...,
[-8.3652, -7.5910, -8.6671, ..., -8.0487, -8.5826, -7.8624],
[-8.1572, -7.1679, -8.8977, ..., -8.1059, -8.1500, -7.7310],
[-8.1821, -7.5455, -9.2328, ..., -8.7435, -8.4430, -7.2503]]],
grad_fn=<LogSoftmaxBackward>)

形状为 (1,10,2000)。

例如,我想提取其特定的 10 列,最后创建一个形状为 (1,10,10) 或 (10,10) 的数组。

目前,当然,我可以将这些值提取到数组中:

    for i in range(x.size(0)):

values = np.array([x[i][1:, 1].detach().numpy()])

idx_to_get = [1,5,7,25,37,44,720,11,25,46]

for idx in idx_to_get:
values = np.append(sentence_scores, np.array([np.array(x[0][1:,idx].detach().numpy())]), axis=0)

values = np.delete(values, 0, axis=0)

print(torch.from_numpy(values))

运行上面的代码给了我以下值的输出:

tensor([[-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
-7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
-7.6684],
[-7.2831, -7.7666, -7.8302, -7.3651, -7.2184, -6.7932, -7.1968, -7.6590,
-7.4033, -6.9504, -7.0767, -7.5366, -7.8364, -7.5935, -8.1235, -7.3222,
-7.8096],
[-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
-7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
-7.6684],
[-7.5650, -7.6627, -7.4230, -7.4726, -7.5621, -7.4489, -7.8344, -7.6130,
-7.9440, -7.6158, -7.1895, -7.8070, -7.2306, -7.6364, -7.7390, -7.6832,
-7.5931],
[-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
-7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
-7.6684],
[-7.5589, -6.7990, -7.2068, -7.4451, -7.6688, -7.2991, -7.1398, -7.4362,
-7.4959, -8.0101, -7.5106, -8.0425, -7.6203, -7.7266, -7.9249, -7.6479,
-7.6684],
[-7.2831, -7.7666, -7.8302, -7.3651, -7.2184, -6.7932, -7.1968, -7.6590,
-7.4033, -6.9504, -7.0767, -7.5366, -7.8364, -7.5935, -8.1235, -7.3222,
-7.8096],
[-8.3559, -8.3751, -8.2082, -8.6825, -8.4860, -8.4156, -8.4683, -8.8760,
-8.7354, -8.6155, -8.7544, -8.4527, -8.3690, -8.5795, -8.6023, -8.2267,
-8.4736],
[-7.4392, -7.4694, -7.4094, -7.5062, -7.7691, -7.9009, -7.7664, -7.1755,
-8.0641, -7.6327, -7.6413, -7.9604, -7.9520, -7.8893, -7.8119, -7.8718,
-8.0961],
[-8.2182, -8.0280, -8.1398, -8.0258, -7.9951, -8.0664, -8.1976, -7.6182,
-8.0356, -8.0293, -7.7228, -7.7847, -7.4966, -7.6925, -7.5268, -7.0476,
-7.2920]])

但值也应该有 grad_fn。我该怎么办?

我知道,使用values.requires_grad_(True)是有效的,但我相信使用该函数时,我会丢失x上的LogSoftmaxBackward。

最佳答案

问题是您无法使用 numpy 函数来完成此操作并保留图形。您必须仅使用 PyTorch 函数。

x = torch.rand((1,10,2000), requires_grad=True)
idx_to_get = [1,5,7,25,37,44,720,11,25,46]
values = x[0,1:,idx_to_get]
values
tensor([[0.6669, 0.1121, 0.1323, 0.7351, 0.0252, 0.2551, 0.3044, 0.3986, 0.7351,
0.1060],
[0.6169, 0.7715, 0.2829, 0.2860, 0.6810, 0.2485, 0.8585, 0.5284, 0.2860,
0.8363],
[0.6877, 0.0899, 0.6346, 0.7018, 0.7357, 0.1477, 0.2073, 0.3877, 0.7018,
0.0226],
[0.9241, 0.7883, 0.8442, 0.1831, 0.0551, 0.0209, 0.5300, 0.6909, 0.1831,
0.2950],
[0.5141, 0.5072, 0.4354, 0.3998, 0.5152, 0.9183, 0.2200, 0.5955, 0.3998,
0.8631],
[0.9630, 0.3542, 0.8435, 0.8299, 0.8614, 0.5029, 0.8672, 0.4985, 0.8299,
0.2207],
[0.6399, 0.5128, 0.2131, 0.4255, 0.9318, 0.6598, 0.8478, 0.7902, 0.4255,
0.9080],
[0.8920, 0.0357, 0.8957, 0.7379, 0.0191, 0.6750, 0.8326, 0.8535, 0.7379,
0.9087],
[0.5243, 0.7446, 0.4278, 0.3542, 0.1104, 0.2999, 0.0132, 0.8218, 0.3542,
0.6761]], grad_fn=<IndexBackward>)

关于python - 如何从多维张量中提取值而不丢失后向信息 - PyTorch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349058/

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