class_docstr: Wrapper for fused forward and backward closures.
class_docstr_tok: ["Wrapper","for","fused","forward","and","backward","closures","."]
docstr: Initializes a ForwardBackwardClosure instance.
  
  Args:
      closure: Callable that returns a tensor.
      parameters: A dictionary of tensors whose `grad` fields are to be returned.
      backward: Callable that takes the (reduced) output of `forward` and sets the
          `grad` attributes of tensors in `parameters`.
      reducer: Optional callable used to reduce the output of the forward pass.
      callback: Optional callable that takes the reduced output of `forward` and
          the gradients of `parameters` as positional arguments.
      context_manager: A ContextManager used to wrap each forward-backward call.
          When passed as `None`, `context_manager` defaults to a `zero_grad_ctx`
          that zeroes the gradients of `parameters` upon entry.
docstr_tok: ["Initializes","a","ForwardBackwardClosure","instance",".","Args",":","closure",":","Callable","that","returns","a","tensor",".","parameters",":","A","dictionary","of","tensors","whose","`","grad","`","fields","are","to","be","returned",".","backward",":","Callable","that","takes","the","(","reduced",")","output","of","`","forward","`","and","sets","the","`","grad","`","attributes","of","tensors","in","`","parameters","`",".","reducer",":","Optional","callable","used","to","reduce","the","output","of","the","forward","pass",".","callback",":","Optional","callable","that","takes","the","reduced","output","of","`","forward","`","and","the","gradients","of","`","parameters","`","as","positional","arguments",".","context_manager",":","A","ContextManager","used","to","wrap","each","forward-backward","call",".","When","passed","as","`","None","`",",","`","context_manager","`","defaults","to","a","`","zero_grad_ctx","`","that","zeroes","the","gradients","of","`","parameters","`","upon","entry","."]
code: def __init__(
      self,
      forward: Callable[[], Tensor],
      parameters: Dict[str, Tensor],
      backward: Callable[[Tensor], None] = Tensor.backward,
      reducer: Optional[Callable[[Tensor], Tensor]] = torch.sum,
      callback: Optional[
          Callable[[Tensor, Sequence[Optional[Tensor]]], None]
      ] = None,
      context_manager: Callable = None,  # pyre-ignore [9]
  ) -> None:
      r"""Initializes a ForwardBackwardClosure instance.
  
      Args:
          closure: Callable that returns a tensor.
          parameters: A dictionary of tensors whose `grad` fields are to be returned.
          backward: Callable that takes the (reduced) output of `forward` and sets the
              `grad` attributes of tensors in `parameters`.
          reducer: Optional callable used to reduce the output of the forward pass.
          callback: Optional callable that takes the reduced output of `forward` and
              the gradients of `parameters` as positional arguments.
          context_manager: A ContextManager used to wrap each forward-backward call.
              When passed as `None`, `context_manager` defaults to a `zero_grad_ctx`
              that zeroes the gradients of `parameters` upon entry.
      """
      if context_manager is None:
          context_manager = partial(zero_grad_ctx, parameters)
  
      self.forward = forward
      self.backward = backward
      self.parameters = parameters
      self.reducer = reducer
      self.callback = callback
      self.context_manager = context_manager
  
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 ForwardBackwardClosure object  with example: obj = ForwardBackwardClosure(forward, parameters, backward, reducer, callback, context_manager)