仅舍入 torch.tensor() 的特定条目

Rounding only specific entries of torch.tensor()

使用torch.round()是否有可能最终舍入张量的特定条目?示例:

tensor([ 8.5040e+00,  7.3818e+01,  5.2922e+00, -1.8912e-01,  5.4389e-01,
        -3.6032e-03,  4.5763e-01, -2.7471e-02])

期望的输出:

tensor([ 9.,  74.,  5., 0.,  5.4389e-01,
        -3.6032e-03,  4.5763e-01, -2.7471e-02])

(只取前4个四舍五入)

您可以按照以下步骤进行

a[:4]=torch.round(a[:4])