Implicit copy from Variable to numpy slice is slow

Copying from a Variable to a numpy slice without doing .data.numpy() first takes a few seconds to perform, even with small tensors.

tensor = torch.randn(1000, 10)
variable = Variable(tensor)

array = np.empty((10000, 10))

array[0:1000] = variable  # Slow (7.49 s!)
array[1000:2000] =  # (5.73 µs)

this is expected.

in the first case, it’s probably indexing one element at a time or something weird like that.

1 Like