<jupyter_start><jupyter_text>Table of Contents 1&nbsp;&nbsp;Tensors1.1&nbsp;&nbsp;Basic Usage1.2&nbsp;&nbsp;Additional Resources2&nbsp;&nbsp;Variables2.1&nbsp;&nbsp;Basic Usage2.2&nbsp;&nbsp;Important to Remember2.3&nbsp;&nbsp;Additional Resources<jupyter_code>import tensorflow as tf
from pprint import pprint<jupyter_output><empty_output><jupyter_text>TensorsA tf.Tensor object represents a partially defined computation that will eventually produce a value. TensorFlow programs work by first building a graph of tf.Tensor objects, detailing how each tensor is computed based on the other available tensors and then by running parts of this graph to achieve the desired results.  Basic Usage<jupyter_code>three = tf.constant(3)
print('Constant Tensor that evaluates to 3:')
print(three)

print('\nAccessing a tensor\'s shape and data type:')
print('Shape:', three.shape)
print('Data type:', three.dtype)<jupyter_output>Constant Tensor that evaluates to 3:
Tensor("Const:0", shape=(), dtype=int32)

Accessing a tensor's shape and data type:
Shape: ()
Data type: <dtype: 'int32'><jupyter_text>Additional Resources* [Tensors](https://www.tensorflow.org/programmers_guide/tensors)* [tf.Tensor](https://www.tensorflow.org/api_docs/python/tf/Tensor)* [Source code: Tensor](https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/python/framework/ops.pyL183)  VariablesA TensorFlow variable is the best way to represent shared, persistent state manipulated by your program.- A tf.Variable represents a tensor whose value can be changed by running ops on it. - A tf.Variable exists outside the context of a single session.run call (unlike tf.Tensor objects).Internally, a tf.Variable stores a persistent tensor. Specific ops allow you to read and modify the values of this tensor. These modifications are visible across multiple tf.Sessions, so multiple workers can see the same values for a tf.Variable.  Basic Usage<jupyter_code>tf.reset_default_graph()
# tf.AUTO_REUSE: create variables if they do not exist, and return them otherwise.
with tf.variable_scope('my_scope', reuse=tf.AUTO_REUSE):
    # Creating a Variable directly via the tf.Variable constructor (old-school).
    var_basic = tf.Variable(initial_value=5, dtype=tf.int32, name='var_basic')
    # Creating a Variable via tf.get_variable (recommended).
    var_get = tf.get_variable('var_get', dtype=tf.int32, initializer=6)
    # Defining a variable whose initial value depends on another variable being initialized.
    var_dep = tf.get_variable('var_dep', initializer=var_get.initialized_value() + 1)

with tf.Session() as sess:
    # Assigns initial values to all global variables.
    # All variables are considered global by default.
    sess.run(tf.global_variables_initializer())
    print('Initialized all global variables:')
    pprint(tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES))
    
    six, seven = sess.run([var_get, var_dep])
    print('\nEvaluated tensors outputs: {}, {}'.format(six, seven))<jupyter_output>Initialized all global variables:
[<tf.Variable 'my_scope/var_basic:0' shape=() dtype=int32_ref>,
 <tf.Variable 'my_scope/var_get:0' shape=() dtype=int32_ref>,
 <tf.Variable 'my_scope/var_dep:0' shape=() dtype=int32_ref>]

Evaluated tensors outputs: 6, 7<jupyter_text>To access a variable again, must call the exact same `get_variable` with the same parameters, and must do so in the same variable scope, as shown below for retrieving `var_get`.<jupyter_code>with tf.variable_scope('my_scope', reuse=tf.AUTO_REUSE):
    print(tf.get_variable('var_get', dtype=tf.int32, initializer=6))<jupyter_output><tf.Variable 'my_scope/var_get:0' shape=() dtype=int32_ref><jupyter_text>Important to RememberBy default every tf.Variable gets placed in the following two collections: * tf.GraphKeys.GLOBAL_VARIABLES --- variables that can be shared across multiple devices, * tf.GraphKeys.TRAINABLE_VARIABLES--- variables for which TensorFlow will calculate gradients.  Additional Resources* [Variables](https://www.tensorflow.org/programmers_guide/variables)* [tf.Variable](https://www.tensorflow.org/api_docs/python/tf/Variable)* [Source code](https://github.com/tensorflow/tensorflow/blob/r1.4/tensorflow/python/ops/variables.pyL36)<jupyter_code>tf.reset_default_graph()
sess = tf.InteractiveSession()
y = tf.get_variable('y', initializer=[4., 5., 6.])
z = y + 3
y.initializer.run()
z.eval()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10992505110152169, 'fraction_numerical': 0.01294571882807177, 'mean_word_length': 6.001589825119237, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 6, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '228579', 'license': 'MIT', 'n_tokens_mistral': 1365, 'n_tokens_neox': 1324, 'n_words': 389, 'path': 'section1/notebooks/TFBasics_TensorsAndVariables.ipynb', 'repo_name': 'joyjeni/-Learn-Artificial-Intelligence-with-TensorFlow', 'size': '4403'}