How can I masked (or index) evaluate a tensor?

How can I masked (or index) evaluate a tensor?
For example:

In[2]: import numpy as np
In[3]: a = np.array([[1,2], [3,4], [5,6]])
In[4]: b = np.array([[7,8], [9,10]])
In[5]: a[[0,1]] = b
In[6]: a

array([[ 7, 8],
[ 9, 10],
[ 5, 6]])


In[2]: import torch
In[3]: a = torch.FloatTensor([[1,2], [3,4], [5,6]])
In[4]: b = torch.FloatTensor([[7,8], [9,10]])
In[5]: a[torch.ByteTensor([0,1])] = b
Traceback (most recent call last):
File “/home/lii/anaconda2/lib/python2.7/site-packages/IPython/core/”, line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File “”, line 1, in
a[torch.ByteTensor([0,1])] = b
RuntimeError: Number of elements of destination tensor != Number of elements in mask at /data/users/soumith/miniconda2/conda-bld/pytorch-0.1.7_1485444530918/work/torch/lib/TH/generic/THTensorMath.c:43

Thank you!

Can anyone help? Thank you!

I find index_copy_ method works, a.index_copy(0, torch.LongTensor([0,1]), b) gives the right answer.

Yes, index_copy or gather may do it for you.

Thank you for your answer, I will have another question and need your help soon!