计算训练期间的火车准确性

发布于 2025-02-05 15:20:31 字数 3130 浏览 1 评论 0原文

我正在培训XLM-Roberta模型以获取文本二进制文本分类,但是我在列车准确性方面遇到了一些问题。最后,我的目标是使用准确的火车和验证数据创建曲线图。 我想在培训模型中获得火车准确性。我有这个脚本,但是我该怎么办? 在我使用的脚本下方:

import random
import numpy as np
import gc
from sklearn.metrics import accuracy_score
seed_val = 45
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
# Store the average loss after each epoch so we can plot them.
training_stats = []
correct_train = 0
total_train = 0
# Measure how long the training epoch takes.
total_t0 = time.time()
for epoch in range(0, epochs):    
    print("")
    print('======== Epoch {:} / {:} ========'.format(epoch + 1, epochs))
    stacked_val_labels = []
    #Training
    print('Training...')
    # put the model
    model.train()
    # This turns gradient calculations on and off.
    torch.set_grad_enabled(True)
    # Measure how long the training epoch takes.
    t0 = time.time()
    # Reset the total loss for this epoch.
    total_train_loss = 0
    for i, batch in enumerate(train_dataloader):
        train_status = 'Batch ' + str(i) + ' of ' + str(len(train_dataloader))
        print(train_status, end='\r')
        b_input_ids = batch[0].to(device)
        b_input_mask = batch[1].to(device)
        b_labels = batch[2].to(device)      
        outputs = model(b_input_ids, 
                  attention_mask=b_input_mask, 
                  labels=b_labels)
        model.zero_grad()        
        # Get the loss from the outputs tuple: (loss, logits)
        loss = outputs[0]
        logits = outputs[1]
        # Move logits and labels to CPU
        logits = logits.detach().cpu().numpy()
        label_ids = b_labels.to('cpu').numpy()
        #label = targets_list.extend(label_ids)
        # Convert the loss from a torch tensor to a number.
        # Calculate the total loss.
        total_train_loss = total_train_loss + loss.item()
        # Zero the gradients
        optimizer.zero_grad()
        # Perform a backward pass to calculate the gradients.
        loss.backward()
        # This is to help prevent the "exploding gradients" problem.
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        # Use the optimizer to update the weights.
        optimizer.step() 

        #Accuracy
        #output = (outputs[1]>0.5).float()
        output = (logits>0.5).float()
        correct = (output == label_ids).float().sum()
        # Measure how long this epoch took.
        training_time = format_time(time.time() - t0)
    print("")
    print('Train loss:' ,total_train_loss)
    print('Train acc:' , 100 * correct / len(train_dataloader))
    print("  Training epcoh took: {:}".format(training_time))

对于Exampe,在这种情况下,我会收到此错误,我不知道找到一个解决方案:

AttributeError                            Traceback (most recent call last)
<ipython-input-38-63da1425fee8> in <module>()
     93         #Accuracy
     94         #output = (outputs[1]>0.5).float()
---> 95         output = (logits>0.5).float()
     96         correct = (output == label_ids).float().sum()
     97         # Optimizer for TPU

I am training XLM-Roberta model to get text binary text classification, but I have some problem with putting accuracy for train. My aim, at the end, is to create curve-plot with accuracy train and validation data.
I would like to get train accuracy during training model. I have this script, but how can I do?
Below the script that I use:

import random
import numpy as np
import gc
from sklearn.metrics import accuracy_score
seed_val = 45
random.seed(seed_val)
np.random.seed(seed_val)
torch.manual_seed(seed_val)
torch.cuda.manual_seed_all(seed_val)
# Store the average loss after each epoch so we can plot them.
training_stats = []
correct_train = 0
total_train = 0
# Measure how long the training epoch takes.
total_t0 = time.time()
for epoch in range(0, epochs):    
    print("")
    print('======== Epoch {:} / {:} ========'.format(epoch + 1, epochs))
    stacked_val_labels = []
    #Training
    print('Training...')
    # put the model
    model.train()
    # This turns gradient calculations on and off.
    torch.set_grad_enabled(True)
    # Measure how long the training epoch takes.
    t0 = time.time()
    # Reset the total loss for this epoch.
    total_train_loss = 0
    for i, batch in enumerate(train_dataloader):
        train_status = 'Batch ' + str(i) + ' of ' + str(len(train_dataloader))
        print(train_status, end='\r')
        b_input_ids = batch[0].to(device)
        b_input_mask = batch[1].to(device)
        b_labels = batch[2].to(device)      
        outputs = model(b_input_ids, 
                  attention_mask=b_input_mask, 
                  labels=b_labels)
        model.zero_grad()        
        # Get the loss from the outputs tuple: (loss, logits)
        loss = outputs[0]
        logits = outputs[1]
        # Move logits and labels to CPU
        logits = logits.detach().cpu().numpy()
        label_ids = b_labels.to('cpu').numpy()
        #label = targets_list.extend(label_ids)
        # Convert the loss from a torch tensor to a number.
        # Calculate the total loss.
        total_train_loss = total_train_loss + loss.item()
        # Zero the gradients
        optimizer.zero_grad()
        # Perform a backward pass to calculate the gradients.
        loss.backward()
        # This is to help prevent the "exploding gradients" problem.
        torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)
        # Use the optimizer to update the weights.
        optimizer.step() 

        #Accuracy
        #output = (outputs[1]>0.5).float()
        output = (logits>0.5).float()
        correct = (output == label_ids).float().sum()
        # Measure how long this epoch took.
        training_time = format_time(time.time() - t0)
    print("")
    print('Train loss:' ,total_train_loss)
    print('Train acc:' , 100 * correct / len(train_dataloader))
    print("  Training epcoh took: {:}".format(training_time))

For exampe in this case I recieve this error and I don't know to find a solution:

AttributeError                            Traceback (most recent call last)
<ipython-input-38-63da1425fee8> in <module>()
     93         #Accuracy
     94         #output = (outputs[1]>0.5).float()
---> 95         output = (logits>0.5).float()
     96         correct = (output == label_ids).float().sum()
     97         # Optimizer for TPU

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文