How to convert a variable to a numpy?

I found that a tensor can be converted into a numpy array. but how to cast a variable into a numpy array?

va.data can do the job.

You can use the variable .data.
But this whatever you do with the numpy array will not be recorded by the autograd system and you won’t be able to backpropagate back.

1 Like

Thanks. It can fulfill my requirement.

what about replacing the var by such sort of var.data <-- np.array, it won’t autograd ???

Variable can only contain Tensors. But you can just convert your numpy array to a Tensor with: torch.from_numpy. Before creating a Variable from it.

Also you should remember that any action that you do directly on the tensor with var.data will not autograd !

1 Like

var.data.numpy() will convert Variable to numpy array.