Skip to content

Commit 9aa0832

Browse files
Prepare for RefCounting Anonymous Variables.
The documented case is no longer relevant. After cl/401590080, tf.function captured anonymous variables hold strong reference to the variables via the handle tensors. Shall be submitted together with cl/401590080. PiperOrigin-RevId: 406882396
1 parent b9a9955 commit 9aa0832

File tree

1 file changed

+31
-39
lines changed

1 file changed

+31
-39
lines changed

site/en/guide/function.ipynb

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,43 +1167,6 @@
11671167
"good_consume_next(iterator)"
11681168
]
11691169
},
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-
},
12071170
{
12081171
"cell_type": "markdown",
12091172
"metadata": {
@@ -1379,10 +1342,39 @@
13791342
{
13801343
"cell_type": "markdown",
13811344
"metadata": {
1382-
"id": "Tu0SnPwaL7pI"
1345+
"id": "ZoPg5w1Pjqnb"
13831346
},
13841347
"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())"
13861378
]
13871379
},
13881380
{

0 commit comments

Comments
 (0)