Module: tf.experimental.numpy

tf.experimental.numpy: NumPy API on TensorFlow.

This module provides a subset of NumPy API, built on top of TensorFlow operations. APIs are based on and have been tested with NumPy 1.16 version.

The set of supported APIs may be expanded over time. Also future releases may change the baseline version of NumPy API being supported. A list of some systematic differences with NumPy is listed later in the "Differences with NumPy" section.

Getting Started

Please also see TensorFlow NumPy Guide.

In the code snippets below, we will assume that tf.experimental.numpy is imported as tnp and NumPy is imported as np

print(tnp.ones([2,1]) + np.ones([1, 2]))


The module provides an ndarray class which wraps an immutable tf.Tensor. Additional functions are provided which accept array-like objects. Here array-like objects include ndarrays as defined by this module, as well as tf.Tensor, in addition to types accepted by NumPy.

A subset of NumPy dtypes are supported. Type promotion follows NumPy semantics.

print(tnp.ones([1, 2], dtype=tnp.int16) + tnp.ones([2, 1], dtype=tnp.uint8))

Array Interface

The ndarray class implements the __array__ interface. This should allow these objects to be passed into contexts that expect a NumPy or array-like object (e.g. matplotlib).

np.sum(tnp.ones([1, 2]) + np.ones([2, 1]))

TF Interoperability

The TF-NumPy API calls can be interleaved with TensorFlow calls without incurring Tensor data copies. This is true even if the ndarray or