I found a way to make the ‘r_max’ differentiable.
import torch

x = torch.linspace(-20, 19, 40)
Y, X = torch.meshgrid(x, x)
R = (X**2 + Y**2)**0.5

r_max = torch.tensor(15.0, requires_grad=True)

R_norm = R / r_max
mask = R_norm.where(R_norm>1, torch.zeros_like(r))
mask = mask.where(R_norm<1, torch.ones_like(r))