[RUNTIME ERROR]: element 0 of tensors does not require grad and does not have a grad_fn

Hello World,

I’m working on a classification problem. I’m trying to classify URLs as malicious or benign. I’m implementing logistic regression for this problem, and here is my code:

class LogisticRegressionModel(nn.Module):
    def __init__(self, in_dim, num_classes):
        self.linear = nn.Linear(in_dim, num_classes)

    def forward(self, x):
        out = self.linear(x)
        return out
class Train(LogisticRegressionModel):

    def __init__(self, in_dim, num_classes, lr, batch_size):
        super().__init__(in_dim, num_classes)
        self.batch_size = batch_size
        self.learning_rate = lr
        self.input_layer_dim = in_dim
        self.output_layer_dim = num_classes
        self.criterion = nn.CrossEntropyLoss()
        self.model = LogisticRegressionModel(self.input_layer_dim, self.output_layer_dim)
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.model = self.model.to(self.device)
        self.optimizer = torch.optim.Adam(self.model.parameters(), lr = self.learning_rate)
#         self.optimizer = torch.optim.SGD(self.model.parameters(), lr = self.learning_rate)  
    def epochs(self, iterations, train_dataset, batch_size):
        epochs = int(iterations/(len(train_dataset)/batch_size))
        return epochs
    def train_model(self, training_data, n_iters):
        batch = self.batch_size
        epochs = self.epochs(n_iters, training_data, batch)
        training_data = torch.utils.data.DataLoader(dataset = training_data, batch_size = batch, shuffle = False)
        for epoch in range(epochs):
            for i, data in enumerate(training_data):

                X_train = data[:, :-1]
                Y_train = data[:, -1]

                if torch.cuda.is_available():
                    x = Variable(X_train).cuda()
                    y = Variable(Y_train).cuda()
                    x = X_train.float()
                    y = Y_train.type(torch.LongTensor)

                # Clear gradients
                # Forward propagation
                out = self.model(x).data
                # Calculate softmax and cross entropy loss
                loss = self.criterion(out, y)

                # Calculate gradient
                # Update parameters
batch_size = 5000
train_class = Train((training_set.shape[1]-1), number_of_target_labels+1, 0.001, batch_size)
temp = train_class.train_model(training_set, batch_size)

However, I get the following error:

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

I don’t know how to resolve this error. The data-set that I’m using does include NaNs, however I replace them with zeros. If you’d like to have a look at the Notebook and data-set, you can do so through this link:

You should not use .data on the output of your model. It only returns the matrix of you tensor not the other atttributes like grad_fn.

Use like this:

out = self.model(x)

By the way, you only need .data for initializing the weights and biases, not in training and eval methods.

1 Like

Yes it solved my problem. Thanks a lot Nikronic!

You’re welcome, good luck, mate.

1 Like