James Arten
James Arten

Reputation: 666

Rounding only specific entries of torch.tensor()

Using torch.round() is it possible to eventually round specific entries of a tensor? Example:

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])

Desired output:

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

(Only first 4 rounded)

Upvotes: 1

Views: 809

Answers (2)

Volkov Maxim
Volkov Maxim

Reputation: 331

Another (a little bit shorter) option is

t = torch.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])

t[:4].round()

or inplace

t[:4].round_()

Upvotes: 1

noob
noob

Reputation: 788

you can do as follow

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

Upvotes: 1

Related Questions