How to convert float64 to torch.autograd.variable

I have a float number saved to A, as follows, I wish I can convert it to torch.autograd.variable. Searching the interent I found i can do the following, but now after doing the following, when I print the value of the B (which is the converted number) it does not show me anything.
Any suggestion?

A= np.float64(0.85210)
print(A)
print(type(A))
Output:
0.8521
<type ‘numpy.float64’>

Then for converting I do:

B=(Variable((torch.from_numpy(np.array(A, dtype=np.float64))).cuda()))
print(B)
print(type(B))
and the output is:
Variable containing:[torch.cuda.DoubleTensor with no dimension]
<class ‘torch.autograd.variable.Variable’>

P.S. the reason I convert to np.array first is because using torch.from_numpy(A) giving me the error that :
RuntimeError: from_numpy expects an np.ndarray but got numpy.float64

1 Like

torch.from_numpy() requires a numpy array as an argument , but what you are giving it is a float value.

First you need to convert your float value to an numpy array , which can be done like this :

A = 0.85210
A = numpy.array([A])

B = torch.autograd.Variable(torch.from_numpy(A))

This should work.

2 Likes