|
1167 | 1167 | "good_consume_next(iterator)"
|
1168 | 1168 | ]
|
1169 | 1169 | },
|
1170 |
| - { |
1171 |
| - "cell_type": "markdown", |
1172 |
| - "metadata": { |
1173 |
| - "id": "FHQ0UeU-vWo8" |
1174 |
| - }, |
1175 |
| - "source": [ |
1176 |
| - "### Deleting tf.Variables between `Function` calls\n", |
1177 |
| - "\n", |
1178 |
| - "Another error you may encounter is a garbage-collected variable. `ConcreteFunction`s only retain [WeakRefs](https://docs.python.org/3/library/weakref.html) to the variables they close over, so you must retain a reference to any variables." |
1179 |
| - ] |
1180 |
| - }, |
1181 |
| - { |
1182 |
| - "cell_type": "code", |
1183 |
| - "execution_count": null, |
1184 |
| - "metadata": { |
1185 |
| - "id": "uMiRPfETjpt-" |
1186 |
| - }, |
1187 |
| - "outputs": [], |
1188 |
| - "source": [ |
1189 |
| - "external_var = tf.Variable(3)\n", |
1190 |
| - "@tf.function\n", |
1191 |
| - "def f(x):\n", |
1192 |
| - " return x * external_var\n", |
1193 |
| - "\n", |
1194 |
| - "traced_f = f.get_concrete_function(4)\n", |
1195 |
| - "print(\"Calling concrete function...\")\n", |
1196 |
| - "print(traced_f(4))\n", |
1197 |
| - "\n", |
1198 |
| - "# The original variable object gets garbage collected, since there are no more\n", |
1199 |
| - "# references to it.\n", |
1200 |
| - "external_var = tf.Variable(4)\n", |
1201 |
| - "print()\n", |
1202 |
| - "print(\"Calling concrete function after garbage collecting its closed Variable...\")\n", |
1203 |
| - "with assert_raises(tf.errors.FailedPreconditionError):\n", |
1204 |
| - " traced_f(4)" |
1205 |
| - ] |
1206 |
| - }, |
1207 | 1170 | {
|
1208 | 1171 | "cell_type": "markdown",
|
1209 | 1172 | "metadata": {
|
|
1379 | 1342 | {
|
1380 | 1343 | "cell_type": "markdown",
|
1381 | 1344 | "metadata": {
|
1382 |
| - "id": "Tu0SnPwaL7pI" |
| 1345 | + "id": "ZoPg5w1Pjqnb" |
1383 | 1346 | },
|
1384 | 1347 | "source": [
|
1385 |
| - "You can close over outer names, as long as you don't update their values.\n" |
| 1348 | + "Another way to update a global value, is to make it a `tf.Variable` and use the `Variable.assign` method instead.\n" |
| 1349 | + ] |
| 1350 | + }, |
| 1351 | + { |
| 1352 | + "cell_type": "code", |
| 1353 | + "execution_count": null, |
| 1354 | + "metadata": { |
| 1355 | + "id": "oeJMdXd3M0cc" |
| 1356 | + }, |
| 1357 | + "outputs": [], |
| 1358 | + "source": [ |
| 1359 | + "@tf.function\n", |
| 1360 | + "def variable_add():\n", |
| 1361 | + " return 1 + foo\n", |
| 1362 | + "\n", |
| 1363 | + "foo = tf.Variable(1)\n", |
| 1364 | + "print(\"Variable:\", variable_add())\n" |
| 1365 | + ] |
| 1366 | + }, |
| 1367 | + { |
| 1368 | + "cell_type": "code", |
| 1369 | + "execution_count": null, |
| 1370 | + "metadata": { |
| 1371 | + "id": "L3q7sUJWZOSd" |
| 1372 | + }, |
| 1373 | + "outputs": [], |
| 1374 | + "source": [ |
| 1375 | + "print(\"Updating the value of `foo` to 100!\")\n", |
| 1376 | + "foo.assign(100)\n", |
| 1377 | + "print(\"Variable:\", variable_add())" |
1386 | 1378 | ]
|
1387 | 1379 | },
|
1388 | 1380 | {
|
|
0 commit comments