The error is IndexError: Target 2 is out of bounds. I need guidance please

Rexedoziem: <p>class DeBERTadataset:<br>
def <strong>init</strong>(self, texts, target):<br>
self.tokenizer = config.TOKENIZER<br>
self.max_len = config.max_len<br>
self.texts = texts<br>
self.target = target</p>
<pre><code>def __len__(self):
    return len(self.texts)

def __getitem__(self, index):
    texts = str(self.texts[index])
    texts = ''.join(texts.split())
    target = self.target[index]


    inputs = self.tokenizer.encode_plus(
        texts,
        None,
        add_special_tokens = True,
        max_length = self.max_len,
        padding = 'max_length',
        truncation = True
    )


    resp = {
        'ids': torch.tensor(inputs['input_ids'], dtype=torch.long), 
        'mask': torch.tensor(inputs['attention_mask'], dtype=torch.long),
        'token_type_ids': torch.tensor(inputs['token_type_ids'], dtype=torch.long),
        'target': torch.tensor(self.target[index], dtype=torch.long)
    
    }

    return resp
</code></pre>
<p>class BERTModel(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(BERTModel, self).<strong>init</strong>()<br>
self.bert = BertModel.from_pretrained(‘bert-base-uncased’, return_dict=False)<br>
self.dropout = nn.Dropout(0.3)<br>
self.classified = nn.Linear(768, 2)</p>
<pre><code>def forward(self, ids, mask, token_type_ids):
    _, pooled_output = self.bert(
        input_ids = ids,
        attention_mask = mask,
        token_type_ids = token_type_ids
    )

    pooled_output = self.dropout(pooled_output)
    outputs = self.classified(pooled_output)
    return outputs
</code></pre>
<p>import torch.nn as nn<br>
from tqdm import tqdm<br>
import torch</p>
<p>def loss_fn(outputs, target):<br>
return nn.CrossEntropyLoss()(outputs, target)</p>
<p>def train_fx(data_loader, model, optimizer, scheduler, device):<br>
model.train()</p>
<pre><code>train_loss = 0
for bi, d in tqdm(enumerate(data_loader)):
    ids = d['ids']
    mask = d['mask']
    token_type_ids = d['token_type_ids']
    target = d['target']
# putting them into the device

    ids = ids.to(device, dtype=torch.long)
    mask = mask.to(device, dtype=torch.long)
    token_type_ids = token_type_ids.to(device, dtype=torch.long)
    target = target.to(device, dtype=torch.long)
        
    optimizer.zero_grad()
    outputs = model(
        ids = ids,
        mask = mask,
        token_type_ids = token_type_ids
    )
    
    loss = loss_fn(outputs, target)
    loss.backward()
    optimizer.step()
    scheduler.step()
    train_loss += loss.item()
train_loss = train_loss / len(data_loader)

print(f'Train loss: {train_loss:.3f}')
</code></pre>
<p>def eval_fx(data_loader, model, device):<br>
model.eval()<br>
valid_loss = 0</p>
<pre><code>#final_target = []
#final_outputs = []
with torch.no_grad():
    for bi, d in tqdm(enumerate(data_loader)):
        ids = d['ids']
        mask = d['mask']
        token_type_ids = d['token_type_ids']
        target = d['target']
    # putting them into the device

        ids = ids.to(device, dtype=torch.long)
        mask = mask.to(device, dtype=torch.long)
        token_type_ids = token_type_ids.to(device, dtype=torch.long)
        target = target.to(device, dtype=torch.long)
            
        outputs = model(
            ids = ids,
            mask = mask,
            token_type_ids = token_type_ids
        )
        loss = loss_fn(outputs, target)

        valid_loss += loss.item()
    valid_loss = valid_loss / len(data_loader)

print(f'valid loss: {valid_loss:.3f}')
</code></pre>
<p>def accuracy_metrics(self, outputs, target):<br>
outputs = torch.argmax(outputs).cpu().detach().numpy() # Due to the linear layer in our feedforward function<br>
target = target.cpu().detach().numpy()<br>
return {‘Accuracy’: metrics.accuracy_score(outputs, target)}</p>
<p>from tqdm import tqdm<br>
best_accuracy = 0<br>
for epoch in range(config.epochs):<br>
train_fx(train_data_loader, model, optimizer,scheduler, device)<br>
outputs, target = eval_fx(valid_data_loader, model, device)<br>
accuracy = accuracy_metrics(outputs, target)<br>
print(f’Accuracy score ----- {accuracy}')<br>
if accuracy &gt; best_accuracy:<br>
torch.save(model.state_dict(), config.model_path)<br>
best_accuracy = accuracy_metrics</p>

ptrblck: <p>The error is raised in the loss calculation since your target contains values, which are out-of-bounds.<br>
Based on your code, it seems you are working on a binary classification and treat it as a 2-class multi-class classification using <code>nn.CrossEntropyLoss</code>. In this case the target should be a <code>LongTensor</code> and contain class indices in the range <code>[0, nb_classes-1]</code>, so <code>[0, 1]</code> in your case.<br>
A target value of <code>2</code> is thus invalid and you would have to check why it’s there (and remove it).</p>

Rexedoziem: <p>I’m working on a multi class classification problem, where the target has 3 classes(effective, ineffective, adequate). I changed the num_classes from 2 to 3. Where it’ll have 0,1,2, and the problem was solved.<br>
class BERTModel(nn.Module):<br>
def <strong>init</strong>(self):<br>
super(BERTModel, self).<strong>init</strong>()<br>
self.bert = BertModel.from_pretrained(‘bert-base-uncased’, return_dict=False)<br>
self.dropout = nn.Dropout(0.3)<br>
self.classified = nn.Linear(768, 2)<br>
I changed the 2 in the self.classified to a 3 and it worked. <img src="https://discuss.pytorch.org/images/emoji/apple/grinning.png?v=12" title=":grinning:" class="emoji" alt=":grinning:" loading="lazy" width="20" height="20"></p>

Rexedoziem: <p>I’m having an issue: cannot unpack non-iterable float object. This is my code at the top of this comment. I’ve checked the shape of my outputs and target for both training and validation loops and it gave me this…….<br>
Training:<br>
Outputs - torch.size([4, 3])<br>
Target - torch.size([4])</p>
<p>Validation:<br>
Outputs - torch.size([1, 3])<br>
Target - torch.size([1])</p>
<p>So I don’t know why it’s showing me errors in my validation loop.</p>

ptrblck: <p>Which line of code is raising this issue?<br>
Could you post the complete stacktrace, please?</p>

Rexedoziem: <p>def eval_fx(data_loader, model, device):<br>
model.eval()<br>
valid_loss = 0</p>
<pre><code class="lang-auto">#final_target = []
#final_outputs = []
with torch.no_grad():
    for bi, d in tqdm(enumerate(data_loader)):
        ids = d['ids']
        mask = d['mask']
        token_type_ids = d['token_type_ids']
        target = d['target']
    # putting them into the device

        ids = ids.to(device, dtype=torch.long)
        mask = mask.to(device, dtype=torch.long)
        token_type_ids = token_type_ids.to(device, dtype=torch.long)
        target = target.to(device, dtype=torch.long)
            
        outputs = model(
            ids = ids,
            mask = mask,
            token_type_ids = token_type_ids
        )
           final_target.extend(target.cpu().detach.numpy.tolist())
final_outputs.extend(torch.argmax(outputs).cpu().detach().numpy().tolist())

return final_target, final_outputs
</code></pre>

Rexedoziem: <p>best_accuracy = 0<br>
for epoch in range(config.epochs):<br>
train_fx(train_data_loader, model, optimizer,scheduler, device)<br>
outputs, target = eval_fx(valid_data_loader, model, device)<br>
accuracy = accuracy_metrics(outputs, target)<br>
print(f’Accuracy score ----- {accuracy}')<br>
if accuracy &gt; best_accuracy:<br>
torch.save(model.state_dict(), config.model_path)<br>
best_accuracy = accuracy_metrics</p>
<p>The problem lies here : outputs, target = eval_fx(valid_data_loader, model, device)</p>

ptrblck: <p>Based on the stacktrace I guess <code>eval_fx</code> is returning a single <code>float</code> value, while you are expecting two returned objects:</p>
<pre><code class="lang-python">def eval_fx():
    return 1.

outputs, target = eval_fx()
# TypeError: cannot unpack non-iterable float object
</code></pre>

Rexedoziem: <p>Yes, I am expecting a double value instead of single</p>

Rexedoziem: <p>How do I go about it. And what is the remedy for this error?</p>

ptrblck: <p>Check the method definition and <code>return</code> two objects instead of one.</p>

srishti-git1110: <p>Hi,<br>
From what I can understand, you are trying to store targets (<code>final_target</code>) and final outputs (<code>final_outputs</code>) corresponding to each batch of your dataset in the corresponding lists.</p>
<p>Now, according to this, these lines aren’t indented properly in the code you’ve shown here -</p>
<p><code>final_target.extend(target.cpu().detach.numpy.tolist())</code><br>
<code>final_outputs.extend(torch.argmax(outputs).cpu().detach().numpy().tolist())</code><br>
<code>return final_target, final_outputs</code></p>
<p>Can you try this:</p>
<pre><code class="lang-auto">def eval_fx(data_loader, model, device):
  model.eval()
  valid_loss = 0

  final_target = []
  final_outputs = []
  with torch.no_grad():
    for bi, d in tqdm(enumerate(data_loader)):
      ids = d['ids']
      mask = d['mask']
      token_type_ids = d['token_type_ids']
      target = d['target']
    

      ids = ids.to(device, dtype=torch.long)
      mask = mask.to(device, dtype=torch.long)
      token_type_ids = token_type_ids.to(device, dtype=torch.long)
      target = target.to(device, dtype=torch.long)
            
      outputs = model(ids = ids,
                      mask = mask,
                      token_type_ids = token_type_ids
                      )
      final_target.extend(target.cpu().detach.numpy.tolist())
      final_outputs.extend(torch.argmax(outputs).cpu().detach().numpy().tolist())

  return final_target, final_outputs
</code></pre>

Harshal_Dharpure: <pre><code class="lang-auto">
class Classifier(pl.LightningModule):

  def __init__(self):
    super().__init__()
    self.MFB = MFB(512,768,True,256,64,0.1)
    self.fin_y_shape = torch.nn.Linear(768,512)
    self.fin_old = torch.nn.Linear(64,2)
    self.fin = torch.nn.Linear(16 * 768, 64)
    self.fin_inten = torch.nn.Linear(64,5)
    self.fin_e1 = torch.nn.Linear(64,2)
    self.fin_e2 = torch.nn.Linear(64,2)
    self.fin_e3 = torch.nn.Linear(64,2)
    self.fin_e4 = torch.nn.Linear(64,2)
    self.fin_e5 = torch.nn.Linear(64,2)
    self.fin_e6 = torch.nn.Linear(64,2)
    self.fin_e7 = torch.nn.Linear(64,2)
    self.fin_e8 = torch.nn.Linear(64,2)
    self.fin_e9 = torch.nn.Linear(64,2)
    


    self.validation_step_outputs = []
    self.test_step_outputs = []

  def forward(self, x,y,rag):
      x_,y_,rag_ = x,y,rag
      print("x.shape", x.shape)
      z = self.MFB(torch.unsqueeze(y, axis=1), torch.unsqueeze(x, axis=1))
      #cross_attention= (rag and  x/y)
      z_new = torch.squeeze(z, dim=1)
      c_inten = self.fin_inten(z_new)
      c_e1 = self.fin_e1(z_new)
      c_e2 = self.fin_e2(z_new)
      c_e3 = self.fin_e3(z_new)
      c_e4 = self.fin_e4(z_new)
      c_e5 = self.fin_e5(z_new)
      c_e6 = self.fin_e6(z_new)
      c_e7 = self.fin_e7(z_new)
      c_e8 = self.fin_e8(z_new)
      c_e9 = self.fin_e9(z_new)
      c = self.fin_old(z_new)


      output = torch.log_softmax(c, dim=1)
      c_inten = torch.log_softmax(c_inten, dim=1)
      c_e1 = torch.log_softmax(c_e1, dim=1)
      c_e2 = torch.log_softmax(c_e2, dim=1)
      c_e3 = torch.log_softmax(c_e3, dim=1)
      c_e4 = torch.log_softmax(c_e4, dim=1)

      c_e5 = torch.log_softmax(c_e5, dim=1)
      c_e6 = torch.log_softmax(c_e6, dim=1)
      c_e7 = torch.log_softmax(c_e7, dim=1)

      c_e8 = torch.log_softmax(c_e8, dim=1)
      c_e9 = torch.log_softmax(c_e9, dim=1)


      return output,c_e1,c_e2,c_e3,c_e4,c_e5,c_e6,c_e7,c_e8,c_e9,c_inten

  def cross_entropy_loss(self, logits, labels):
    return F.nll_loss(logits, labels)

  def training_step(self, train_batch, batch_idx):
      #lab,txt,rag,img,name,per,iro,alli,ana,inv,meta,puns,sat,hyp= train_batch
      lab,txt,rag,img,name,e1,e2,e3,e4,e5,e6,e7,e8,e9,intensity = train_batch
      #logit_offen,a,b,c,d,e,f,g,h,i,logit_inten_target= self.forward(txt,img,rag)
      lab = train_batch[lab]
      #print(lab)
      name= train_batch[name]
      txt = train_batch[txt]
      rag = train_batch[rag]
      img = train_batch[img]
      e1 = train_batch[e1]
      e2 = train_batch[e2]
      e3 = train_batch[e3]
      e4 = train_batch[e4]
      e5 = train_batch[e5]
      e6 = train_batch[e6]
      e7 = train_batch[e7]
      e8 = train_batch[e8]
      e9 = train_batch[e9]
      intensity = train_batch[intensity]
      # per = train_batch[per]
      # iro= train_batch[iro]
      # alli = train_batch[alli]
      # ana = train_batch[ana]
      # inv = train_batch[inv]
      # meta = train_batch[meta]
      # puns = train_batch[puns]
      # sat = train_batch[sat]
      # hyp = train_batch[hyp]

      logit_offen,a,b,c,d,e,f,g,h,i,logit_inten_target= self.forward(txt,img,rag)

      loss1 = self.cross_entropy_loss(logit_offen, lab)
      loss4 = self.cross_entropy_loss(a, e1)
      loss5 = self.cross_entropy_loss(b, e2)
      loss6 = self.cross_entropy_loss(c, e3)
      loss7 = self.cross_entropy_loss(d, e4)
      loss8 = self.cross_entropy_loss(e, e5)
      loss9 = self.cross_entropy_loss(f, e6)
      loss10 = self.cross_entropy_loss(g, e7)
      loss11 = self.cross_entropy_loss(h, e8)
      loss12 = self.cross_entropy_loss(i, e9)
      loss17 = self.cross_entropy_loss(logit_inten_target, intensity)
      # loss2 = self.cross_entropy_loss(a,per)
      # loss3 = self.cross_entropy_loss(b,iro)
      # loss4 = self.cross_entropy_loss(c, alli)
      # loss5 = self.cross_entropy_loss(d,ana)
      # loss6 = self.cross_entropy_loss(e,inv)
      # loss7 = self.cross_entropy_loss(f,meta)
      # loss8 = self.cross_entropy_loss(g,puns)
      # loss9 = self.cross_entropy_loss(h,sat)
      # loss10 = self.cross_entropy_loss(i,hyp)

      loss = loss1 + loss4 + loss5 + loss6 + loss7 + loss8 +loss9 + loss10 +loss11 +loss12

      self.log('train_loss', loss)
      return loss


  def validation_step(self, val_batch, batch_idx):
      #lab,txt,rag,img,name,per,iro,alli,ana,inv,meta,puns,sat,hyp = val_batch
      lab,txt,rag,img,name,e1,e2,e3,e4,e5,e6,e7,e8,e9,intensity= val_batch
      lab = val_batch[lab]
      #print(lab)
      txt = val_batch[txt]
      rag = val_batch[rag]
      img = val_batch[img]
      name = val_batch[name]
      e1 = val_batch[e1]
      e2 = val_batch[e2]
      e3 = val_batch[e3]
      e4 = val_batch[e4]
      e5 = val_batch[e5]
      e6 = val_batch[e6]
      e7 = val_batch[e7]
      e8 = val_batch[e8]
      e9 = val_batch[e9]
      intensity = val_batch[intensity]
      # per = val_batch[per]
      # iro = val_batch[iro]
      # alli = val_batch[alli]
      # ana = val_batch[ana]
      # inv = val_batch[inv]
      # meta = val_batch[meta]
      # puns = val_batch[puns]
      # sat = val_batch[sat]
      # hyp = val_batch[hyp]


      logits,a,b,c,d,e,f,g,h,i,inten = self.forward(txt,img,rag)


      logits=logits.float()
    

      tmp = np.argmax(logits.detach().cpu().numpy(),axis=-1)
      loss = self.cross_entropy_loss(logits, lab)
      lab = lab.detach().cpu().numpy()
      self.log('val_acc', accuracy_score(lab,tmp))
      self.log('val_roc_auc',roc_auc_score(lab,tmp))
      self.log('val_loss', loss)
      tqdm_dict = {'val_acc': accuracy_score(lab,tmp)}
      self.validation_step_outputs.append({'progress_bar': tqdm_dict,'val_f1 offensive': f1_score(lab,tmp,average='macro')})

      return {
                'progress_bar': tqdm_dict,
      'val_f1 offensive': f1_score(lab,tmp,average='macro')
      }

  def on_validation_epoch_end(self):
    outs = []
    outs14=[]
    for out in self.validation_step_outputs:
       outs.append(out['progress_bar']['val_acc'])
       outs14.append(out['val_f1 offensive'])
    self.log('val_acc_all_offn', sum(outs)/len(outs))
    self.log('val_f1 offensive', sum(outs14)/len(outs14))
    print(f'***val_acc_all_offn at epoch end {sum(outs)/len(outs)}****')
    print(f'***val_f1 offensive at epoch end {sum(outs14)/len(outs14)}****')
    self.validation_step_outputs.clear()

  def test_step(self, batch, batch_idx):
      lab,txt,rag,img,name,e1,e2,e3,e4,e5,e6,e7,e8,e9,intensity= batch
      #lab,txt,rag,img,name,per,iro,alli,ana,inv,meta,puns,sat,hyp= batch

      lab = batch[lab]
      #print(lab)
      rag = batch[rag]

      txt = batch[txt]
      img = batch[img]
      name = batch[name]
      e1 = batch[e1]
      e2 = batch[e2]
      e3 = batch[e3]
      e4 = batch[e4]
      e5 = batch[e5]
      e6 = batch[e6]
      e7 = batch[e7]
      e8 = batch[e8]
      e9 = batch[e9]
      intensity = batch[intensity]
      # per = batch[per]
      # iro = batch[iro]
      # alli = batch[alli]
      # ana = batch[ana]
      # inv = batch[inv]
      # meta = batch[meta]
      # puns = batch[puns]
      # sat = batch[sat]
      # hyp = batch[hyp]

      logits,a,b,c,d,e,f,g,h,i,inten= self.forward(txt,img,rag)
      logits = logits.float()
      tmp = np.argmax(logits.detach().cpu().numpy(force=True),axis=-1)
      loss = self.cross_entropy_loss(logits, lab)
      lab = lab.detach().cpu().numpy()
      self.log('test_acc', accuracy_score(lab,tmp))
      self.log('test_roc_auc',roc_auc_score(lab,tmp))
      self.log('test_loss', loss)
      tqdm_dict = {'test_acc': accuracy_score(lab,tmp)}
      self.test_step_outputs.append({'progress_bar': tqdm_dict,'test_acc': accuracy_score(lab,tmp), 'test_f1_score': f1_score(lab,tmp,average='macro')})
      return {
                'progress_bar': tqdm_dict,
                'test_acc': accuracy_score(lab,tmp),
                'test_f1_score': f1_score(lab,tmp,average='macro')
      }
  def on_test_epoch_end(self):
      # OPTIONAL
      outs = []
      outs1,outs2,outs3,outs4,outs5,outs6,outs7,outs8,outs9,outs10,outs11,outs12,outs13,outs14 = \
      [],[],[],[],[],[],[],[],[],[],[],[],[],[]
      for out in self.test_step_outputs:
        outs.append(out['test_acc'])
        outs2.append(out['test_f1_score'])
      self.log('test_acc', sum(outs)/len(outs))
      self.log('test_f1_score', sum(outs2)/len(outs2))
      self.test_step_outputs.clear()

  def configure_optimizers(self):
    # optimizer = torch.optim.Adam(self.parameters(), lr=3e-2)
    optimizer = torch.optim.Adam(self.parameters(), lr=1e-5)

    return optimizer


"""
Main Model:
Initialize
Forward Pass
Training Step
Validation Step
Testing Step

Pp
"""

class HmDataModule(pl.LightningDataModule):

  def setup(self, stage):
    self.hm_train = t_p
    self.hm_val = v_p
    # self.hm_test = test
    self.hm_test = te_p

  def train_dataloader(self):
    return DataLoader(self.hm_train, batch_size=10, drop_last=True)

  def val_dataloader(self):
    return DataLoader(self.hm_val, batch_size=10, drop_last=True)

  def test_dataloader(self):
    return DataLoader(self.hm_test, batch_size=10, drop_last=True)

data_module = HmDataModule()
checkpoint_callback = ModelCheckpoint(
     monitor='val_acc_all_offn',
     dirpath='mrinal/',
     filename='epoch{epoch:02d}-val_f1_all_offn{val_acc_all_offn:.2f}',
     auto_insert_metric_name=False,
     save_top_k=1,
    mode="max",
 )
all_callbacks = []
all_callbacks.append(checkpoint_callback)
# train
from pytorch_lightning import seed_everything
seed_everything(42, workers=True)
hm_model = Classifier()
gpus=1
#if torch.cuda.is_available():gpus=0
trainer = pl.Trainer(deterministic=True,max_epochs=2,precision=16,callbacks=all_callbacks)
trainer.fit(hm_model, data_module)
</code></pre>
<h2><a name="infolightning_fabricutilitiesseedseed-set-to-42-infopytorch_lightningutilitiesrank_zerousing-bfloat16-automatic-mixed-precision-amp-infopytorch_lightningutilitiesrank_zerogpu-available-false-used-false-infopytorch_lightningutilitiesrank_zerotpu-available-false-using-0-tpu-cores-infopytorch_lightningutilitiesrank_zeroipu-available-false-using-0-ipus-infopytorch_lightningutilitiesrank_zerohpu-available-false-using-0-hpus-infopytorch_lightningcallbacksmodel_summary-name-type-params-1" class="anchor" href="#infolightning_fabricutilitiesseedseed-set-to-42-infopytorch_lightningutilitiesrank_zerousing-bfloat16-automatic-mixed-precision-amp-infopytorch_lightningutilitiesrank_zerogpu-available-false-used-false-infopytorch_lightningutilitiesrank_zerotpu-available-false-using-0-tpu-cores-infopytorch_lightningutilitiesrank_zeroipu-available-false-using-0-ipus-infopytorch_lightningutilitiesrank_zerohpu-available-false-using-0-hpus-infopytorch_lightningcallbacksmodel_summary-name-type-params-1"></a>INFO:lightning_fabric.utilities.seed:Seed set to 42<br>
INFO:pytorch_lightning.utilities.rank_zero:Using bfloat16 Automatic Mixed Precision (AMP)<br>
INFO:pytorch_lightning.utilities.rank_zero:GPU available: False, used: False<br>
INFO:pytorch_lightning.utilities.rank_zero:TPU available: False, using: 0 TPU cores<br>
INFO:pytorch_lightning.utilities.rank_zero:IPU available: False, using: 0 IPUs<br>
INFO:pytorch_lightning.utilities.rank_zero:HPU available: False, using: 0 HPUs<br>
INFO:pytorch_lightning.callbacks.model_summary:<br>
| Name        | Type   | Params</h2>
<h2><a name="h-0-mfb-mfb-210-m-1-fin_y_shape-linear-393-k-2-fin_old-linear-130-3-fin-linear-786-k-4-fin_inten-linear-325-5-fin_e1-linear-130-6-fin_e2-linear-130-7-fin_e3-linear-130-8-fin_e4-linear-130-9-fin_e5-linear-130-10-fin_e6-linear-130-11-fin_e7-linear-130-12-fin_e8-linear-130-13-fin_e9-linear-130-2" class="anchor" href="#h-0-mfb-mfb-210-m-1-fin_y_shape-linear-393-k-2-fin_old-linear-130-3-fin-linear-786-k-4-fin_inten-linear-325-5-fin_e1-linear-130-6-fin_e2-linear-130-7-fin_e3-linear-130-8-fin_e4-linear-130-9-fin_e5-linear-130-10-fin_e6-linear-130-11-fin_e7-linear-130-12-fin_e8-linear-130-13-fin_e9-linear-130-2"></a>0  | MFB         | MFB    | 21.0 M<br>
1  | fin_y_shape | Linear | 393 K<br>
2  | fin_old     | Linear | 130<br>
3  | fin         | Linear | 786 K<br>
4  | fin_inten   | Linear | 325<br>
5  | fin_e1      | Linear | 130<br>
6  | fin_e2      | Linear | 130<br>
7  | fin_e3      | Linear | 130<br>
8  | fin_e4      | Linear | 130<br>
9  | fin_e5      | Linear | 130<br>
10 | fin_e6      | Linear | 130<br>
11 | fin_e7      | Linear | 130<br>
12 | fin_e8      | Linear | 130<br>
13 | fin_e9      | Linear | 130</h2>
<h2><a name="h-222-m-trainable-params-0-non-trainable-params-222-m-total-params-88745-total-estimated-model-params-size-mb-xshape-torchsize10-768-xshape-torchsize10-768-val_acc_all_offn-at-epoch-end-03-val_f1-offensive-at-epoch-end-029292929292929293-usrlocallibpython310dist-packagespytorch_lightningloopsfit_looppy298-the-number-of-training-batches-34-is-smaller-than-the-logging-interval-trainerlog_every_n_steps50-set-a-lower-value-for-log_every_n_steps-if-you-want-to-see-logs-for-the-training-epoch-epoch-0-0-034-0000-its-xshape-torchsize10-768-3" class="anchor" href="#h-222-m-trainable-params-0-non-trainable-params-222-m-total-params-88745-total-estimated-model-params-size-mb-xshape-torchsize10-768-xshape-torchsize10-768-val_acc_all_offn-at-epoch-end-03-val_f1-offensive-at-epoch-end-029292929292929293-usrlocallibpython310dist-packagespytorch_lightningloopsfit_looppy298-the-number-of-training-batches-34-is-smaller-than-the-logging-interval-trainerlog_every_n_steps50-set-a-lower-value-for-log_every_n_steps-if-you-want-to-see-logs-for-the-training-epoch-epoch-0-0-034-0000-its-xshape-torchsize10-768-3"></a>22.2 M    Trainable params<br>
0         Non-trainable params<br>
22.2 M    Total params<br>
88.745    Total estimated model params size (MB)<br>
x.shape torch.Size([10, 768])<br>
x.shape torch.Size([10, 768])<br>
<em><strong>val_acc_all_offn at epoch end 0.3</strong></em>*<br>
<em><strong>val_f1 offensive at epoch end 0.29292929292929293</strong></em>*<br>
/usr/local/lib/python3.10/dist-packages/pytorch_lightning/loops/fit_loop.py:298: The number of training batches (34) is smaller than the logging interval Trainer(log_every_n_steps=50). Set a lower value for log_every_n_steps if you want to see logs for the training epoch.<br>
Epoch 0:   0%<br>
0/34 [00:00&lt;?, ?it/s]<br>
x.shape torch.Size([10, 768])</h2>
<p>IndexError                                Traceback (most recent call last)<br>
 in &lt;cell line: 296&gt;()<br>
294 <span class="hashtag-raw">#if</span> torch.cuda.is_available():gpus=0<br>
295 trainer = pl.Trainer(deterministic=True,max_epochs=2,precision=16,callbacks=all_callbacks)<br>
 → 296 trainer.fit(hm_model, data_module)</p>
<p>29 frames<br>
/usr/local/lib/python3.10/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)<br>
2702     if size_average is not None or reduce is not None:<br>
2703         reduction = _Reduction.legacy_get_string(size_average, reduce)<br>
 → 2704     return torch._C._nn.nll_loss_nd(input, target, weight, _Reduction.get_enum(reduction), ignore_index)<br>
2705<br>
2706</p>
<p>IndexError: Target 2 is out of bounds.</p>
<p>can you please help me in that</p>

ptrblck: <p>Your linear layers return 2 logits corresponding to two classes. The target should thus contain class indices in <code>[0, 1]</code>.</p>

Harshal_Dharpure: <aside class="quote no-group" data-username="ptrblck" data-post="14" data-topic="159629">
<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/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p><code>]</code>.</p>
</blockquote>
</aside>
<p>class indices is in [0,1] but still giving the same error</p>

ptrblck: <p>The error message shows a target index of <code>2</code> is used so you would need to double check where it’s coming from.</p>