|
70 | 70 | "source": [
|
71 | 71 | "This is an introductory TensorFlow tutorial that shows how to:\n",
|
72 | 72 | "\n",
|
73 |
| - "* Import the required package\n", |
74 |
| - "* Create and use tensors\n", |
75 |
| - "* Use GPU acceleration\n", |
76 |
| - "* Demonstrate `tf.data.Dataset`" |
| 73 | + "* Import the required package.\n", |
| 74 | + "* Create and use tensors.\n", |
| 75 | + "* Use GPU acceleration.\n", |
| 76 | + "* Build a data pipeline with `tf.data.Dataset`." |
77 | 77 | ]
|
78 | 78 | },
|
79 | 79 | {
|
|
106 | 106 | "source": [
|
107 | 107 | "## Tensors\n",
|
108 | 108 | "\n",
|
109 |
| - "A Tensor is a multi-dimensional array. Similar to NumPy `ndarray` objects, `tf.Tensor` objects have a data type and a shape. Additionally, `tf.Tensor`s can reside in accelerator memory (like a GPU). TensorFlow offers a rich library of operations ([tf.add](https://www.tensorflow.org/api_docs/python/tf/add), [tf.matmul](https://www.tensorflow.org/api_docs/python/tf/matmul), [tf.linalg.inv](https://www.tensorflow.org/api_docs/python/tf/linalg/inv) etc.) that consume and produce `tf.Tensor`s. These operations automatically convert built-in Python types, for example:\n" |
| 109 | + "A Tensor is a multi-dimensional array. Similar to NumPy `ndarray` objects, `tf.Tensor` objects have a data type and a shape. Additionally, `tf.Tensor`s can reside in accelerator memory (like a GPU). TensorFlow offers a rich library of operations (for example, `tf.math.add`, `tf.linalg.matmul`, and `tf.linalg.inv`) that consume and produce `tf.Tensor`s. These operations automatically convert built-in Python types. For example:\n" |
110 | 110 | ]
|
111 | 111 | },
|
112 | 112 | {
|
|
118 | 118 | },
|
119 | 119 | "outputs": [],
|
120 | 120 | "source": [
|
121 |
| - "print(tf.add(1, 2))\n", |
122 |
| - "print(tf.add([1, 2], [3, 4]))\n", |
123 |
| - "print(tf.square(5))\n", |
124 |
| - "print(tf.reduce_sum([1, 2, 3]))\n", |
| 121 | + "print(tf.math.add(1, 2))\n", |
| 122 | + "print(tf.math.add([1, 2], [3, 4]))\n", |
| 123 | + "print(tf.math.square(5))\n", |
| 124 | + "print(tf.math.reduce_sum([1, 2, 3]))\n", |
125 | 125 | "\n",
|
126 | 126 | "# Operator overloading is also supported\n",
|
127 |
| - "print(tf.square(2) + tf.square(3))" |
| 127 | + "print(tf.math.square(2) + tf.math.square(3))" |
128 | 128 | ]
|
129 | 129 | },
|
130 | 130 | {
|
|
144 | 144 | },
|
145 | 145 | "outputs": [],
|
146 | 146 | "source": [
|
147 |
| - "x = tf.matmul([[1]], [[2, 3]])\n", |
| 147 | + "x = tf.linalg.matmul([[1]], [[2, 3]])\n", |
148 | 148 | "print(x)\n",
|
149 | 149 | "print(x.shape)\n",
|
150 | 150 | "print(x.dtype)"
|
|
168 | 168 | "id": "Dwi1tdW3JBw6"
|
169 | 169 | },
|
170 | 170 | "source": [
|
171 |
| - "### NumPy Compatibility\n", |
| 171 | + "### NumPy compatibility\n", |
172 | 172 | "\n",
|
173 | 173 | "Converting between a TensorFlow `tf.Tensor`s and a NumPy `ndarray` is easy:\n",
|
174 | 174 | "\n",
|
|
191 | 191 | "ndarray = np.ones([3, 3])\n",
|
192 | 192 | "\n",
|
193 | 193 | "print(\"TensorFlow operations convert numpy arrays to Tensors automatically\")\n",
|
194 |
| - "tensor = tf.multiply(ndarray, 42)\n", |
| 194 | + "tensor = tf.math.multiply(ndarray, 42)\n", |
195 | 195 | "print(tensor)\n",
|
196 | 196 | "\n",
|
197 | 197 | "\n",
|
198 |
| - "print(\"And NumPy operations convert Tensors to numpy arrays automatically\")\n", |
| 198 | + "print(\"And NumPy operations convert Tensors to NumPy arrays automatically\")\n", |
199 | 199 | "print(np.add(tensor, 1))\n",
|
200 | 200 | "\n",
|
201 | 201 | "print(\"The .numpy() method explicitly converts a Tensor to a numpy array\")\n",
|
|
210 | 210 | "source": [
|
211 | 211 | "## GPU acceleration\n",
|
212 | 212 | "\n",
|
213 |
| - "Many TensorFlow operations are accelerated using the GPU for computation. Without any annotations, TensorFlow automatically decides whether to use the GPU or CPU for an operation—copying the tensor between CPU and GPU memory, if necessary. Tensors produced by an operation are typically backed by the memory of the device on which the operation executed, for example:" |
| 213 | + "Many TensorFlow operations are accelerated using the GPU for computation. Without any annotations, TensorFlow automatically decides whether to use the GPU or CPU for an operation—copying the tensor between CPU and GPU memory, if necessary. Tensors produced by an operation are typically backed by the memory of the device on which the operation executed. For example:" |
214 | 214 | ]
|
215 | 215 | },
|
216 | 216 | {
|
|
237 | 237 | "id": "vpgYzgVXW2Ud"
|
238 | 238 | },
|
239 | 239 | "source": [
|
240 |
| - "### Device Names\n", |
| 240 | + "### Device names\n", |
241 | 241 | "\n",
|
242 | 242 | "The `Tensor.device` property provides a fully qualified string name of the device hosting the contents of the tensor. This name encodes many details, such as an identifier of the network address of the host on which this program is executing and the device within that host. This is required for distributed execution of a TensorFlow program. The string ends with `GPU:<N>` if the tensor is placed on the `N`-th GPU on the host."
|
243 | 243 | ]
|
|
248 | 248 | "id": "ZWZQCimzuqyP"
|
249 | 249 | },
|
250 | 250 | "source": [
|
251 |
| - "### Explicit Device Placement\n", |
| 251 | + "### Explicit device placement\n", |
252 | 252 | "\n",
|
253 |
| - "In TensorFlow, *placement* refers to how individual operations are assigned (placed on) a device for execution. As mentioned, when there is no explicit guidance provided, TensorFlow automatically decides which device to execute an operation and copies tensors to that device, if needed. However, TensorFlow operations can be explicitly placed on specific devices using the `tf.device` context manager, for example:" |
| 253 | + "In TensorFlow, *placement* refers to how individual operations are assigned (placed on) a device for execution. As mentioned, when there is no explicit guidance provided, TensorFlow automatically decides which device to execute an operation and copies tensors to that device, if needed.\n", |
| 254 | + "\n", |
| 255 | + "However, TensorFlow operations can be explicitly placed on specific devices using the `tf.device` context manager. For example:" |
254 | 256 | ]
|
255 | 257 | },
|
256 | 258 | {
|
|
266 | 268 | "def time_matmul(x):\n",
|
267 | 269 | " start = time.time()\n",
|
268 | 270 | " for loop in range(10):\n",
|
269 |
| - " tf.matmul(x, x)\n", |
| 271 | + " tf.linalg.matmul(x, x)\n", |
270 | 272 | "\n",
|
271 | 273 | " result = time.time()-start\n",
|
272 | 274 | "\n",
|
|
296 | 298 | "source": [
|
297 | 299 | "## Datasets\n",
|
298 | 300 | "\n",
|
299 |
| - "This section uses the [`tf.data.Dataset` API](https://www.tensorflow.org/guide/datasets) to build a pipeline for feeding data to your model. The `tf.data.Dataset` API is used to build performant, complex input pipelines from simple, re-usable pieces that will feed your model's training or evaluation loops." |
| 301 | + "This section uses the [`tf.data.Dataset` API](../../guide/data.ipynb) to build a pipeline for feeding data to your model. `tf.data.Dataset` is used to build performant, complex input pipelines from simple, re-usable pieces that will feed your model's training or evaluation loops." |
300 | 302 | ]
|
301 | 303 | },
|
302 | 304 | {
|
|
307 | 309 | "source": [
|
308 | 310 | "### Create a source `Dataset`\n",
|
309 | 311 | "\n",
|
310 |
| - "Create a *source* dataset using one of the factory functions like [`Dataset.from_tensors`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensors), [`Dataset.from_tensor_slices`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#from_tensor_slices), or using objects that read from files like [`TextLineDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TextLineDataset) or [`TFRecordDataset`](https://www.tensorflow.org/api_docs/python/tf/data/TFRecordDataset). See the [TensorFlow Dataset guide](https://www.tensorflow.org/guide/datasets#reading_input_data) for more information." |
| 312 | + "Create a *source* dataset using one of the factory functions like `tf.data.Dataset.from_tensors`, `tf.data.Dataset.from_tensor_slices`, or using objects that read from files like `tf.data.TextLineDataset` or `tf.data.TFRecordDataset`. Refer to the _Reading input data_ section of the [tf.data: Build TensorFlow input pipelines](../../guide/data.ipynb) guide for more information." |
311 | 313 | ]
|
312 | 314 | },
|
313 | 315 | {
|
|
341 | 343 | "source": [
|
342 | 344 | "### Apply transformations\n",
|
343 | 345 | "\n",
|
344 |
| - "Use the transformations functions like [`map`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#map), [`batch`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#batch), and [`shuffle`](https://www.tensorflow.org/api_docs/python/tf/data/Dataset#shuffle) to apply transformations to dataset records." |
| 346 | + "Use the transformations functions like `tf.data.Dataset.map`, `tf.data.Dataset.batch`, and `tf.data.Dataset.shuffle` to apply transformations to dataset records." |
345 | 347 | ]
|
346 | 348 | },
|
347 | 349 | {
|
|
352 | 354 | },
|
353 | 355 | "outputs": [],
|
354 | 356 | "source": [
|
355 |
| - "ds_tensors = ds_tensors.map(tf.square).shuffle(2).batch(2)\n", |
| 357 | + "ds_tensors = ds_tensors.map(tf.math.square).shuffle(2).batch(2)\n", |
356 | 358 | "\n",
|
357 | 359 | "ds_file = ds_file.batch(2)"
|
358 | 360 | ]
|
|
0 commit comments