I have two questions that are related. If I have a unbalanced dataset, how do I calculate the per-class-accuracy properly for each batch? And if I add class weight, should I then assume the dataset is balanced and calculate the accuracy the usual way?
In the following code snip I assume that the dataset is balanced:
for local_batch, local_labels in training_generator:
	# Transfer the data to GPU/CPU
	local_batch, local_labels = local_batch.to(device), local_labels.to(device)
	
	# Initialize gradients, zero the parameter gradients
	optimizer.zero_grad()

	# forward pass: compute predicted outputs by passing inputs to the model
	predictions = model(local_batch)
	
	# calculate training accuracy
	predicted_labels = predictions.argmax(dim=1, keepdim=True).squeeze()
	num_train_correct = (predicted_labels == local_labels).sum().item()
	train_accuracy = num_train_correct / local_batch.size(0)
	...


What if my dataset is unbalanced? How do I then calculate the per-class-accuracy for each batch while training?

	# calculate training accuracy
	predicted_labels = predictions.argmax(dim=1, keepdim=True).squeeze()
	num_train_correct = ????
	train_accuracy = ????
	...


What if I add class weight to CrossEntropyLoss. Should I then assume the dataset is balanced and calculate the accuracy as below or is there a third way to calculate accuracy when class weight is applied?

	# calculate training accuracy
	predicted_labels = predictions.argmax(dim=1, keepdim=True).squeeze()
	num_train_correct = (predicted_labels == local_labels).sum().item()
	train_accuracy = num_train_correct / local_batch.size(0)
	...