Skip to content

added alpha scaling in expnorm and visualization function #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 19, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion torchmdnet/models/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,33 @@
from torch_geometric.nn import radius_graph, MessagePassing


def visualize_basis(basis_type, num_rbf=50, cutoff_lower=0, cutoff_upper=5):
"""
Function for quickly visualizing a specific basis. This is useful for inspecting
the distance coverage of basis functions for non-default lower and upper cutoffs.

Args:
basis_type (str): Specifies the type of basis functions used. Can be one of
['gauss',expnorm']
num_rbf (int, optional): The number of basis functions.
(default: :obj:`50`)
cutoff_lower (float, optional): The lower cutoff of the basis.
(default: :obj:`0`)
cutoff_upper (float, optional): The upper cutoff of the basis.
(default: :obj:`5`)
"""
import matplotlib.pyplot as plt

distances = torch.linspace(cutoff_lower-1, cutoff_upper+1, 1000)
basis_kwargs = {'num_rbf':num_rbf, 'cutoff_lower':cutoff_lower, 'cutoff_upper':cutoff_upper}
basis_expansion = rbf_class_mapping[basis_type](**basis_kwargs)
expanded_distances = basis_expansion(distances)

for i in range(expanded_distances.shape[-1]):
plt.plot(distances.numpy(), expanded_distances[:,i].detach().numpy())
plt.show()


class NeighborEmbedding(MessagePassing):
def __init__(self, hidden_channels, num_rbf, cutoff_lower,
cutoff_upper, max_z=100):
Expand Down Expand Up @@ -83,6 +110,7 @@ def __init__(self, cutoff_lower=0.0, cutoff_upper=5.0, num_rbf=50, trainable=Tru
self.trainable = trainable

self.cutoff_fn = CosineCutoff(0, cutoff_upper)
self.alpha = 5.0/(cutoff_upper - cutoff_lower)

means, betas = self._initial_params()
if trainable:
Expand All @@ -107,7 +135,7 @@ def reset_parameters(self):

def forward(self, dist):
dist = dist.unsqueeze(-1)
return self.cutoff_fn(dist) * torch.exp(-self.betas * (torch.exp(-dist + self.cutoff_lower) - self.means) ** 2)
return self.cutoff_fn(dist) * torch.exp(-self.betas * (torch.exp(self.alpha*(-dist + self.cutoff_lower)) - self.means) ** 2)


class ShiftedSoftplus(nn.Module):
Expand Down