File size: 4,808 Bytes
b368c46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import torch
import torch.nn as nn

class MultiScaleGaussianBasis(nn.Module):
    """
    Multi-Scale Gaussian Basis for spectral reconstruction.
    
    This module generates a basis of Gaussian functions with fixed widths (scales)
    and learnable centers to capture both sharp features and broad oscillations.

    OBS1: We are fixing the Gaussian widths and learning their centers.
    """
    def __init__(self, n_basis=128, emin=-10.0, emax=50.0, scales_ratios=[0.1, 0.5, 1.0], global_bg=True):
        """
        Args:
            n_basis (int): Total number of basis functions.
            emin (float): Minimum energy value.
            emax (float): Maximum energy value.
            scales_ratios (list): Relative initial widths/scales for each Gaussian group. There 
                                  will be `len(scales_ratios)` Gaussian groups.
        """
        super().__init__()
        self.n_basis = n_basis
        self.emin = emin
        self.emax = emax
        self.global_bg = global_bg
        
        if self.global_bg:
            # We add a learnable logistic/sigmoid function to act as an edge step background
            # that saturates at energies > 0 and stays low at energies < 0
            self.bg_center = nn.Parameter(torch.tensor(0.0))
            self.bg_width = nn.Parameter(torch.tensor(2.0)) # Initial soft step

        
        # We split the basis roughly equally among the scales
        n_scales = len(scales_ratios)
        n_per_scale = n_basis // n_scales
        remainder = n_basis % n_scales
        
        centers_list = []
        sigmas_list = []
        
        # Base grid spacing
        full_range = emax - emin
        
        for i, ratio in enumerate(scales_ratios):
            count = n_per_scale + (1 if i < remainder else 0)
            
            # Distribute centers evenly across the energy range
            centers = torch.linspace(emin, emax, count)
            
            # Finding the spacing between Gaussians
            spacing = full_range / count

            # Computing constant `sigma` for Gaussian group
            sigma = spacing * ratio * 2.0 # Factor of 2 for overlap
            
            # If remainder == 0, all Gaussian groups are initialized at the same point in the 
            # grid. During learning, the different centers will be adjusted to better fit the data
            centers_list.append(centers)
            sigmas_list.append(torch.full_like(centers, sigma))
            
        # Keep centers learnable so the network can adjust peak locations
        self.centers = nn.Parameter(torch.cat(centers_list))
        
        # register_buffer to store constants the model needs but are not learnable parameters, we can 
        # access it via self.sigmas in the forward pass
        self.register_buffer('sigmas', torch.cat(sigmas_list)) 
        
    def forward(self, energy_grid):
        """
        Evaluate the basis functions on the provided energy grid.
        
        Args:
            energy_grid (torch.Tensor): Shape [N_E] for a shared grid or
                                        [B, N_E] for per-graph native grids.
            
        Returns:
            torch.Tensor: Basis matrix with shape [N_E, n_basis] or
                          [B, N_E, n_basis].
        """
        # [..., N_E, 1] - [n_basis] -> [..., N_E, n_basis]
        x_mu_diff = energy_grid.unsqueeze(-1) - self.centers

        # Avoid clamping sigmas as they are fixed, but applying clamping just in case
        sigmas = self.sigmas.clamp(min=1e-4) # [n_basis]

        B = torch.exp(-(x_mu_diff**2) / (2*sigmas**2))
        
        if self.global_bg:
            # Clamp width to prevent division by zero or overly sharp step that causes NaNs
            bg_width = self.bg_width.clamp(min=1e-3)
            # Sigmoid models the cumulative edge step background
            bg = torch.sigmoid((energy_grid - self.bg_center) / bg_width)
            B = torch.cat([B, bg.unsqueeze(-1)], dim=-1)
            # sigmoid is f(x) = 1 / (1 + exp(-(x-x0)/w)). In the limit of bg_width -> 0, 
            # this becomes a step function. In the limit of bg_width -> inf, this becomes 
            # a constant y=0.5. Also, bg_center is the center of the sigmoid (when the 
            # function value is 0.5). These two parameters are learned and fixed for all 
            # atoms in the batch, but the scale of the sigmoid (the constant multiplying 
            # it) is different for each atom (predicted along the coefficients c_k of 
            # the Gaussians). 
            
            # Note: This sigmoid is exactly the Fermi-Dirac distribution up to a minus sign 
            #       in the exponential argument, where bg_width = k_b*T and bg_center = E0. 
            
        return B