Skip to content

Commit

Permalink
calculate gamma positions in float64 #31
Browse files Browse the repository at this point in the history
  • Loading branch information
lucidrains committed Sep 27, 2023
1 parent 5117062 commit 05a3654
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
14 changes: 11 additions & 3 deletions enformer_pytorch/modeling_enformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,22 @@ def get_positional_features_gamma(positions, features, seq_len, stddev = None, s
if not exists(start_mean):
start_mean = seq_len / features

mean = torch.linspace(start_mean, seq_len, features, device = positions.device)
# turns out xlogy between tensorflow and torch differs because of the log - thanks to phd student @johahi for finding this!
# do everything in float64 here for precision

dtype = positions.dtype
positions = positions.double()
mean = torch.linspace(start_mean, seq_len, features, device = positions.device, dtype = torch.float64)

mean = mean[None, ...]
concentration = (mean / stddev) ** 2
rate = mean / stddev ** 2
probabilities = gamma_pdf(positions.float().abs()[..., None], concentration, rate)

probabilities = gamma_pdf(positions.abs()[..., None], concentration, rate)
probabilities = probabilities + eps
outputs = probabilities / torch.amax(probabilities, dim = -1, keepdim = True)
return outputs

return outputs.to(dtype)

def get_positional_embed(seq_len, feature_size, device):
distances = torch.arange(-seq_len + 1, seq_len, device = device)
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
name = 'enformer-pytorch',
packages = find_packages(exclude=[]),
include_package_data = True,
version = '0.7.6',
version = '0.7.7',
license='MIT',
description = 'Enformer - Pytorch',
author = 'Phil Wang',
Expand Down

0 comments on commit 05a3654

Please sign in to comment.