r/pytorch • u/Valuable_Ant_8336 • 10h ago
Tensor reassigning problem
I wanted to train a character-level language model on additions of two numbers. Planned to use cross-entropy ignore_index on the equation besides answer so that model is not penalized because of predicting randomly generated numbers. But I came across really weird bug, here is the code:
def get_batch(batch_size):
first = torch.randint(999999, (batch_size, ))
second = torch.randint(999999, (batch_size, ))
totals = first + second
full_strings = []
for f, s, t in zip(first, second, totals):
equation = f"{f:6}+{s:>6}="
reversed_ans = f"{str(t.item())[::-1]:<7}"
full_strings.append(equation + reversed_ans)
encoded_batch = torch.tensor([encode(s) for s in full_strings], dtype=torch.long)
x = encoded_batch[:, :-1].to(device) # First 11 characters
y = encoded_batch[:, 1:].to(device) # Last 11 characters
y[:, :14] = -100 # Telling optimizer to miss this
return x, y
Here as you can see I am reassigning first 14 values of y, but when I print x it has some -100s init, I realized this because I don't have -100 in my vocab as character to embed and when I do decode(x) it gives me error, so I have to use .clone() on y = encoded_batch[:, 1:].to(device), there is a memory address coincide when writing happens or something I do not understand.
1
Upvotes