Implementing Xlnet classification class

sripathisony_sony: <p>Could you please let me know where i am going wrong.</p>
<pre><code class="lang-auto"> def __init__(self, n_classes):
   super(SentimentClassifier, self).__init__()
   self.xlnet = XLNetModel.from_pretrained(PRE_TRAINED_MODEL_NAME)
   self.drop = nn.Dropout(p=0.3)
   self.out = nn.Linear(self.xlnet.config.hidden_size, n_classes)
 def forward(self, input_ids, attention_mask):
   _, pooled_output = self.xlnet(self,
       input_ids=input_ids,
       attention_mask=attention_mask,
   )
   output = self.drop(pooled_output)
   return self.out(output)
class Classification(Dataset):
    def __init__(self, texts, labels, tokenizer, max_len):
       self.texts = texts
       self.labels = labels
       self.tokenizer = tokenizer
       self.max_len = max_len
   
   def __len__(self):
       return len(self.texts)
   
   def __getitem__(self, item):
       text = str(self.texts[item])
       label = self.labels[item]

       encoding = self.tokenizer.encode_plus(
       text,
       add_special_tokens=True,
       max_length=self.max_len,
       return_token_type_ids=False,
       pad_to_max_length=False,
       return_attention_mask=True,
       return_tensors='pt',
       )

       input_ids = pad_sequences(encoding['input_ids'], maxlen=MAX_LEN, dtype=torch.Tensor ,truncating="post",padding="post")
       input_ids = input_ids.astype(dtype = 'int64')
       input_ids = torch.tensor(input_ids) 

       attention_mask = pad_sequences(encoding['attention_mask'], maxlen=MAX_LEN, dtype=torch.Tensor ,truncating="post",padding="post")
       attention_mask = attention_mask.astype(dtype = 'int64')
       attention_mask = torch.tensor(attention_mask)       

       return {
       'review_text': text,
       'input_ids': input_ids,
       'attention_mask': attention_mask.flatten(),
       'labels': torch.tensor(label, dtype=torch.long)
       }
def train_epoch(
 model,
 data_loader,
 loss_fn,
 optimizer,
 device,
 scheduler,
 n_examples
):
 model = model.train()
 losses = []
 correct_predictions = 0
 for d in data_loader:
   input_ids = d["input_ids"].to(device)
   attention_mask = d["attention_mask"].to(device)
   labels = d["labels"].to(device)
   outputs = model(
     input_ids=input_ids,
     attention_mask=attention_mask)
   _, preds = torch.max(outputs[1], dim=1)
   loss = loss_fn(outputs, lables)
   correct_predictions += torch.sum(preds == labels)
   losses.append(loss.item())
   loss.backward()
   nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
   optimizer.step()
   scheduler.step()
   optimizer.zero_grad()
   
 return correct_predictions.double() / n_examples, np.mean(losses)
train_acc, train_loss = train_epoch(
   model,
   train_data_loader,
   loss_fn,
   optimizer,
   device,
   scheduler,
   len(df_train)
 )
print(f'Train loss {train_loss} accuracy {train_acc}') ```


error:TypeError: forward() got multiple values for argument 'input_ids'</code></pre>

Dwight_Foster: <p>Could you print out input_ids and its shape in the train loop?</p>

sripathisony_sony: <p>thanks, i am getting below error  trying to print shape<br>
def train_epoch(<br>
model,<br>
data_loader,<br>
loss_fn,<br>
optimizer,<br>
device,<br>
scheduler,<br>
n_examples<br>
):<br>
model = xlnet_model.train()<br>
losses = []<br>
correct_predictions = 0<br>
for d in data_loader:<br>
input_ids = d[“input_ids”].reshape(4,512).to(device)<br>
print(d[‘input_ids’].shape)<br>
attention_mask = d[“attention_mask”].to(device)<br>
labels = d[“labels”].to(device)<br>
outputs = xlnet_model(input_ids=input_ids, attention_mask=attention_mask)</p>
<pre><code>_, preds = torch.max(outputs, dim=1)
loss = loss_fn(outputs, labels)
correct_predictions += torch.sum(preds == labels)
losses.append(loss.item())
loss.backward()
nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
optimizer.step()
scheduler.step()
optimizer.zero_grad()
</code></pre>
<p>return correct_predictions.double() / n_examples, np.mean(losses)</p>
<p>error:ValueError: not enough values to unpack (expected 2, got 1)</p>

Dwight_Foster: <p>which line is this error on</p>

sripathisony_sony: <p>at this line outputs = xlnet_model(input_ids=input_ids, attention_mask=attention_mask)</p>
<p>I am using xlnet pertained model.</p>

sripathisony_sony: <p>ValueError                                Traceback (most recent call last)</p>
<p> in ()<br>
6     device,<br>
7     scheduler,<br>
----&gt; 8     len(df_train)<br>
9   )<br>
10 print(f’Train loss {train_loss} accuracy {train_acc}’)</p>
<p>2 frames</p>
<p> in forward(self, input_ids, attention_mask)<br>
9     _, pooled_output = self.xlnet(<br>
10       input_ids=input_ids,<br>
—&gt; 11       attention_mask=attention_mask)<br>
12     output = self.drop(pooled_output)<br>
13     return self.out(output)</p>
<p>ValueError: not enough values to unpack (expected 2, got 1)</p>

Dwight_Foster: <p>Ok well in you original code in this line there is an extra comma</p>
<pre><code class="lang-auto">     attention_mask=attention_mask, &lt;---- here
   )
</code></pre>
<p>but I don’t think that is the problem. What is the shape of your attention mask and what github are you using for the xlnet?</p>

sripathisony_sony: <p><a class="mention" href="/u/dwight_foster">@Dwight_Foster</a><br>
input_id and Attention mask shape size:<br>
torch.Size([4, 160])<br>
torch.Size([4, 160])</p>

Dwight_Foster: <p><strong>Ok what repository are you using for the xlnet model?</strong> The error means that for some reason either input ids or attention masks should have two different things in it and you are only passing one tensor for each.</p>

sripathisony_sony: <p>I didn’t get , what you are saying, Could you please explain to me<br>
link:<a href="https://curiousily.com/posts/sentiment-analysis-with-bert-and-hugging-face-using-pytorch-and-python/" rel="noopener nofollow ugc">https://curiousily.com/posts/sentiment-analysis-with-bert-and-hugging-face-using-pytorch-and-python/</a></p>