RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x645 and 215x215)

neelam19051: <p>Here, I am providing a few chunks of my code where it gives an error regarding matrix multiplication. I am new here, and I am trying but unable to figure out its solution. If possible, please help me out.</p>
<pre><code class="lang-auto">BP  Weight_model start
Weight_classifier(
  (weight_layer): MaskedLinear(in_features=215, out_features=215, bias=True)
  (outlayer): Linear(in_features=215, out_features=215, bias=True)
)
batch_size_8,learning_rate_0.01,epoch_times_1
Traceback (most recent call last):
  File "/home/bvs/neelam/input_ourmodel/input/4valid.py", line 1012, in &lt;module&gt;
    validation(Terms[0], 5)
  File "/home/bvs/neelam/input_ourmodel/input/4valid.py", line 994, in validation
    each_fold_scores = Main(train_set, test_set, func=func)
  File "/home/bvs/neelam/input_ourmodel/input/4valid.py", line 884, in Main
    out = weight_model(weight_features)
  File "/home/bvs/miniconda3/envs/crisprcasfinder/envs/envML/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/bvs/neelam/input_ourmodel/input/4valid.py", line 314, in forward
    weight_out = self.weight_layer(weight_features)
  File "/home/bvs/miniconda3/envs/crisprcasfinder/envs/envML/lib/python3.10/site-packages/torch/nn/modules/module.py", line 1130, in _call_impl
    return forward_call(*input, **kwargs)
  File "/home/bvs/neelam/input_ourmodel/input/4valid.py", line 331, in forward
    return F.linear(input, masked_weight, self.bias)
RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x645 and 215x215)`
</code></pre>
<p>Here, I am providing link of all my input file that i am using in this model (4valid.py). (<a href="https://github.com/neelam19051/DLmodel" class="inline-onebox" rel="noopener nofollow ugc">GitHub - neelam19051/DLmodel</a>)</p>
<p>Thank you so much!</p>

ptrblck: <p>The issue is raised due to a shape mismatch in a linear layer as seen here:</p>
<pre data-code-wrap="python"><code class="lang-python">lin = nn.Linear(215, 215)

# fails
x = torch.randn(8, 645)
out = lin(x)
# RuntimeError: mat1 and mat2 shapes cannot be multiplied (8x645 and 215x215)

# works
x = torch.randn(8, 215)
out = lin(x)
</code></pre>
<p>Either reduce the input features to <code>215</code> in the incoming activation tensor or change the <code>in_features</code> of the failing linear layer to <code>645</code>.</p>

neelam19051: <p>After examining the code segment provided, I attempted to address the issue by altering the input features, specifically adjusting them to either 215 or 645. Despite these adjustments, the error persists.</p>
<pre><code class="lang-auto">
class Weight_classifier(nn.Module):
    def __init__(self, func):
        super(Weight_classifier, self).__init__()
        self.weight_layer = MaskedLinear(645, OUT_nodes[func], '{}_maskmatrix.csv'.format(func)).cuda()
        self.outlayer= nn.Linear(OUT_nodes[func], OUT_nodes[func])

    def forward(self, weight_features):
        weight_out = self.weight_layer(weight_features)
        weight_out = F.relu(weight_out)
        weight_out = F.sigmoid(self.outlayer(weight_out))
        return weight_out

class MaskedLinear(nn.Linear):
    def __init__(self, in_features, out_features, relation_file, bias=True):
        super(MaskedLinear, self).__init__(in_features, out_features, bias)

        mask = self.readRelationFromFile(relation_file)
        self.register_buffer('mask', mask)
        self.iter = 0

    def forward(self, input):
        
       # masked_weight = self.weight.T * self.mask
        masked_weight = self.weight * self.mask
        print(f"Input shape: {input.shape}")  # Print the shape of the input tensor
        print(f"Masked weight shape: {masked_weight.shape}")  # Print the shape of the masked_weight tensor
        return F.linear(input, masked_weight, self.bias)

    def readRelationFromFile(self, relation_file):
        mask = []
        with open(relation_file, 'r') as f:
            for line in f:
                l = [int(x) for x in line.strip().split(',')]
                for item in l:
                    assert item == 1 or item == 0  # relation 
                mask.append(l)
        return Variable(torch.Tensor(mask))
</code></pre>
<p>Thank you for your time and response.</p>