Can anyone please tell me if there is a specific method to calculate the running loss/training loss? I came a few formulae while looking for it

train_loss = train_loss + ((1 / (batch + 1)) * (loss.data - train_loss))
train_loss += loss.item().

Below is the code while using it:
#train model
def train(n_epochs,model,loader,optimizer,criterion,save_path):
for epoch in range(n_epochs):
    train_loss = 0

    net.train()
    for batch, (data,target) in enumerate(loaders['train']):
        
        target = target.view(target.size(0),-1)
        target = Variable(target)

        optimizer.zero_grad()

        outputs = net(data)
        
        loss = criterion(outputs,target)
        loss.backward()
        optimizer.step()
        #calculate training loss
        train_loss = train_loss + ((1 / (batch + 1)) * (loss.data - train_loss))

        #print results
        if batch % 100 == 0:
            print("Epoch: {}, Batch: {}, Training Loss: {}".format(epoch+1, batch, train_loss/1000))

print("Finished Training") 

I want to know if there is a particular way to calculate it?