class_docstr: Adds stateful behavior and a numpy.ndarray-typed API to a closure with an
  expected return type Tuple[Tensor, Union[Tensor, Sequence[Optional[Tensor]]]].
class_docstr_tok: ["Adds","stateful","behavior","and","a","numpy.ndarray-typed","API","to","a","closure","with","an","expected","return","type","Tuple","[","Tensor",",","Union","[","Tensor",",","Sequence","[","Optional","[","Tensor","]","]","]","]","."]
code: def _get_gradient_ndarray(
      self, fill_value: Optional[float] = None
  ) -> ndarray:
      if self.persistent and self._gradient_ndarray is not None:
          if fill_value is not None:
              self._gradient_ndarray.fill(fill_value)
          return self._gradient_ndarray
  
      size = sum(param.numel() for param in self.parameters.values())
      array = (
          np_zeros(size, dtype=np_float64)
          if fill_value is None or fill_value == 0.0
          else np_full(size, fill_value, dtype=np_float64)
      )
      if self.persistent:
          self._gradient_ndarray = array
  
      return array
  
file_path: botorch/botorch/optim/closures/core.py
imports: from __future__ import annotations
  from functools import partial
  from typing import Any, Callable, Dict, Optional, Sequence, Tuple
  import torch
  from botorch.optim.utils import _handle_numerical_errors, get_tensors_as_ndarray_1d, set_tensors_from_ndarray_1d
  from botorch.optim.utils.numpy_utils import as_ndarray
  from botorch.utils.context_managers import zero_grad_ctx
  from numpy import float64, full, ndarray, zeros
  from torch import Tensor
label_desc: Use image node_id 5 for calling the NdarrayOptimizationClosure obj's underlying member method code with example usage: obj._get_gradient_ndarray(fill_value) and returns: array, self