I used clamp(min=0) for the output of forward propagation. I calculated the loss of two batches manually, one for each time. The loss works fine. I guess the problem is that when I tried this, and it didn’t work
model=DetectionNet()
train(model=model, train_loader=train_loader, criterion=loss_fn, optimizer=optimizer, scheduler=step_lr_scheduler, num_epochs=150)

then I modified the code and run the following since I use jupyterbook, the variable of previous excution still exits, then two different instances are created. It did calculate the loss, but it calculates the gradient of the first instance of the model.
train(model=DetectionNet(), train_loader=train_loader, criterion=loss_fn, optimizer=optimizer, #scheduler=step_lr_scheduler, num_epochs=150)

I added 1e-8 inside the sqrt(). It worked. I have an initial loss around 500, is it normal for a single-stage detector, after 90 epochs,  the loss kind of stuck at 80, could you give me any advice? I used the following optimizer.
optimizer = optim.Adam(model.parameters(), lr=2e-5, betas=(0.9, 0.999), eps=1e-08, weight_decay=0, amsgrad=False)
scheduler = lr_scheduler.StepLR(optimizer, step_size=80, gamma=0.9)