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 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.