How to compute derivatives with respect to tensor indexing in PyTorch

shi: <pre><code class="lang-auto">def gauss_aod_pro(self):###
        aod_profile_aerosol = torch.zeros_like(self.aod_atm)
        sigma=torch.tensor(1)
        num_layer= sigma/0.5 
        level_w = torch.linspace(-num_layer, num_layer, int(2 * num_layer + 1))
        delz = 0.5 * level_w
        W_total = torch.sum(torch.exp(-(delz**2) / (2 * sigma**2)))
        aod_peak = self.aod_total / W_total
        aod_layer_gauss = aod_peak * torch.exp(-(delz**2) / (2 * sigma**2))
        g_aerosol_layer, ssa_aerosol, tau_radio = self.get_gmean_and_ssa()
        if self.layer_top == self.Nlayer - 1:
            aod_profile_aerosol[int(self.layer_top)] = self.aod_total 
            aod_profile_aerosol = aod_profile_aerosol * tau_radio
            aod_profile_total_atm = self.aod_atm + aod_profile_aerosol
            ssa_aersol_layer = (ssa_aerosol * aod_profile_aerosol[int(self.layer_top)]) / aod_profile_total_atm[int(self.layer_top)]
        elif self.layer_top == self.Nlayer - 2: 
            aod_loss = torch.sum(aod_layer_gauss[2:])
            aod_weight = aod_layer_gauss[0:2] / torch.sum(aod_layer_gauss[0:2])
            #aod_level_new=np.zeros([int(num_level+n)])
            aod_layer_new= aod_layer_gauss[0:2] + aod_loss * aod_weight
            aod_profile_aerosol[int(self.layer_top):] = aod_layer_new
            aod_profile_aerosol = aod_profile_aerosol * tau_radio
            aod_profile_total_atm = self.aod_atm + aod_profile_aerosol
            ssa_aersol_layer = (ssa_aerosol * aod_profile_aerosol[int(self.layer_top):]) /      aod_profile_total_atm[int(self.layer_top):]
        elif self.layer_top == self.Nlayer - 3:
            aod_loss = torch.sum(aod_layer_gauss[3:])
            aod_weight = aod_layer_gauss[0:3] / torch.sum(aod_layer_gauss[0:3])
            #aod_level_new=np.zeros([int(num_level+n)])
            aod_layer_new= aod_layer_gauss[0:3] + aod_loss * aod_weight
            aod_profile_aerosol[int(self.layer_top):] = aod_layer_new
            aod_profile_aerosol = aod_profile_aerosol * tau_radio
            aod_profile_total_atm = self.aod_atm + aod_profile_aerosol
            ssa_aersol_layer = (ssa_aerosol * aod_profile_aerosol[int(self.layer_top):]) /    aod_profile_total_atm[int(self.layer_top):]
        elif self.layer_top == self.Nlayer - 4:
            aod_loss = torch.sum(aod_layer_gauss[4:])
            aod_weight = aod_layer_gauss[0:4] / torch.sum(aod_layer_gauss[0:4])
            #aod_level_new=np.zeros([int(num_level+n)])
            aod_layer_new= aod_layer_gauss[0:4] * aod_weight
            aod_profile_aerosol[int(self.layer_top):] = aod_layer_new
            aod_profile_aerosol = aod_profile_aerosol * tau_radio
            aod_profile_total_atm = self.aod_atm + aod_profile_aerosol
            ssa_aersol_layer = (ssa_aerosol * aod_profile_aerosol[int(self.layer_top):]) / aod_profile_total_atm[int(self.layer_top):]
        else:
            aod_profile_aerosol[int(self.layer_top):int(self.layer_top) + 5] = aod_layer_gauss
            aod_profile_aerosol = aod_profile_aerosol * tau_radio
            aod_profile_total_atm = self.aod_atm + aod_profile_aerosol
            ssa_aersol_layer = (ssa_aerosol * aod_profile_aerosol[int(self.layer_top) : int(self.layer_top) + 5]) / aod_profile_total_atm[int(self.layer_top) : int(self.layer_top) + 5]
        ssa_ = torch.sum(ssa_aersol_layer)
        gradients_ = torch.autograd.grad(ssa_, self.layer_top, allow_unused=True)[0]
        return aod_profile_total_atm, ssa_aersol_layer, g_aerosol_layer,gradients_`
</code></pre>
<p>As shown in the above code, I need to calculate the derivative of SSA with respect to layer_top. However,I got None – sad . layer_top is used as an index to slice the target tensor. I would like to inquire whether this is reasonable and how it can be implemented.</p>
<p>Dear all readers： layer_top is used as an index to slice the target tensor. I would like to inquire whether this is reasonable and how it can be implemented.</p>