Fix loss accumulator when buffers are not filled
They were reporting incorrect losses.
This commit is contained in:
parent
fe50d6f9d0
commit
61e5047c60
|
@ -8,17 +8,22 @@ class LossAccumulator:
|
||||||
|
|
||||||
def add_loss(self, name, tensor):
|
def add_loss(self, name, tensor):
|
||||||
if name not in self.buffers.keys():
|
if name not in self.buffers.keys():
|
||||||
self.buffers[name] = (0, torch.zeros(self.buffer_sz))
|
self.buffers[name] = (0, torch.zeros(self.buffer_sz), False)
|
||||||
i, buf = self.buffers[name]
|
i, buf, filled = self.buffers[name]
|
||||||
# Can take tensors or just plain python numbers.
|
# Can take tensors or just plain python numbers.
|
||||||
if isinstance(tensor, torch.Tensor):
|
if isinstance(tensor, torch.Tensor):
|
||||||
buf[i] = tensor.detach().cpu()
|
buf[i] = tensor.detach().cpu()
|
||||||
else:
|
else:
|
||||||
buf[i] = tensor
|
buf[i] = tensor
|
||||||
self.buffers[name] = ((i+1) % self.buffer_sz, buf)
|
filled = i+1 >= self.buffer_sz or filled
|
||||||
|
self.buffers[name] = ((i+1) % self.buffer_sz, buf, filled)
|
||||||
|
|
||||||
def as_dict(self):
|
def as_dict(self):
|
||||||
result = {}
|
result = {}
|
||||||
for k, v in self.buffers.items():
|
for k, v in self.buffers.items():
|
||||||
result["loss_" + k] = torch.mean(v[1])
|
i, buf, filled = v
|
||||||
|
if filled:
|
||||||
|
result["loss_" + k] = torch.mean(buf)
|
||||||
|
else:
|
||||||
|
result["loss_" + k] = torch.mean(buf[:i])
|
||||||
return result
|
return result
|
Loading…
Reference in New Issue
Block a user