|
142 | 142 | "\n",
|
143 | 143 | "An instance of `tf.experimental.numpy.ndarray`, called **ND Array**, represents a multidimensional dense array of a given `dtype` placed on a certain device. It is an alias to `tf.Tensor`. Check out the ND array class for useful methods like `ndarray.T`, `ndarray.reshape`, `ndarray.ravel` and others.\n",
|
144 | 144 | "\n",
|
145 |
| - "First create an ND array object, and then invoke different methods. " |
| 145 | + "First create an ND array object, and then invoke different methods." |
146 | 146 | ]
|
147 | 147 | },
|
148 | 148 | {
|
|
170 | 170 | {
|
171 | 171 | "cell_type": "markdown",
|
172 | 172 | "metadata": {
|
173 |
| - "id": "Mub8-dvJMUr4" |
| 173 | + "id": "-BOY8CGRKEhE" |
174 | 174 | },
|
175 | 175 | "source": [
|
176 | 176 | "### Type promotion\n",
|
177 | 177 | "\n",
|
| 178 | + "There are 4 options for type promotion in TensorFlow.\n", |
| 179 | + "\n", |
| 180 | + "- By default, TensorFlow raises errors instead of promoting types for mixed type operations.\n", |
| 181 | + "- Running `tf.numpy.experimental_enable_numpy_behavior()` switches TensorFlow to use `NumPy` type promotion rules (described below).\n", |
| 182 | + "- After TensorFlow 2.15, there are two new options (refer to [TF NumPy Type Promotion](tf_numpy_type_promotion.ipynb) for details):\n", |
| 183 | + " - `tf.numpy.experimental_enable_numpy_behavior(dtype_conversion_mode=\"all\")` uses Jax type promotion rules.\n", |
| 184 | + " - `tf.numpy.experimental_enable_numpy_behavior(dtype_conversion_mode=\"safe\")` uses Jax type promotion rules, but disallows certain unsafe promotions." |
| 185 | + ] |
| 186 | + }, |
| 187 | + { |
| 188 | + "cell_type": "markdown", |
| 189 | + "metadata": { |
| 190 | + "id": "SXskSHrX5J45" |
| 191 | + }, |
| 192 | + "source": [ |
| 193 | + "#### NumPy Type Promotion\n", |
| 194 | + "\n", |
178 | 195 | "TensorFlow NumPy APIs have well-defined semantics for converting literals to ND array, as well as for performing type promotion on ND array inputs. Please see [`np.result_type`](https://numpy.org/doc/1.16/reference/generated/numpy.result_type.html) for more details."
|
179 | 196 | ]
|
180 | 197 | },
|
|
200 | 217 | " (tnp.int32, tnp.int64, tnp.float32, tnp.float64)]\n",
|
201 | 218 | "for i, v1 in enumerate(values):\n",
|
202 | 219 | " for v2 in values[i + 1:]:\n",
|
203 |
| - " print(\"%s + %s => %s\" % \n", |
| 220 | + " print(\"%s + %s => %s\" %\n", |
204 | 221 | " (v1.dtype.name, v2.dtype.name, (v1 + v2).dtype.name))"
|
205 | 222 | ]
|
206 | 223 | },
|
|
932 | 949 | "metadata": {
|
933 | 950 | "accelerator": "GPU",
|
934 | 951 | "colab": {
|
935 |
| - "collapsed_sections": [], |
936 | 952 | "name": "tf_numpy.ipynb",
|
| 953 | + "provenance": [], |
937 | 954 | "toc_visible": true
|
938 | 955 | },
|
939 | 956 | "kernelspec": {
|
|
0 commit comments