'Linear' object has no attribute 'squeeze'

Class Dataset

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]

Classifier Class

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

Training Loop

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]}')

Error

AttributeError                            Traceback (most recent call last)
<ipython-input-65-f590a86ab4d3> in <module>
     12         model.zero_grad()
     13         output=model(inputs)
---> 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(
--> 594             type(self).__name__, name))
    595 
    596     def __setattr__(self, name, value):

AttributeError: 'Linear' object has no attribute 'squeeze'

Hi,

In your network you are returning a nn.Module rather than a tensor. Probably, you want to return self.fc3(x).

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.

Bests

1 Like

Thanks for the solution @Nikronic, it worked. I had missed writing it by mistake.
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 :slight_smile:

1 Like

Glad it worked!

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!

2 Likes