'Linear' object has no attribute 'squeeze'

Bhavishya_Pandit: <p><strong>Class Dataset</strong></p>
<pre><code class="lang-auto">from sklearn.model_selection import train_test_split
import numpy as np

class dataset(Dataset):
    def __init__(self):
        self.tf=TfidfVectorizer(max_df=0.99, min_df=0.005)
        self.x=self.tf.fit_transform(corpus).toarray()
        self.y=list(df.review)
        self.x_train,self.x_test,self.y_train,self.y_test=train_test_split(self.x,self.y,test_size=0.2)
        self.token2idx=self.tf.vocabulary_
        self.idx2token = {idx: token for token, idx in self.token2idx.items()}
        print(self.idx2token)
    
    def __getitem__(self,i):
        return self.x_train[i, :], self.y_train[i]
    
    def __len__(self):
        return self.x_train.shape[0]
</code></pre>
<p><strong>Classifier Class</strong></p>
<pre><code class="lang-auto">class classifier(nn.Module):
    def __init__(self,vocab_size,hidden1,hidden2):
        super(classifier,self).__init__()
        self.fc1=nn.Linear(vocab_size,hidden1)
        self.fc2=nn.Linear(hidden1,hidden2)
        self.fc3=nn.Linear(hidden2,1)
    def forward(self,inputs):
        x=F.relu(self.fc1(inputs.squeeze(1).float()))
        x=F.relu(self.fc2(x))
        return self.fc3
</code></pre>
<p><strong>Training Loop</strong></p>
<pre><code class="lang-auto">epochs=10

total=0
model.train()
for epoch in tqdm(range(epochs)):
    progress_bar=tqdm_notebook(train_loader,leave=False)
    losses=[]
    correct=0
    for inputs,target in progress_bar:
        model.zero_grad()
        output=model(inputs)
        loss=criterion(output.squeeze(),target.float())
        loss.backward()
        nn.utils.clip_grad_norm_(model.parameters(), 3)
        optim.step()
        correct += (output == target).float().sum()
        progress_bar.set_description(f'Loss: {loss.item():.3f}')
        losses.append(loss.item())
        total += 1
    epoch_loss = sum(losses) / total
    train_losses.append(epoch_loss)   
    tqdm.write(f'Epoch #{epoch + 1}\tTrain Loss: {epoch_loss:.3f}\tAccuracy: {correct/output.shape[0]}')
</code></pre>
<p><strong>Error</strong></p>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-65-f590a86ab4d3&gt; in &lt;module&gt;
     12         model.zero_grad()
     13         output=model(inputs)
---&gt; 14         loss=criterion(output.squeeze(),target.float())
     15         loss.backward()
     16         nn.utils.clip_grad_norm_(model.parameters(), 3)

~\Anaconda3\lib\site-packages\torch\nn\modules\module.py in __getattr__(self, name)
    592                 return modules[name]
    593         raise AttributeError("'{}' object has no attribute '{}'".format(
--&gt; 594             type(self).__name__, name))
    595 
    596     def __setattr__(self, name, value):

AttributeError: 'Linear' object has no attribute 'squeeze'
</code></pre>

Nikronic: <p>Hi,</p>
<p>In your network you are returning a <code>nn.Module</code> rather than a tensor. Probably, you want to return <code>self.fc3(x)</code>.</p>
<aside class="quote no-group quote-modified" data-username="Bhavishya_Pandit" data-post="1" data-topic="107135">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/bhavishya_pandit/48/32728_2.png" class="avatar"> Bhavishya_Pandit:</div>
<blockquote>
<p>def forward(self,inputs):<br>
x=F.relu(self.fc1(inputs.squeeze(1).float()))<br>
x=F.relu(self.fc2(x))<br>
return self.fc3    # HERE</p>
</blockquote>
</aside>
<p>Also, please format code blocks to enhance readability. And generally, I think it would be very nice to learn the tools/rules available on this forum by first reading guidelines.</p>
<p>Bests</p>

Bhavishya_Pandit: <p>Thanks for the solution <a class="mention" href="/u/nikronic">@Nikronic</a>, it worked. I had missed writing it by mistake.<br>
And sure would definitely read the guidelines of the forum, this is the first time I have asked a query on the forum. Nevertheless, thanks again <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

Nikronic: <p>Glad it worked!</p>
<p>I noticed that you have just joined the community and that’s why I mentioned guidelines. It makes situation better for all of us! Thanks for your interest!</p>