Q:

Numpy/PyTorch method for partial tiling

I seem to recall encountering a Numpy or PyTorch method similar to numpy.tile, except that it allowed partial tiling to reach specified dimensions. So if I had 
a = np.array([[5, 6, 7, 8],
              [1, 2, 3, 4]])

(or, correspondingly, t = torch.Tensor(a) if this is a PyTorch method),
then
a.mystery_method((3,4))

would produce output
array([[5, 6, 7, 8],
       [1, 2, 3, 4],
       [5, 6, 7, 8]])

Unfortunately, I now can't find this method in the Numpy or Pytorch docs. Does it actually exist, and what is it if so?

A:

You can use np.resize -
M = 3 # number of rows for output
np.resize(a,(M,a.shape[1]))

Another way with np.take or simply indexing along the first axis for performance -
np.take(a,np.arange(M)%a.shape[0],axis=0) # with np.take
a[np.arange(M)%a.shape[0]]                # with indexing

Runtime test -
In [91]: a = np.random.randint(0,9,(2000,4000))

In [92]: M = 3000

In [93]: %timeit np.resize(a,(M,a.shape[1]))
10 loops, best of 3: 24.1 ms per loop

In [94]: %timeit np.take(a,np.arange(M)%a.shape[0],axis=0)
100 loops, best of 3: 16.2 ms per loop

In [95]: %timeit a[np.arange(M)%a.shape[0]]
100 loops, best of 3: 15.2 ms per loop

