AttributeError: 'str' object has no attribute 'parameters' ,

111448: <p>Any help please</p>
<pre><code class="lang-auto">def training_model(model,model_type,train_dataloader,validation_dataloader,epochs):
  training_loss = []
  validation_loss = []
  validation_acc = []
  optimizer = AdamW(model.parameters(), lr=1e-3, correct_bias=True )
  total_steps = len(train_dataloader) * epochs
  warmup_steps = 0 
  scheduler = get_linear_schedule_with_warmup(optimizer, num_warmup_steps = warmup_steps,num_training_steps = total_steps)
  start_time = time.time()
  for epoch in range(epochs):
      total_train_loss = 0
      model.train()
      print('Epoch: {}'.format(epoch+1))
      for step, batch in enumerate(Bar(train_dataloader)):
          b_input_ids = batch[0].to(device)
          b_input_mask = batch[1].to(device)
          b_labels = batch[2].to(device)
          model.zero_grad()
          loss, logits = model(b_input_ids,token_type_ids=None, attention_mask=b_input_mask,labels=b_labels) #it computes loss function so it takes labels
          total_train_loss += loss.item()
          loss.backward()
          torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
          optimizer.step()
          scheduler.step()

      avg_train_loss = total_train_loss / len(train_dataloader)  

      model.eval()
      save_model(model,epoch+1,model_type)
      total_eval_accuracy = 0
      total_eval_loss = 0
      nb_eval_steps = 0
      print("Validation")
      for batch in validation_dataloader:
          b_input_ids = batch[0].to(device)
          b_input_mask = batch[1].to(device)
          b_labels = batch[2].to(device)

          with torch.no_grad():        
            (loss, logits) = model(b_input_ids,token_type_ids=None,attention_mask=b_input_mask,labels=b_labels)
            total_eval_loss += loss.item()
            logits = logits.detach().cpu().numpy()
            label_ids = b_labels.to('cpu').numpy()


      avg_val_loss = total_eval_loss / len(validation_dataloader)
      print("Validation Loss :", avg_val_loss )
      training_loss.append(avg_train_loss)
      validation_loss.append(avg_val_loss)

  elapsed_time = time.time() - start_time
  print("Done")
  print("Total training took :",str(datetime.timedelta(seconds=int(round((elapsed_time))))))
  return model,training_loss,validation_loss
</code></pre>
<pre><code class="lang-auto">
train_dataloader ,validation_dataloader = prepare_data(dataset_cbow,0.99,64) #dataset_cbow
</code></pre>
<pre><code class="lang-auto">model_embed,training_loss_embed,validation_loss_embed  = training_model(model_embed,"embed",train_dataloader,validation_dataloader,epochs=5)


</code></pre>
<pre><code class="lang-auto">AttributeError                            Traceback (most recent call last)
&lt;ipython-input-43-2fd6dc3ee641&gt; in &lt;module&gt;()
----&gt; 1 model_embed,training_loss_embed,validation_loss_embed  = training_model(model_embed,"embed",train_dataloader,validation_dataloader,epochs=5)

&lt;ipython-input-42-f3427a8de88a&gt; in training_model(model, model_type, train_dataloader, validation_dataloader, epochs)
      3   validation_loss = []
      4   validation_acc = []
----&gt; 5   optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, correct_bias=True )
      6   total_steps = len(train_dataloader) * epochs
      7   warmup_steps = 0

AttributeError: 'str' object has no attribute 'parameters'
</code></pre>

pascal_notsawo: <p>How can we help you?</p>
<p>It would be more interesting to document your problem.</p>

Abhilash_Srivastava: <aside class="quote no-group" data-username="111448" data-post="1" data-topic="107367">
<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/111448/48/32822_2.png" class="avatar"> 111448:</div>
<blockquote>
<p>optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, correct_bias=True )</p>
</blockquote>
</aside>
<p>Here the <code>model </code> is not created correctly.</p>
<p>Also, please format your code for easy reading.</p>

111448: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64.png" data-download-href="https://discuss.pytorch.org/uploads/default/f683d5393e222cc9938e2fd5d86fa0547024dd64" title="Screenshot (328)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_690x132.png" alt="Screenshot (328)" data-base62-sha1="zaLUjZnZge7ehpoiXH0NiWfn59G" width="690" height="132" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_690x132.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_1035x198.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_1380x264.png 2x" data-dominant-color="3A3B3B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (328)</span><span class="informations">1920×369 30.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

111448: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64.png" data-download-href="https://discuss.pytorch.org/uploads/default/f683d5393e222cc9938e2fd5d86fa0547024dd64" title="Screenshot (328)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_345x66.png" alt="Screenshot (328)" data-base62-sha1="zaLUjZnZge7ehpoiXH0NiWfn59G" width="345" height="66" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_345x66.png, https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_517x99.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/f/6/f683d5393e222cc9938e2fd5d86fa0547024dd64_2_690x132.png 2x" data-dominant-color="3A3B3B"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot (328)</span><span class="informations">1920×369 30.8 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

pascal_notsawo: <p>Help us to help you. It will be interesting that you show where you created <code>model_embed</code> and the code of the <code>training_model </code> function.</p>
<p>As I see <code>model_embed</code> is passed instead of the <code>model</code> parameter of the <code>training_model</code> function.<br>
Apparently <code>model_embed</code> is a string (<a href="https://github.com/python/cpython/blob/master/Lib/string.py" rel="noopener nofollow ugc"><code>str</code></a>) and not a subclass of <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="noopener nofollow ugc"><code>torch.nn.Module</code></a> (and therefore has no <code>parameters</code> attribute).</p>

111448: <p>I am sorry about the format i update it , but i try optimizer = torch.optim.AdamW(model.parameters(), lr=1e-3, correct_bias=True ) and still have same error</p>

pascal_notsawo: <p><a href="https://discuss.pytorch.org/t/attributeerror-str-object-has-no-attribute-parameters/107367/5">Help us to help you</a>. It is so simple.</p>
<p>It will be interesting that you show where you created <code>model_embed</code> and the code of the <code>training_model </code> function.</p>