Expected object of backend CUDA but got backend CPU for argument #2 'mat2'

I encountered this error for the below code snippet.

def init(self, original_dim, projection_dim, n_modalities=2):
super().init()

    self.C = []
    self.n_modalities = n_modalities

    for _ in range(n_modalities):
        # C tensor performs the mapping of the h vector and stores the s vector values as well
        C = torch.zeros(original_dim, projection_dim)
        for i in range(original_dim):
            C[i, np.random.randint(0, projection_dim-1)] = 2 * \
                np.random.randint(0, 2) - 1  # s values

        self.C.append(C)

def to(self, device):
    for i in range(len(self.C)):
        self.C[i].to(device)

def forward(self, *x):
    feature_size = x[0].size()
    y = [0]*self.n_modalities

    for i, d in enumerate(x):
        y[i] = d.mm(self.C[i]).view(feature_size[0], -1) # Error line

If self.C[i] returns a tensor, you would have to reassign the result of to:

self.C[i] = self.C[i].to(device)

Thank you for quick response,

model is working for below snippet but is it appropriate to define “device” in forward block?

def forward(self, *x):
device = torch.device (“cuda” if torch.cuda.is_available() else “cpu”)
feature_size = x[0].size()
y = [0]*self.n_modalities

for i, d in enumerate(x):
self.C[i] = self.C[i].to(device)
    y[i] = d.mm(self.C[i]).view(feature_size[0], -1)

If you need to create a tensor in the forward method, I would recommend to use the device attribute of any parameter of the model. Otherwise you might run into problems e.g. if you are using data parallel.

However, in your approach you are pushing each tensor in self.C to the same device in each forward pass, which seems not to be necessary, as you could also do it in the __init__ method once.
Is self.C containing tensors with a variable shapes? If not, you could just use torch.stack to create a single tensor.