code: def calc_est_grad(func, x, y, rad, num_samples):
      B, *_ = x.shape
      Q = num_samples // 2
      N = len(x.shape) - 1
      with ch.no_grad():
          # Q * B * C * H * W
          extender = [1] * N
          queries = x.repeat(Q, *extender)
          noise = ch.randn_like(queries)
          norm = (
              noise.view(B * Q, -1).norm(dim=-1).view(B * Q, *extender)
          )
          noise = noise / norm
          noise = ch.cat([-noise, noise])
          queries = ch.cat([queries, queries])
          y_shape = [1] * (len(y.shape) - 1)
          l = func(
              queries + rad * noise, y.repeat(2 * Q, *y_shape)
          ).view(-1, *extender)
          grad = (
              l.view(2 * Q, B, *extender)
              * noise.view(2 * Q, B, *noise.shape[1:])
          ).mean(dim=0)
      return grad
  
imports: import torch
  import shutil
  import dill
  import os
  from subprocess import Popen, PIPE
  import pandas
  from PIL import Image
  from .None import constants
label_desc: Use image node_id 2 for calling a global function with example usage: calc_est_grad(func, x, y, rad, num_samples) and returns: grad