Exports tf.experimental.numpy.enable_numpy_behavior, and unbreaks tf-numpy guide.

PiperOrigin-RevId: 358529149
Change-Id: I533f9bce385480cdbd17492cf21130fab5d00328
This commit is contained in:
Peng Wang 2021-02-19 19:50:56 -08:00 committed by TensorFlower Gardener
parent 1d65151693
commit edd7e6ff07
4 changed files with 22 additions and 5 deletions
tensorflow/python
framework
ops/numpy_ops
third_party/py/numpy/tf_numpy_api

View File

@ -391,9 +391,10 @@ class Tensor(internal.NativeObject, core_tf_types.Tensor):
"tolist", "data"}:
# TODO(wangpeng): Export the enable_numpy_behavior knob
raise AttributeError("""
'{}' object has no attribute '{}'.
If you are looking for numpy-related methods, please run the following:
import tensorflow.python.ops.numpy_ops.np_config
np_config.enable_numpy_behavior()""")
np_config.enable_numpy_behavior()""".format(type(self).__name__, name))
self.__getattribute__(name)
@staticmethod

View File

@ -175,6 +175,7 @@ from tensorflow.python.ops.numpy_ops import np_utils
# pylint: disable=wildcard-import
from tensorflow.python.ops.numpy_ops.np_array_ops import *
from tensorflow.python.ops.numpy_ops.np_arrays import ndarray
from tensorflow.python.ops.numpy_ops.np_config import *
from tensorflow.python.ops.numpy_ops.np_dtypes import *
from tensorflow.python.ops.numpy_ops.np_math_ops import *
# pylint: enable=wildcard-import

View File

@ -20,18 +20,29 @@ from __future__ import print_function
from tensorflow.python.framework import ops
from tensorflow.python.ops.numpy_ops import np_dtypes
from tensorflow.python.ops.numpy_ops import np_export
from tensorflow.python.ops.numpy_ops import np_math_ops
@np_export.np_export("experimental_enable_numpy_behavior")
def enable_numpy_behavior(prefer_float32=False):
"""Enable NumPy behavior on Tensors.
Includes addition of methods, type promotion on operator overloads and
support for NumPy-style slicing.
Enabling NumPy behavior has three effects:
* It adds to `tf.Tensor` some common NumPy methods such as `T`,
`reshape` and `ravel`.
* It changes dtype promotion in `tf.Tensor` operators to be
compatible with NumPy. For example,
`tf.ones([], tf.int32) + tf.ones([], tf.float32)` used to throw a
"dtype incompatible" error, but after this it will return a
float64 tensor (obeying NumPy's promotion rules).
* It enhances `tf.Tensor`'s indexing capability to be on par with
[NumPy's](https://numpy.org/doc/stable/reference/arrays.indexing.html).
Args:
prefer_float32: Whether to allow type inference to use float32, or use
float64 similar to NumPy.
prefer_float32: Controls whether dtype inference will use float32
for Python floats, or float64 (the default and the
NumPy-compatible behavior).
"""
ops.enable_numpy_style_type_promotion()
ops.enable_numpy_style_slicing()

View File

@ -388,6 +388,10 @@ tf_module {
name: "expand_dims"
argspec: "args=[\'a\', \'axis\'], varargs=None, keywords=None, defaults=None"
}
member_method {
name: "experimental_enable_numpy_behavior"
argspec: "args=[\'prefer_float32\'], varargs=None, keywords=None, defaults=[\'False\'], "
}
member_method {
name: "expm1"
argspec: "args=[\'x\'], varargs=None, keywords=None, defaults=None"