Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug fixed: for loss and accuracy calculation in train.py #3

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
.DS_store
.idea
*.pyc
*.pyc
eenet/
datasets/cifar-100-python
datasets/cifar-100-python.tar.gz
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
from config import Config
from data_tools.dataloader import get_dataloaders
from predict import validate
from predict_utils import dynamic_evaluate
from utils.predict_utils import dynamic_evaluate
from train import train
from utils import save_checkpoint, load_checkpoint, measure_flops, load_state_dict
from utils.utils import save_checkpoint, load_checkpoint, measure_flops, load_state_dict

# import sys
# if all(['models' not in sys.path]):
Expand Down
14 changes: 8 additions & 6 deletions predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import torch.nn.parallel
import torch.optim

from utils import accuracy, AverageMeter
from utils.utils import accuracy, AverageMeter


def validate(model, val_loader, criterion, args):
Expand Down Expand Up @@ -40,17 +40,19 @@ def validate(model, val_loader, criterion, args):
if not isinstance(output, list):
output = [output]

loss = torch.zeros(0)
#loss = torch.zeros(0)
loss = torch.tensor(0.0, dtype=torch.float)
for j in range(len(output)):
if 'bert' in model.__class__.__name__:
loss += (j + 1) * criterion(output[j], target_var) / (args.num_exits * (args.num_exits + 1))
loss += (j + 1) * criterion(output[j][0][0], target_var) / (args.num_exits * (args.num_exits + 1))
else:
loss += criterion(output[j], target_var) / args.num_exits

loss += criterion(output[j][0][0], target_var) / args.num_exits
losses.update(loss.item(), input.size(0))

for j in range(len(output)):
prec1, prec5 = accuracy(output[j].data, target, topk=(1, 5))
relevant_output = output[j][0][0].detach()
prec1, prec5 = accuracy(relevant_output, target, topk=(1, 5))
top1[j].update(prec1.item(), input.size(0))
top5[j].update(prec5.item(), input.size(0))

Expand Down
21 changes: 13 additions & 8 deletions train.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import torch.nn.parallel
import torch.optim

from utils import adjust_learning_rate, accuracy, AverageMeter
from utils.utils import adjust_learning_rate, accuracy, AverageMeter


def train(model, train_loader, criterion, optimizer, epoch, args, train_params):
Expand Down Expand Up @@ -52,18 +52,23 @@ def train(model, train_loader, criterion, optimizer, epoch, args, train_params):
if not isinstance(output, list):
output = [output]

loss = torch.tensor(0)
#the loss variable must be float type as it handles floating point values from the dataset
loss = torch.tensor(0.0, dtype=torch.float)
#print(f"Data type of loss: {loss.dtype}")
for j in range(len(output)):
loss += (j + 1) * criterion(output[j], target_var) / (args.num_exits * (args.num_exits + 1))
if epoch > train_params['num_epoch'] * 0.75 and j < len(output) - 1:
T = 3
alpha_kl = 0.01
loss += torch.nn.KLDivLoss()(torch.log_softmax(output[j] / T, dim=-1), torch.softmax(output[-1] / T, dim=-1)) * alpha_kl * T * T
#torch._C._nn.cross_entropy_loss function accepts a tensor.
loss += (j + 1) * criterion(output[j][0][0], target_var) / (args.num_exits * (args.num_exits + 1))
if epoch > train_params['num_epoch'] * 0.75 and j < len(output) - 1:
T = 3
alpha_kl = 0.01
loss += torch.nn.KLDivLoss()(torch.log_softmax(output[j] / T, dim=-1), torch.softmax(output[-1] / T, dim=-1)) * alpha_kl * T * T

losses.update(loss.item(), input.size(0))

for j in range(len(output)):
prec1, prec5 = accuracy(output[j].data, target, topk=(1, 5))
#tensor has data attribute
relevant_output = output[j][0][0].detach()
prec1, prec5 = accuracy(relevant_output, target, topk=(1, 5))
top1[j].update(prec1.item(), input.size(0))
top5[j].update(prec5.item(), input.size(0))

Expand Down
2 changes: 1 addition & 1 deletion utils/predict_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

import pandas as pd

from predict_helpers import *
from utils.predict_helpers import *


def dynamic_evaluate(model, test_loader, val_loader, args):
Expand Down
2 changes: 1 addition & 1 deletion utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import models
import torch

from op_counter import measure_model
from utils.op_counter import measure_model


def save_checkpoint(state, args, is_best, filename, result, prec1_per_exit, prec5_per_exit):
Expand Down