I am trying to use ignore_index
, which is a newly introduced keyword parameter for nn.CrossEntropyLoss()
. I updated pytorch from source code and it is up to date. (bleeding edge version)
The following is my code:
criterion = nn.CrossEntropyLoss()
for epoch in range(self.numEpoch):
for batch in self.train_loader:
user_idx = Variable(batch['user_idx']).cuda()
item_vecs = Variable(batch['item_vecs'].float()).cuda()
optimizer.zero_grad()
pred = model(user_idx, item_vecs)
loss = criterion(pred, item_vecs, ignore_index=-1) # I get the error here!!
loss.backward()
optimizer.step()
I get the following error message when I call criterion(pred, item_vecs, ignore_index=-1)
TypeError: "forward() got an unexpected keyword argument 'ignore_index'"
I am tracking the source code, and I realized that the function forward()
is from class CrossEntropyLoss()
in torch/nn/modules/loss.py
The initializer has the parameter ignore_index
. (Refer to line 515 of torch/nn/modules/loss.py
)
Does anyone have any idea why this doesn’t work?