<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L243-L499) |

Implements the Numpy backend ops for a PC auto-batching VM.

#### View aliases


**Main aliases**

[`tfp.experimental.auto_batching.numpy_backend.NumpyBackend`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/auto_batching/NumpyBackend)

<br />

<br />

<br />

<br />

| ## Attributes ||
|---|---|
| `variable_class` | <br /> <br /> |

<br />

## Methods

### `any`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L493-L495)

    any(
        t, name=None
    )

### `assert_matching_dtype`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L319-L332)

    assert_matching_dtype(
        expected_dtype, val, message=''
    )

Asserts that the dtype of `val` matches `expected_dtype`.

<br />

<br />

<br />

| Args ||
|---|---|
| `expected_dtype` | A numpy dtype |
| `val` | An object convertible to `np.array` |
| `message` | Optional diagnostic message. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If dtype does not match. |

<br />

### `batch_size`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L334-L340)

    batch_size(
        val, name=None
    )

Returns the first (batch) dimension of `val`.

### `broadcast_to_shape_of`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L394-L410)

    broadcast_to_shape_of(
        val, target, name=None
    )

Broadcasts val to the shape of target.

<br />

<br />

<br />

| Args ||
|---|---|
| `val` | Python or Numpy array to be broadcast. Must be `np.array` compatible and broadcast-compatible with `target`. |
| `target` | Python or Numpy array whose shape we broadcast `val` to match. |
| `name` | Optional name for the op. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `broadcast_val` | A `np.ndarray` with shape matching `val + target`. Provided that `val`'s dimension sizes are all smaller or equal to `target`'s, the returned value will be the shape of `target`. |

<br />

### `cond`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L412-L430)

    cond(
        pred, true_fn, false_fn, name=None
    )

Implements a conditional operation for the backend.

<br />

<br />

<br />

| Args ||
|---|---|
| `pred` | A Python or Numpy `bool` scalar indicating the condition. |
| `true_fn` | A callable accepting and returning nests of `np.ndarray`s with the same structure as `state`, to be executed when `pred` is True. |
| `false_fn` | A callable accepting and returning nests of `np.ndarray`s with the same structure as `state`, to be executed when `pred` is False. |
| `name` | Optional name for the op. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `state` | Output state, matching nest structure of input argument `state`. |

<br />

### `create_variable`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L362-L387)

    create_variable(
        name, alloc, type_, max_stack_depth, batch_size
    )

Returns an intialized Variable.

<br />

<br />

<br />

| Args ||
|---|---|
| `name` | Name for the variable. |
| `alloc` | `VariableAllocation` for the variable. |
| `type_` | [`instructions.TensorType`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/auto_batching/TensorType) describing the sub-batch shape and dtype of the variable being created. |
| `max_stack_depth` | Python `int`, the maximum stack depth to enforce. |
| `batch_size` | Python `int`, the number of parallel threads being executed. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `var` | A new, initialized Variable object. |

<br />

### `equal`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L483-L486)

    equal(
        t1, t2, name=None
    )

Implements equality comparison for Numpy backend.

### `fill`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L346-L360)

    fill(
        value, size, dtype, shape, name=None
    )

Fill a fresh batched Tensor of the given shape and dtype with `value`.

<br />

<br />

<br />

| Args ||
|---|---|
| `value` | Scalar to fill with. |
| `size` | Scalar `int` `Tensor` specifying the number of VM threads. |
| `dtype` | [`tf.DType`](https://www.tensorflow.org/api_docs/python/tf/dtypes/DType) of the zeros to be returned. |
| `shape` | Rank 1 `int` `Tensor`, the per-thread value shape. |
| `name` | Optional name for the op. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `result` | `Tensor` of `dtype` `value`s with shape `[size, *shape]` |

<br />

### `full_mask`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L389-L392)

    full_mask(
        size, name=None
    )

Returns an all-True mask `np.ndarray` with shape `[size]`.

### `merge_dtypes`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L292-L302)

    merge_dtypes(
        dt1, dt2
    )

Merges two dtypes, returning a compatible dtype.

<br />

<br />

<br />

| Args ||
|---|---|
| `dt1` | A numpy dtype, or None. |
| `dt2` | A numpy dtype, or None. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `dtype` | The more precise numpy dtype (e.g. prefers int64 over int32). |

<br />

### `merge_shapes`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L304-L317)

    merge_shapes(
        s1, s2
    )

Merges two shapes, returning a broadcasted shape.

<br />

<br />

<br />

| Args ||
|---|---|
| `s1` | A `list` of Python `int` or None. |
| `s2` | A `list` of Python `int` or None. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `shape` | A `list` of Python `int` or None. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | If `s1` and `s2` are not broadcast compatible. |

<br />

### `not_equal`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L488-L491)

    not_equal(
        t1, t2, name=None
    )

Implements inequality comparison for Numpy backend.

### `prepare_for_cond`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L432-L443)

    prepare_for_cond(
        state
    )

Backend hook for preparing Tensors for `cond`.

Does nothing in the numpy backend (needed by the TensorFlow backend).

<br />

<br />

<br />

| Args ||
|---|---|
| `state` | A state to be prepared for use in conditionals. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `state` | The prepared state. |

<br />

### `reduce_min`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L466-L469)

    reduce_min(
        t, name=None
    )

Implements reduce_min for Numpy backend.

### `run_on_dummies`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L266-L290)

    run_on_dummies(
        primitive_callable, input_types
    )

Runs the given `primitive_callable` with dummy input.

This is useful for examining the outputs for the purpose of type inference.

<br />

<br />

<br />

| Args ||
|---|---|
| `primitive_callable` | A python callable. |
| `input_types` | `list` of [`instructions.Type`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/auto_batching/Type) type of each argument to the callable. Note that the contained `TensorType` objects must match the dimensions with which the primitive is to be invoked at runtime, even though type inference conventionally does not store the batch dimension in the `TensorType`s. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `outputs` | pattern of backend-specific objects whose types may be analyzed by the caller with `type_of`. |

<br />

### `static_value`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L342-L344)

    static_value(
        t
    )

Gets the eager/immediate value of `t`.

### `switch_case`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L478-L481)

    switch_case(
        branch_selector, branch_callables, name=None
    )

Implements a switch (branch_selector) { case ... } construct.

### `type_of`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L253-L264)

    type_of(
        t, dtype_hint=None
    )

Returns the [`instructions.Type`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/auto_batching/Type) of `t`.

<br />

<br />

<br />

| Args ||
|---|---|
| `t` | `np.ndarray` or a Python constant. |
| `dtype_hint` | dtype to prefer, if `t` is a constant. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `vm_type` | [`instructions.TensorType`](https://www.tensorflow.org/probability/api_docs/python/tfp/experimental/auto_batching/TensorType) describing `t` |

<br />

### `where`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L445-L464)

    where(
        condition, x, y, name=None
    )

Implements a where selector for the Numpy backend.

Extends [`tf.where`](https://www.tensorflow.org/api_docs/python/tf/where) to support broadcasting of `on_false`.

<br />

<br />

<br />

| Args ||
|---|---|
| `condition` | A `bool` `np.ndarray`, either a vector having length `y.shape[0]` or matching the full shape of `y`. |
| `x` | `np.ndarray` of values to take when `condition` is `True`. |
| `y` | `np.ndarray` of values to take when `condition` is `False`. May be smaller than `x`, as long as it is broadcast-compatible. |
| `name` | Optional name for the op. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| `masked` | A `np.ndarray` where indices corresponding to `True` values in `condition` come from the corresponding value in `x`, and others come from `y`. |

<br />

### `while_loop`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L471-L476)

    while_loop(
        cond, body, loop_vars, name=None
    )

Implements while loops for Numpy backend.

### `wrap_straightline_callable`

[View source](https://github.com/tensorflow/probability/blob/v0.25.0/tensorflow_probability/python/experimental/auto_batching/numpy_backend.py#L497-L499)

    wrap_straightline_callable(
        f
    )

Method exists solely to be stubbed, i.e. for defun or XLA compile.