See the [Variables Guide](https://tensorflow.org/guide/variables). A variable maintains state in the graph across calls to `run()`. You add a variable to the graph by constructing an instance of the class `Variable`. The `Variable()` constructor requires an initial value for the variable,
| 466 | |
| 467 | @tf_export("Variable", v1=[]) |
| 468 | class Variable(six.with_metaclass(VariableMetaclass, trackable.Trackable)): |
| 469 | """See the [Variables Guide](https://tensorflow.org/guide/variables). |
| 470 | |
| 471 | A variable maintains state in the graph across calls to `run()`. You add a |
| 472 | variable to the graph by constructing an instance of the class `Variable`. |
| 473 | |
| 474 | The `Variable()` constructor requires an initial value for the variable, |
| 475 | which can be a `Tensor` of any type and shape. The initial value defines the |
| 476 | type and shape of the variable. After construction, the type and shape of |
| 477 | the variable are fixed. The value can be changed using one of the assign |
| 478 | methods. |
| 479 | |
| 480 | If you want to change the shape of a variable later you have to use an |
| 481 | `assign` Op with `validate_shape=False`. |
| 482 | |
| 483 | Just like any `Tensor`, variables created with `Variable()` can be used as |
| 484 | inputs for other Ops in the graph. Additionally, all the operators |
| 485 | overloaded for the `Tensor` class are carried over to variables, so you can |
| 486 | also add nodes to the graph by just doing arithmetic on variables. |
| 487 | |
| 488 | ```python |
| 489 | import tensorflow as tf |
| 490 | |
| 491 | # Create a variable. |
| 492 | w = tf.Variable(<initial-value>, name=<optional-name>) |
| 493 | |
| 494 | # Use the variable in the graph like any Tensor. |
| 495 | y = tf.matmul(w, ...another variable or tensor...) |
| 496 | |
| 497 | # The overloaded operators are available too. |
| 498 | z = tf.sigmoid(w + y) |
| 499 | |
| 500 | # Assign a new value to the variable with `assign()` or a related method. |
| 501 | w.assign(w + 1.0) |
| 502 | w.assign_add(1.0) |
| 503 | ``` |
| 504 | |
| 505 | When you launch the graph, variables have to be explicitly initialized before |
| 506 | you can run Ops that use their value. You can initialize a variable by |
| 507 | running its *initializer op*, restoring the variable from a save file, or |
| 508 | simply running an `assign` Op that assigns a value to the variable. In fact, |
| 509 | the variable *initializer op* is just an `assign` Op that assigns the |
| 510 | variable's initial value to the variable itself. |
| 511 | |
| 512 | ```python |
| 513 | # Launch the graph in a session. |
| 514 | with tf.compat.v1.Session() as sess: |
| 515 | # Run the variable initializer. |
| 516 | sess.run(w.initializer) |
| 517 | # ...you now can run ops that use the value of 'w'... |
| 518 | ``` |
| 519 | |
| 520 | The most common initialization pattern is to use the convenience function |
| 521 | `global_variables_initializer()` to add an Op to the graph that initializes |
| 522 | all the variables. You then run that Op after launching the graph. |
| 523 | |
| 524 | ```python |
| 525 | # Add an Op to initialize global variables. |
no outgoing calls