Fábio Perez
Fábio Perez

Reputation: 26108

Copying a PyTorch Variable to a Numpy array

Suppose I have a PyTorch Variable in GPU:

var = Variable(torch.rand((100,100,100))).cuda()

What's the best way to copy (not bridge) this variable to a NumPy array?

var.clone().data.cpu().numpy()

or

var.data.cpu().numpy().copy()

By running a quick benchmark, .clone() was slightly faster than .copy(). However, .clone() + .numpy() will create a PyTorch Variable plus a NumPy bridge, while .copy() will create a NumPy bridge + a NumPy array.

Upvotes: 4

Views: 3110

Answers (2)

prosti
prosti

Reputation: 46469

Because clone() is recorded by AD second options is less intense. There are few options you may also consider.

Upvotes: 0

Wasi Ahmad
Wasi Ahmad

Reputation: 37761

This is a very interesting question. According to me, the question is little bit opinion-based and I would like to share my opinion on this.

From the above two approaches, I would prefer the first one (use clone()). Since your goal is to copy information, essentially you need to invest extra memory. clone() and copy() should take a similar amount of storage since creating numpy bridge doesn't cause extra memory. Also, I didn't understand what you meant by, copy() will create two numPy arrays. And as you mentioned, clone() is faster than copy(), I don't see any other problem with using clone().

I would love to give a second thought on this if anyone can provide some counter arguments.

Upvotes: 3

Related Questions