def act_and_dur_inverse_weights(
sequences: Tensor, sos_idx: int = 0, eos_idx: int = 1, trim_eos: bool = True
) -> Tensor:
activities = sequences[:, :, 0]
durations = sequences[:, :, 1]
binned = (durations * 144).to(torch.int) # 10 minute bins
combined = torch.stack([activities, binned], dim=-1)
_, locs, ws = torch.unique(
combined.view(-1, 2), dim=0, return_counts=True, return_inverse=True
)
# set sos and eos weights
ws[sos_idx] = 0
ws[eos_idx] = 0
max_act_weight = ws.max()
ws[eos_idx] = max_act_weight
ws[sos_idx] = max_act_weight
weights = 1 / ws[locs]
weights = weights.view(sequences.shape[0], -1)
if trim_eos:
eos_mask = trim_eos_mask(activities, eos_idx)
weights = weights * eos_mask # apply to weights
weights = weights / weights.mean()
return weights