1

I am trying to convert numpy array into PyTorch LongTensor type Variable as follows:

import numpy as np
import torch as th

y = np.array([1., 1., 1.1478225, 1.1478225, 0.8521775, 0.8521775, 0.4434675])
yth = Variable(th.from_numpy(y)).type(torch.LongTensor)

However the result I am getting is a rounded off version:

tensor([ 1,  1,  1,  1,  0,  0,  0])

How can I keep the precision of numpy array while getting LongTensor variable?

Expected result should be:

tensor([1., 1., 1.1478225, 1.1478225, 0.8521775, 0.8521775, 0.4434675])

1 Answer 1

1

LongTensor represents tensors with values of type long / int64 (c.f. table in doc). Your float values are thus converted (i.e. rounded) to integers.

To keep float values, use FloatTensor (float32) or DoubleTensor (float64) instead.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.