Laurinda Souza
Laurinda Souza

Reputation: 1337

“AttributeError: classificadorFinal' object has no attribute 'log_softmax” when trying to train a neural network using pytorch

I'm learning to use pytorch and I got an error that won't let me continue programming.

My code:

import torch.nn as nn
from skorch import NeuralNetClassifier #integracao com sklearn
from sklearn.model_selection import cross_val_score,GridSearchCV
from sklearn.preprocessing import LabelEncoder, MinMaxScaler
import torch
import torch.nn.functional as F
from torch import nn,optim

class classificadorFinal(nn.Module):
    def __init__(self, activation=F.tanh, neurons=16, initializer=torch.nn.init.uniform_, dropout=0.3):
        ##from melhores_parametros
        super().__init__()
        self.dense0 = nn.Linear(4, neurons)
        initializer(self.dense0.weight)
        self.activation0 = activation
        self.dense1 = nn.Linear(neurons, neurons)
        initializer(self.dense1.weight)
        self.activation1 = activation
        self.dense2 = nn.Linear(neurons, 3)

        self.dropout = nn.Dropout(dropout)

    def forward(self, X):
        X = self.dense0(X)
        X = self.activation0(X)
        X = self.dropout(X)
        X = self.dense1(X)
        X = self.activation1(X)
        X = self.dropout(X)
        X = self.dense2(X)
        return X


criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(classificador.parameters(), lr = 0.001, weight_decay = 0.0001)


#treino
for epoch in range(200):##from melhores_parametros

    running_loss = 0.
    running_accuracy = 0.

    for data in train_loader:
        inputs, labels = data

        optimizer.zero_grad()        

        outputs = classificadorFinal(inputs)

        loss = criterion(outputs, labels)###erro
        loss.backward()

        optimizer.step()

        running_loss += loss.item()

        ps = F.softmax(outputs)

        top_p, top_class = ps.topk(k = 1, dim = 1)

        equals = top_class == labels.view(*top_class.shape)

        running_accuracy += torch.mean(equals.type(torch.float))

    print('Época {:3d}: perda {:3.5f} - precisão {:3.5f}'.format(epoch + 1, running_loss/len(train_loader), running_accuracy/len(train_loader)))

The error occurs exactly on loss = criterion(outputs, labels):

AttributeError: 'classificadorFinal' object has no attribute 'log_softmax'

I found out this error is well known, but I did not understand the proposed solution:

disable aux_logits when the model is created aux_logits=False.

A little help, please!

Upvotes: 1

Views: 767

Answers (1)

Michael Jungo
Michael Jungo

Reputation: 32972

The outputs are not actually the output of the model, but rather the model itself. classificadorFinal is the class, calling it creates an object/instance of that class, and inputs will be the first argument to the __init__ method, namely activation.

# Creates an instance of the model
outputs = classificadorFinal(inputs)

You first have to create the model (an instance), which should be done once, then call that model with the inputs. It looks like you have already created the model before, as you are using classificador.parameters() for the optimiser, hence classificador is presumably the instance of the model. You need to call classificador (instance) not classificadorFinal (class) to create the outputs.

# Call the instance of the model, not the class 
outputs = classificador(inputs)

Upvotes: 1

Related Questions