Getting the mat1 dim 1 must match mat2 dim 0 when trying to do sentiment analysis

lotad: <p>Im making a simple model:<br>
class Linear(nn.Module):<br>
def <strong>init</strong>(self, input_size, hidden_size, num_classes):<br>
super(Linear, self).<strong>init</strong>()<br>
self.fc1 = nn.Linear(input_size, hidden_size, bias=True)<br>
self.fc2 = nn.Linear(hidden_size, num_classes, bias=True)</p>
<pre><code>def forward(self, text, text_lengths):
    text = text.float() # dense layer deals with float datatype
    x = self.fc1(text)
    preds = self.fc2(x)
    return preds
</code></pre>
<h2>and training it with the following code:<br>
def train(model, iterator, optimizer, criterion):<br>
epoch_loss = 0<br>
epoch_acc = 0<br>
for batch in iterator:<br>
optimizer.zero_grad()<br>
text, text_lengths = batch.text<br>
print(np.shape(text))<br>
print(np.shape(text_lengths))<br>
predictions = model(text, text_lengths)<br>
loss = criterion(predictions, batch.labels.squeeze())<br>
acc = accuracy(predictions, batch.labels)<br>
loss.backward()<br>
optimizer.step()<br>
epoch_loss += loss.item()<br>
epoch_acc += acc.item()<br>
return epoch_loss / len(iterator), epoch_acc / len(iterator)<br>
When I run it, I am getting the following error:</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
----&gt; 1 run_train(num_epochs, linear_model, train_iterator,  valid_iterator, optimizer, loss_func, ‘linear’)</p>
<p> in run_train(epochs, model, train_iterator, valid_iterator, optimizer, criterion, model_type)<br>
5<br>
6         # train the model<br>
----&gt; 7         train_loss, train_acc = train(model, train_iterator,    optimizer, criterion)<br>
8<br>
9         # evaluate the model</p>
<p> in train(model, iterator, optimizer, criterion)<br>
8         print(np.shape(text))<br>
9         print(np.shape(text_lengths))<br>
—&gt; 10         predictions = model(text, text_lengths)<br>
11         loss = criterion(predictions, batch.labels.squeeze())<br>
12         acc = accuracy(predictions, batch.labels)</p>
<p>c:\users\mynam\appdata\local\programs\python\python38\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
720             result = self._slow_forward(*input, **kwargs)<br>
721         else:<br>
–&gt; 722             result = self.forward(*input, **kwargs)<br>
723         for hook in itertools.chain(<br>
724                 _global_forward_hooks.values(),</p>
<p> in forward(self, text, text_lengths)<br>
8     def forward(self, text, text_lengths):<br>
9         text = text.float() # dense layer deals with float datatype<br>
—&gt; 10         x = self.fc1(text)<br>
11         preds = self.fc2(x)<br>
12         return preds</p>
<p>c:\users\mynam\appdata\local\programs\python\python38\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)<br>
720             result = self._slow_forward(*input, **kwargs)<br>
721         else:<br>
–&gt; 722             result = self.forward(*input, **kwargs)<br>
723         for hook in itertools.chain(<br>
724                 _global_forward_hooks.values(),</p>
<p>c:\users\mynam\appdata\local\programs\python\python38\lib\site-packages\torch\nn\modules\linear.py in forward(self, input)<br>
89<br>
90     def forward(self, input: Tensor) -&gt; Tensor:<br>
—&gt; 91         return F.linear(input, self.weight, self.bias)<br>
92<br>
93     def extra_repr(self) -&gt; str:</p>
<p>c:\users\mynam\appdata\local\programs\python\python38\lib\site-packages\torch\nn\functional.py in linear(input, weight, bias)<br>
1672     if input.dim() == 2 and bias is not None:<br>
1673         # fused op is marginally faster<br>
-&gt; 1674         ret = torch.addmm(bias, input, weight.t())<br>
1675     else:<br>
1676         output = input.matmul(weight.t())</p>
<p>RuntimeError: mat1 dim 1 must match mat2 dim 0<br>
I printed out the dimensions of my tensors and they are torch.Size([250, 10]) and torch.Size([250]) if that helps.</p>

ptrblck: <p>The error is raised in:</p>
<pre><code class="lang-python"> x = self.fc1(text)
</code></pre>
<p>Print the shape of <code>text</code> before passing it to <code>self.fc1</code> and make sure the <code>in_features</code> of the linear layer and the feature size of <code>text</code> are equal.</p>
<p>PS: you can post code snippets by wrapping them into three backticks ```, which makes debugging easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>