I try to do brain tumor segmentation for BRATS. Sadly i got this error, The problem seems to be here : loss = functional.nll_loss(y, y_hat)
when i try to implement another loss function such as nn.BCELoss, it doesnt recognize the nn and new fuction, the rest of the code:
def train(i_epoch = 0):
model.train()
iteration = len(train_loader) * i_epoch
for x, y_hat in train_loader:
optimizer.zero_grad()
y = model(x)
y = torch.argmax(y)
loss = functional.nll_loss(y, y_hat) # crossentropy loss. If one class is too imbalanced,
# we can use the weights argument!
loss.backward()
optimizer.step()
iteration += 1
if (iteration % 100 == 0):
writer.add_scalar("loss", loss.data.item(), iteration)
# img =
# writer.add_image("trainimg", y)
# validation:
loss_val = 0
model.eval()
for x, y_hat in val_loader:
y = model(x)
# if (loss_val == 0):
# writer.add_image("val image", toTensor(img), iteration)
loss_val += functional.nll_loss(y, y_hat) # crossentropy loss. If one class is too imbalanced,
# we can use the weights argument!
writer.add_scalar("val", loss_val.data.item(), iteration)
print("Validation loss: {:10}".format(loss_val))