ptrblck:

while they should show valid gradient tensors afterwards.


Thanks for the reply : )
I checked the .grad attribute. It’s like what you said before the first backward call it returns None. And then it starts to return all 0s. Now I get why the embedding layer stays the same… But I still can’t figure out why… could you plz help me with this? Here is the loss_fn and train_epoch functions.
[Original formula for loss function which I aim to maximize]

Screenshot 2021-05-30 at 11.25.431180×256 10.4 KB

def loss_fn(f_pos, f_negs, device):
    """
        Loss function
    """
    loss = None
    for f_p, f_ns in zip(f_pos, f_negs):
        if not loss:
            loss = f_p - F.cross_entropy(f_ns.unsqueeze(0), torch.LongTensor([0]).to(device)) - f_ns[0]
        else:
            loss += f_p - F.cross_entropy(f_ns.unsqueeze(0), torch.LongTensor([0]).to(device)) - f_ns[0]
    return -1 * loss

def train_epoch(myNet, my_dataLoader, device, batch_size,
                loss_fn, optimizer, scheduler, writer):
    """
        Train one epoch
    """
    myNet.train()
    losses = []
    for batch_id, d in enumerate(tqdm(my_dataLoader, desc="Traning: ")):
        print(myNet.query_encoder.weight.grad)
        format_batch_tokens(d, batch_size)
        optimizer.zero_grad()
        f_pos_batch = myNet(d["pos"]["question_tokens"].to(device),
                                context_dict_to_device(d["pos"]["context_tokens"], device), batch_size, SIZE_NEGATIVE_PAIRS)

        f_negs_batch = myNet(d["neg"]["question_tokens"].to(device),
                                    context_dict_to_device(d["neg"]["context_tokens"], device), batch_size, SIZE_NEGATIVE_PAIRS)

        loss = loss_fn(f_pos_batch, f_negs_batch, device)
        writer.add_scalar("Loss/Batch", loss, batch_id)
        losses.append(loss.item())
        loss.backward()
        nn.utils.clip_grad_norm(myNet.parameters(), max_norm=1.0)
        optimizer.step()
        scheduler.step()
        optimizer.zero_grad()
    return np.mean(losses)