withtf.Graph().as_default():withtf.compat.v1.Session()assess:v=tf.Variable(0.0)a=tf.constant(1.0)sess.run(tf.compat.v1.global_variables_initializer())foriinrange(5):update_op=v.assign_add(1.0)calc=[a+v]# `tf.tuple` ensures `update_op` is run before `b`b=tf.tuple(calc,[tf.group(update_op)])res_b=sess.run(b)res_v=sess.run(v)print(res_v)1.02.03.04.05.0
Args
tensors
A list of Tensors or IndexedSlices, some entries can be None.
control_inputs
List of additional ops to finish before returning.
name
(optional) A name to use as a name_scope for the operation.
Returns
Same as tensors.
Raises
ValueError
If tensors does not contain any Tensor or IndexedSlices.
TypeError
If control_inputs is not a list of Operation or Tensor
objects.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-04-26 UTC."],[],[],null,["# tf.tuple\n\n\u003cbr /\u003e\n\n|--------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.16.1/tensorflow/python/ops/control_flow_ops.py#L2037-L2103) |\n\nGroups tensors together. \n\n tf.tuple(\n tensors, control_inputs=None, name=None\n )\n\nThe returned tensors have the same value as the input tensors, but they\nare computed only after all the input tensors have been computed.\n| **Note:** *In TensorFlow 2 with eager and/or Autograph, you should not require\n| this method, as ops execute in the expected order thanks to automatic control\n| dependencies.* Only use [`tf.tuple`](../tf/tuple) when working with v1 [`tf.Graph`](../tf/Graph) code.\n\nSee also [`tf.group`](../tf/group) and [`tf.control_dependencies`](../tf/control_dependencies).\n\n#### Example:\n\n with tf.Graph().as_default():\n with tf.compat.v1.Session() as sess:\n v = tf.Variable(0.0)\n a = tf.constant(1.0)\n sess.run(tf.compat.v1.global_variables_initializer())\n for i in range(5):\n update_op = v.assign_add(1.0)\n b = a + v\n res_b = sess.run(b)\n res_v = sess.run(v)\n print(res_v)\n 0.0\n 0.0\n 0.0\n 0.0\n 0.0\n\n with tf.Graph().as_default():\n with tf.compat.v1.Session() as sess:\n v = tf.Variable(0.0)\n a = tf.constant(1.0)\n sess.run(tf.compat.v1.global_variables_initializer())\n for i in range(5):\n update_op = v.assign_add(1.0)\n calc = [a + v]\n # `tf.tuple` ensures `update_op` is run before `b`\n b = tf.tuple(calc, [tf.group(update_op)])\n res_b = sess.run(b)\n res_v = sess.run(v)\n print(res_v)\n 1.0\n 2.0\n 3.0\n 4.0\n 5.0\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|------------------|---------------------------------------------------------------------|\n| `tensors` | A list of `Tensor`s or `IndexedSlices`, some entries can be `None`. |\n| `control_inputs` | List of additional ops to finish before returning. |\n| `name` | (optional) A name to use as a `name_scope` for the operation. |\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Returns ------- ||\n|---|---|\n| Same as `tensors`. ||\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Raises ------ ||\n|--------------|-----------------------------------------------------------------------|\n| `ValueError` | If `tensors` does not contain any `Tensor` or `IndexedSlices`. |\n| `TypeError` | If `control_inputs` is not a list of `Operation` or `Tensor` objects. |\n\n\u003cbr /\u003e"]]