<jupyter_start><jupyter_text>![alt text](https://www.tensorflow.org/images/tf_logo_social.png)  Tensorflow**TensorFlow is a powerful open source software library for numerical computation, particularly well suited and fine-tuned for large-scale Machine Learning**<jupyter_code># !pip3 install --upgrade tensorflow<jupyter_output><empty_output><jupyter_text>**install TensorFlow**<jupyter_code># !pip3 install tensorflow<jupyter_output><empty_output><jupyter_text>For GPU support**you need to install *tensorflow-gpu* instead of tensorflow**<jupyter_code># !pip3 install tensorflow-gpu<jupyter_output><empty_output><jupyter_text>To test your installation**type the following command. It should output the version of TensorFlow youinstalled.**<jupyter_code>!python3 -c 'import tensorflow; print(tensorflow.__version__)'<jupyter_output>1.14.0<jupyter_text>***Creating Your First Graph and Running It in a Session***<jupyter_code>import tensorflow as tf
from tensorflow_graph_in_jupyter import show_graph
import numpy as np
# to make this notebook's output stable across runs
def reset_graph(seed=42):
    tf.reset_default_graph()
    tf.set_random_seed(seed)
    np.random.seed(seed)
reset_graph()
a = tf.add(3, 5)
a
import tensorflow as tf
a = tf.add(3, 5)
sess = tf.Session()
print(sess.run(a))
sess.close()
show_graph(tf.get_default_graph())
import tensorflow as tf
a = tf.add(3, 5)
with tf.Session() as sess:
    print(sess.run(a))<jupyter_output>8<jupyter_text>tf.mul, tf.sub and tf.neg are deprecated in favor of tf.multiply, tf.subtract and tf.negative.<jupyter_code>x = 2
y = 3
op1 = tf.add(x, y)
op2 = tf.multiply(x, y)
op3 = tf.pow(op2, op1)
with tf.Session() as sess:
    op3 = sess.run(op3)
    print(op3)
x = 2
y = 3
reset_graph()
add_op = tf.add(x, y)
mul_op = tf.multiply(x, y)
useless = tf.multiply(x, add_op)
pow_op = tf.pow(add_op, mul_op)

with tf.Session() as sess:
    z = sess.run(pow_op)
z
show_graph(tf.get_default_graph())
#reset_graph()

c = tf.constant(4.0)
print(c.graph is tf.get_default_graph())

g = tf.Graph()
with g.as_default():
  # Define operations and tensors in `g`.
  c = tf.constant(30.0)
  print(c.graph is g)

print(c.graph is tf.get_default_graph())


# g = tf.Graph()
# with g.as_default():
#     x = tf.add(3, 5)
# sess = tf.Session(graph=g)
# with tf.Session() as sess:
#     sess.run(tf.global_variables_initializer())
#     sess.run(x)
# 1. Using Graph.as_default():
g1 = tf.Graph()
with g1.as_default():
  c = tf.constant(5.0)
  print( c.graph is g1)

print( c.graph is g1)
print( c.graph is tf.get_default_graph(), "*****")
# 2. Constructing and making default:
with tf.Graph().as_default() as g2:
  d = tf.constant(10.0)
  print( d.graph is g2)

print( d.graph is g2)
print( d.graph is tf.get_default_graph(), "*****")

print( c.graph is g2)
print( d.graph is g1)
with tf.Session( graph = g1 ) as sess:
    result = sess.run( c )
    print( result )
    
with tf.Session( graph = g2 ) as sess:
    result = sess.run( d )
    print( result )

# with tf.Session( graph = g2 ) as sess:
#     result = sess.run( c )
#     print( result )
 
# with tf.Session() as sess:
#     print(sess.run( c ))

# with tf.Session() as sess:
#     print(sess.run( d ))


#import tensorflow as tf
reset_graph()

x = tf.Variable(3, name = "X")
y = tf.Variable(4, name = "Y")

f = x*x*y + y + 2<jupyter_output><empty_output><jupyter_text>1. It is just creates a computational graph. In fact, even the variables are not initialized.2. To evaluate this graph, you need to open a Tensorflow session and useit to initialize the variables and evaluate f<jupyter_code>f

# creates a session, initializes the variables, and evaluates
# closes the session
sess = tf.Session()
sess.run(x.initializer)
sess.run(y.initializer)
result = sess.run(f)
print(result)
sess.close()
show_graph(tf.get_default_graph())
#Having to repeat sess.run() all the time is a bit cumbersome, 
#but fortunately there is a better way:

with tf.Session() as sess:
  x.initializer.run()
  y.initializer.run()
  result = f.eval()
result<jupyter_output><empty_output><jupyter_text>1. Calling x.initializer.run() isequivalent to calling tf.get_default_session().run(x.initializer), and similarly f.eval() isequivalent to calling tf.get_default_session().run(f). 2. The session is automatically closed at the end of the block.3. Instead of manually running the initializer for every single variable, you can use the*global_variables_initializer()* function.4. Note that it does not actually perform the initializationimmediately, but rather creates ***a node in the graph that will initialize all variables when it is run***<jupyter_code>init = tf.global_variables_initializer() #prepare an init mode
with tf.Session() as sess:
  init.run() #actually initialize all the variables
  #sess.run(init)
  result = f.eval()
result<jupyter_output><empty_output><jupyter_text>1. **Inside Jupyter or within a Python shell** you may prefer to **create an InteractiveSession**. 2. The onlydifference from a regular Session is that when an InteractiveSession is created it **automatically setsitself as the default session** , so you don’t need a with block<jupyter_code>init = tf.global_variables_initializer()
sess = tf.InteractiveSession()
init.run()
result = f.eval()
print(result)
f.eval()<jupyter_output><empty_output><jupyter_text>Tensorflow Program is typically split into two parts1. the first part builds a computation graph. this is called the **construction phase**. build computational graph.2. The second part runs it. this is the **execution phase**.. trainig process, model architecture.  Managing Graphs Any node you create is automatically added to the default graph<jupyter_code>a = tf.Variable(3)
a
a.graph is tf.get_default_graph()<jupyter_output><empty_output><jupyter_text>In most cases this is fine, but 1. sometimes you may want to **manage multiple independent graphs**.2. You can dothis by creating a new Graph and temporarily making it the default graph inside a with block<jupyter_code>graph = tf.Graph()

with graph.as_default():
  a = tf.Variable(3)  
a.graph is graph
a.graph is tf.get_default_graph()<jupyter_output><empty_output><jupyter_text>1. In Jupyter (or in a Python shell), it is common to run the **same commands more than once** while you are experimenting.2. As a result, you may end up with a default graph **containing many duplicate nodes**. 3. One solution is to **restart the Jupyter kernel** (or thePython shell), but 4. a more convenient solution is to just reset the default graph by running **tf.reset_default_graph()**.  Lifecycle of a Node Value in Tensorflow 1. TensorFlow automatically determines the set of nodes that it depends on and itevaluates these nodes 2. Avariable starts its life when its initializer is run, and it ends when the session is closed.<jupyter_code>w = tf.constant(3)
x = w + 2
y = x + 5
z = x * 3

with tf.Session() as sess:
    print(y.eval())  # 10
    print(z.eval())  # 15<jupyter_output>10
15<jupyter_text>if you want to evaluate y and z efficiently, without evaluating w and x twice as in the previous code, youmust ask TensorFlow to evaluate both y and z in just one graph run<jupyter_code>with tf.Session() as sess:
  y, z = sess.run([y, z])
y, z<jupyter_output><empty_output><jupyter_text>1. In **single-process** TensorFlow, multiple sessions **do not share any state**, even if they reuse the same graph2. In distributed TensorFlow, **variable state is stored on the servers**, not inthe sessions, so **multiple sessions can share the same variables**.<jupyter_code>import tensorflow as tf
N = tf.Variable(29)
P = tf.Variable(100 * N)

# NP = N * P

with tf.Session() as sess:
  sess.run(N.initializer)
  sess.run(P.initializer)
  N = N.eval()
  P = P.eval()
  print(N, P)
import tensorflow as tf
N = tf.Variable(29)
P = tf.Variable(100)

NP = N * P
init = tf.global_variables_initializer()

with tf.Session() as sess:
  sess.run(init)
  N = N.eval()
  NP = sess.run(NP)

  print(n, NP)
import tensorflow as tf
X = tf.placeholder(tf.float32, shape=[3], name = "X")  
Y = tf.Variable([2.0, 4.0, 6.0], tf.float32)
Z = tf.constant([1, 2, 3], tf.float32)

res = X * Y + Z
#sess = tf.InteractiveSession()

init = tf.global_variables_initializer()
with tf.Session() as sess:
    X_val = [3, 6, 9]
    sess.run(init)

    result = sess.run(res, feed_dict={X:X_val})
    Y_val = Y.eval()
    Z_val = Z.eval()

    print(X_val, Y_val, Z_val, result)<jupyter_output><empty_output><jupyter_text>Visualization it with TensorBoard  1. tf.summary.FileWriter('./graphs', sess.graph) 2. tensorboard --logdir="./graphs" --port 6006<jupyter_code>import tensorflow as tf
X = tf.placeholder(tf.float32, shape=[3], name = "X")  
Y = tf.Variable([2.0, 4.0, 6.0], tf.float32)
Z = tf.constant([1, 2, 3], tf.float32)

res = X * Y + Z
#sess = tf.InteractiveSession()

init = tf.global_variables_initializer()
with tf.Session() as sess:
    X_val = [3, 6, 9]
    sess.run(init)
    
    writer = tf.summary.FileWriter('./graphs', sess.graph)

    result = sess.run(res, feed_dict={X:X_val})
    Y_val = Y.eval()
    Z_val = Z.eval()

    print(X_val, Y_val, Z_val, result)

writer.close()<jupyter_output>WARNING:tensorflow:Issue encountered when serializing variables.
Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore.
tf.float32 has type DType, but expected one of: int, long, bool
WARNING:tensorflow:Issue encountered when serializing trainable_variables.
Type is unsupported, or the types of the items don't match field type in CollectionDef. Note this is a warning and probably safe to ignore.
tf.float32 has type DType, but expected one of: int, long, bool
[3, 6, 9] [2. 4. 6.] [1. 2. 3.] [ 7. 26. 57.]
{'fraction_non_alphanumeric': 0.11995481154359659, 'fraction_numerical': 0.01684297011399815, 'mean_word_length': 4.551881413911061, 'pattern_counts': {'":': 0, '<': 57, '<?xml version=': 0, '>': 57, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '362837', 'license': 'Apache-2.0', 'n_tokens_mistral': 3459, 'n_tokens_neox': 3299, 'n_words': 1102, 'path': 'Tensorflow_Tutorial_1.0.ipynb', 'repo_name': 'dhana-sekhar/Tensorflow', 'size': '9737'}