|
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 (for example, `tf.math.add`, `tf.matmul`, and `tf.linalg.inv`) 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)"
|
|
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",
|
|
268 | 268 | "def time_matmul(x):\n",
|
269 | 269 | " start = time.time()\n",
|
270 | 270 | " for loop in range(10):\n",
|
271 |
| - " tf.matmul(x, x)\n", |
| 271 | + " tf.linalg.matmul(x, x)\n", |
272 | 272 | "\n",
|
273 | 273 | " result = time.time()-start\n",
|
274 | 274 | "\n",
|
|
354 | 354 | },
|
355 | 355 | "outputs": [],
|
356 | 356 | "source": [
|
357 |
| - "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", |
358 | 358 | "\n",
|
359 | 359 | "ds_file = ds_file.batch(2)"
|
360 | 360 | ]
|
|
0 commit comments