# -reset the gradients. By default, PyTorch accumulates (sums up) gradients when backward() is called. This behavior is not required here, so you need to ensure that all the gradients are zero before calling the backward.
self._optim.zero_grad()
# -propagate through the network
pred_op=self._model(x)
# -calculate the loss
l=self._crit(pred_op,y)
# -compute gradient by backward propagation
l.backward()
# -update weights
self._optim.step()
# -return the loss
#TODO
returnl
defval_test_step(self,x,y):
# predict
# propagate through the network and calculate the loss and predictions
# return the loss and the predictions
#TODO
pred_op=self._model(x)
y=y.float()
l=self._crit(pred_op,y)
returnl,pred_op
deftrain_epoch(self):
# set training mode
self._model.train()
# iterate through the training set
# transfer the batch to "cuda()" -> the gpu if a gpu is given
# perform a training step
# calculate the average loss for the epoch and return it
#TODO
num_batch=0
total_l=0
foriintqdm(self._train_dl,desc='Training in progress....'):
x,y=i
ifself._cuda:
self._model=self._model.cuda()
self._crit=self._crit.cuda()
x=x.cuda()
y=y.cuda()
total_l+=self.train_step(x,y)
num_batch+=1
returntotal_l/num_batch
defval_test(self):
# set eval mode. Some layers have different behaviors during training and testing (for example: Dropout, BatchNorm, etc.). To handle those properly, you'd want to call model.eval()
# disable gradient computation. Since you don't need to update the weights during testing, gradients aren't required anymore.
# iterate through the validation set
# transfer the batch to the gpu if given
# perform a validation step
# save the predictions and the labels for each batch
# calculate the average loss and average metrics of your choice. You might want to calculate these metrics in designated functions
# return the loss and print the calculated metrics
#TODO
self._model.eval()
self.f1_score_list=[]
witht.no_grad():
predictions=[]
labels=[]
num_batch=0
total_l=0
foriintqdm(self._val_test_dl,desc='Validation in progress....'):