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","]","]","]","]","."]
docstr: Initializes a NdarrayOptimizationClosure instance.
  
  Args:
      closure: A ForwardBackwardClosure instance.
      parameters: A dictionary of tensors representing the closure's state.
          Expected to correspond with the first `len(parameters)` optional
          gradient tensors returned by `closure`.
      as_array: Callable used to convert tensors to ndarrays.
      as_tensor: Callable used to convert ndarrays to tensors.
      get_state: Callable that returns the closure's state as an ndarray. When
          passed as `None`, defaults to calling `get_tensors_as_ndarray_1d`
          on `closure.parameters` while passing `as_array` (if given by the user).
      set_state: Callable that takes a 1-dimensional ndarray and sets the
          closure's state. When passed as `None`, `set_state` defaults to
          calling `set_tensors_from_ndarray_1d` with `closure.parameters` and
          a given ndarray while passing `as_tensor`.
      fill_value: Fill value for parameters whose gradients are None. In most
          cases, `fill_value` should either be zero or NaN.
      persistent: Boolean specifying whether an ndarray should be retained
          as a persistent buffer for gradients.
docstr_tok: ["Initializes","a","NdarrayOptimizationClosure","instance",".","Args",":","closure",":","A","ForwardBackwardClosure","instance",".","parameters",":","A","dictionary","of","tensors","representing","the","closure","'s","state",".","Expected","to","correspond","with","the","first","`","len","(","parameters",")","`","optional","gradient","tensors","returned","by","`","closure","`",".","as_array",":","Callable","used","to","convert","tensors","to","ndarrays",".","as_tensor",":","Callable","used","to","convert","ndarrays","to","tensors",".","get_state",":","Callable","that","returns","the","closure","'s","state","as","an","ndarray",".","When","passed","as","`","None","`",",","defaults","to","calling","`","get_tensors_as_ndarray_1d","`","on","`","closure.parameters","`","while","passing","`","as_array","`","(","if","given","by","the","user",")",".","set_state",":","Callable","that","takes","a","1-dimensional","ndarray","and","sets","the","closure","'s","state",".","When","passed","as","`","None","`",",","`","set_state","`","defaults","to","calling","`","set_tensors_from_ndarray_1d","`","with","`","closure.parameters","`","and","a","given","ndarray","while","passing","`","as_tensor","`",".","fill_value",":","Fill","value","for","parameters","whose","gradients","are","None",".","In","most","cases",",","`","fill_value","`","should","either","be","zero","or","NaN",".","persistent",":","Boolean","specifying","whether","an","ndarray","should","be","retained","as","a","persistent","buffer","for","gradients","."]
code: def __init__(
      self,
      closure: Callable[[], Tuple[Tensor, Sequence[Optional[Tensor]]]],
      parameters: Dict[str, Tensor],
      as_array: Callable[[Tensor], ndarray] = None,  # pyre-ignore [9]
      as_tensor: Callable[[ndarray], Tensor] = torch.as_tensor,
      get_state: Callable[[], ndarray] = None,  # pyre-ignore [9]
      set_state: Callable[[ndarray], None] = None,  # pyre-ignore [9]
      fill_value: float = 0.0,
      persistent: bool = True,
  ) -> None:
      r"""Initializes a NdarrayOptimizationClosure instance.
  
      Args:
          closure: A ForwardBackwardClosure instance.
          parameters: A dictionary of tensors representing the closure's state.
              Expected to correspond with the first `len(parameters)` optional
              gradient tensors returned by `closure`.
          as_array: Callable used to convert tensors to ndarrays.
          as_tensor: Callable used to convert ndarrays to tensors.
          get_state: Callable that returns the closure's state as an ndarray. When
              passed as `None`, defaults to calling `get_tensors_as_ndarray_1d`
              on `closure.parameters` while passing `as_array` (if given by the user).
          set_state: Callable that takes a 1-dimensional ndarray and sets the
              closure's state. When passed as `None`, `set_state` defaults to
              calling `set_tensors_from_ndarray_1d` with `closure.parameters` and
              a given ndarray while passing `as_tensor`.
          fill_value: Fill value for parameters whose gradients are None. In most
              cases, `fill_value` should either be zero or NaN.
          persistent: Boolean specifying whether an ndarray should be retained
              as a persistent buffer for gradients.
      """
      if get_state is None:
          # Note: Numpy supports copying data between ndarrays with different dtypes.
          # Hence, our default behavior need not coerce the ndarray representations
          # of tensors in `parameters` to float64 when copying over data.
          _as_array = as_ndarray if as_array is None else as_array
          get_state = partial(
              get_tensors_as_ndarray_1d,
              tensors=parameters,
              dtype=np_float64,
              as_array=_as_array,
          )
  
      if (
          as_array is None
      ):  # per the note, do this after resolving `get_state`
          as_array = partial(as_ndarray, dtype=np_float64)
  
      if set_state is None:
          set_state = partial(
              set_tensors_from_ndarray_1d,
              parameters,
              as_tensor=as_tensor,
          )
  
      self.closure = closure
      self.parameters = parameters
  
      self.as_array = as_ndarray
      self.as_tensor = as_tensor
      self._get_state = get_state
      self._set_state = set_state
  
      self.fill_value = fill_value
      self.persistent = persistent
      self._gradient_ndarray: Optional[ndarray] = None
  
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 1 to create a new NdarrayOptimizationClosure object  with example: obj = NdarrayOptimizationClosure(closure, parameters, as_array, as_tensor, get_state, set_state, fill_value, persistent)