Bilinear layer : RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead

Hi, I am new in Pytorch. My model is the following:

emb = nn.Embedding(100, 100)
fc_emb = nn.Linear(100, 512, bias=False)
dropout_fc = nn.Dropout(0.3)

conv = nn.Conv1d(in_channels=2, out_channels=512, kernel_size=1, stride=1)

dropout_c = nn.Dropout(0.3)  

cat = nn.Bilinear(512, 512, 512)
drop_cat = nn.Dropout(0.3)  

I have tried this example:

x_c = dropout_c(F.relu(conv(torch.randn(200,2,20))))
x_c = torch.transpose(x_c, 1, 2)
x_n = emb(torch.randint(0,100,(200,20)))
x_n = dropout_fc(F.relu(fc_emb(x_n)))

x = drop_cat(cat(x_c, x_n))

But I keep getting this error:

RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.

when I run the following code the nn.Bilinear works well. But it gives me the error when I run the above codes.

cat(torch.randn(256, 20, 512),torch.randn(256, 20, 512)).shape

Could you please help me?

Bilinear doesn’t like the fact that x_c was transposed. You can replace the call with this: x = drop_cat(cat(x_c.contiguous(), x_n)). This basically just reshapes the data (in memory) to match what the transpose did.

Thank you @ayalaa2 :pray:t2: :v:t2: