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

islamaymansais: <p>Hello World,</p>
<p>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:</p>
<pre><code class="lang-auto">class LogisticRegressionModel(nn.Module):
    
    def __init__(self, in_dim, num_classes):
        super().__init__()
        self.linear = nn.Linear(in_dim, num_classes)

    def forward(self, x):
        out = self.linear(x)
        return out
</code></pre>
<pre><code class="lang-auto">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()
                    
                else:
                    x = X_train.float()
                    y = Y_train.type(torch.LongTensor)

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

                # Calculate gradient
                loss.backward()
                
                # Update parameters
                self.optimizer.step()
</code></pre>
<pre><code class="lang-auto">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)
</code></pre>
<p>However, I get the following error:</p>
<pre><code class="lang-auto">RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<p>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:</p>
<aside class="onebox whitelistedgeneric">
  <header class="source">
      <img src="https://github.githubassets.com/favicon.ico" class="site-icon" width="16" height="16">
      <a href="https://github.com/islamaymansais/Malicious-URL-Classifier" target="_blank" rel="nofollow noopener">GitHub</a>
  </header>
  <article class="onebox-body">
    <img src="https://avatars0.githubusercontent.com/u/25418859?s=400&amp;amp;v=4" class="thumbnail onebox-avatar" width="60" height="60">

<h3><a href="https://github.com/islamaymansais/Malicious-URL-Classifier" target="_blank" rel="nofollow noopener">islamaymansais/Malicious-URL-Classifier</a></h3>

<p>Contribute to islamaymansais/Malicious-URL-Classifier development by creating an account on GitHub.</p>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>


Nikronic: <p>Hi,<br>
You should not use <code>.data</code> on the output of your model. It only returns the matrix of you tensor not the other atttributes like <code>grad_fn</code>.</p>
<aside class="quote no-group" data-username="islamaymansais" data-post="1" data-topic="50893">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/ee59a6/48.png" class="avatar"> islamaymansais:</div>
<blockquote>
<p>out = self.model(x).data</p>
</blockquote>
</aside>
<p>Use like this:</p>
<pre data-code-wrap="python"><code class="lang-python">out = self.model(x)

</code></pre>
<p>By the way, you only need <code>.data</code> for initializing the weights and biases, not in training and eval methods.</p>

islamaymansais: <p>Yes it solved my problem. Thanks a lot Nikronic!</p>

Nikronic: <p>You’re welcome, good luck, mate.</p>