class InvSigmoid(nn.Module): """Pointwise :math:`s^{-1}(x) = \log(x / (1-x))`.""" def __init__(self, a=None): """Initialize a new instance. Examples -------- >>> invsig = InvSigmoid() >>> xvals = [1 / (1 + np.exp(-1)), 1 / (1 + np.exp(-3))] >>> x = autograd.Variable(torch.Tensor(xvals)) >>> invsig(x) # should be [1, 3] Variable containing: 1.0000 3.0000 [torch.FloatTensor of size 2] """ super(InvSigmoid, self).__init__() def forward(self, x): return torch.log(x / (1 - x)) if __name__ == '__main__': from dipp.util.testutils import run_doctests from torch import autograd import numpy as np extraglobs = {'np': np, 'autograd': autograd} run_doctests(extraglobs=extraglobs)
raise ValueError('`init_c` must be provided for `c=None`') self.c = nn.Parameter(torch.Tensor([init_c])) elif isinstance(c, nn.Parameter): self.c = c else: assert c > 0 self.c = float(c) # Sharing the `a` and `c` parameters with the constituting modules self.inv_sigmoid = InvSigmoid() self.local_sim_ax0 = HaarPSISimilarityMap(0, self.a, self.c) self.local_sim_ax1 = HaarPSISimilarityMap(1, self.a, self.c) self.wmap_ax0 = HaarPSIWeightMap(0) self.wmap_ax1 = HaarPSIWeightMap(1) def forward(self, x, y): wmap_ax0 = self.wmap_ax0(x, y) wmap_ax1 = self.wmap_ax1(x, y) numer = torch.sum( self.local_sim_ax0(x, y) * wmap_ax0 + self.local_sim_ax1(x, y) * wmap_ax1) denom = torch.sum(wmap_ax0 + wmap_ax1) return (self.inv_sigmoid(numer / denom) / self.a) ** 2 if __name__ == '__main__': from dipp.util.testutils import run_doctests run_doctests()
num_digits = int(np.ceil(np.log10(max(len(modules) - 1, 1)))) idx_fmt = '{{:<{}}}'.format(num_digits) for i, entry in summary.items(): if i == 0: print('=== Summary of {} ==='.format(entry['name'])) print('Input shapes :', [shape_str(s) for s in entry['shapes_in']]) print('Output shapes:', [shape_str(s) for s in entry['shapes_out']]) print('# of params :', entry['num_params']) else: print() if verbose: print(idx_fmt.format(i - 1) + ': ' + entry['repr']) else: print(idx_fmt.format(i - 1) + ': ' + entry['name']) print('Input shapes :', [shape_str(s) for s in entry['shapes_in']]) print('Output shapes:', [shape_str(s) for s in entry['shapes_out']]) print('# of params :', entry['num_params']) if return_summary: return summary if __name__ == '__main__': from dipp.util.testutils import run_doctests import torch run_doctests(extraglobs={'torch': torch, 'nn': torch.nn})