<jupyter_start><jupyter_text>TensorFlow for Artificial Intelligence - Initial steps- Basics- The Computation Graph   - Activation Functions- Initialization - Optimization- Feature Overview- TensorBoard- TensorFlow Serving- TFLearn- Distributed TensorFlow - TensorFlow on Android - Solving PDE’s- Other worked examples- Future Roadmap - Keras Integration PreambleThis chapter gives you an overview of TensorFlow's basics, capabilities, and ecosystem. We start with a brief history behind the development of TensorFlow to set the context.<jupyter_code>import tensorflow as tf
tf.__version__
# TODO: does this initialize variables?
sess = tf.InteractiveSession()
a = tf.constant(0)
with tf.Session() as sess:
    sess.run(a)
    print(a)
    print(a.dtype)
    print(a.shape)
    print(a.get_shape())
    print(a.graph)
    print(a.device)
    print(a.eval())
b = tf.constant(1)
c = a + b
c.eval()
with tf.Session() as sess:
    c = sess.run(a + b)
    print(c)
d = tf.multiply(a, b)
d
d.eval()
x = tf.random_uniform(
                                 [10, 3],
                                 minval=0,
                                 maxval=100,
                                 dtype=tf.int32
                             )
x
# variables
random_uniform = tf.Variable('random_int', initializer=x)
random_int = tf.get_variable('random_int2', initializer=x)

# get_variable vs. Variable???
# get_variable_scope?
# ValueError: Variable random_int2 already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined
random_int = tf.get_variable('random_int2', initializer=x)
x.eval()
x1 = tf.random_uniform([10, 3], minval=0, maxval=100, dtype=tf.int32, seed=0)
tf.AUTO_REUSE = True
tf.AUTO_REUSE
random_int = tf.get_variable('random_int2', initializer=x1)
# TODO: What is variable reusing?
# TODO: Is this always necessary?
tf.global_variables_initializer()
x.eval()
with tf.Session() as sess:
    tf.global_variables_initializer()
    sess.run(random_int)
random_int_var_one_ex = tf.get_variable("random_int_var_one",
                                        initializer=tf.random_uniform([2, 3, 4],
                                        minval=0,
                                        maxval=10,
                                        dtype=tf.int32))
print(random_int_var_one_ex)
sess = tf.Session()
sess.run(tf.global_variables_initializer())
print(sess.run(random_int_var_one_ex))
random_int_var_one_ex.name
sess.run(tf.get_default_graph().get_tensor_by_name(random_int_var_one_ex.name))
sess.close()
from graphviz import Digraph

def tf_to_dot(graph):
    dot = Digraph()

    for n in graph.as_graph_def().node:
        dot.node(n.name, label=n.name)

        for i in n.input:
            dot.edge(i, n.name)
            
    return dot<jupyter_output><empty_output><jupyter_text>Perceptron v1<jupyter_code>import numpy as np
tf.reset_default_graph()
tf_to_dot(tf.get_default_graph())
x = tf.placeholder(tf.float32, [1, 2], name='x')
print(x)
tf_to_dot(tf.get_default_graph())
init = tf.random_normal([1, 2])
print(init)
tf_to_dot(tf.get_default_graph())
w = tf.Variable(init, name='w')
print(w)
tf_to_dot(tf.get_default_graph())
y = w * x
print(y)
tf_to_dot(tf.get_default_graph())
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    w_current = sess.run(w)
    a = sess.run(y, feed_dict={x: np.array([[1, 2]])})
    print(w_current)
    print(a)
tf_to_dot(tf.get_default_graph())<jupyter_output><empty_output><jupyter_text>Perceptron v2Random init is too complicated<jupyter_code>tf.reset_default_graph()
tf_to_dot(tf.get_default_graph())
x = tf.placeholder(tf.float32, [1, 2], name='x')
print(x)
tf_to_dot(tf.get_default_graph())
w = tf.Variable(tf.ones([1, 2]), name='w')
print(w)
tf_to_dot(tf.get_default_graph())
y = x * w
print(y)
tf_to_dot(tf.get_default_graph())
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    w_current = sess.run(w)
    a = sess.run(y, feed_dict={x: np.array([[1, 2]])})
    print(w_current)
    print(a)<jupyter_output>[[ 1.  1.]]
[[ 1.  2.]]<jupyter_text>Training<jupyter_code>tf.reset_default_graph()
w = tf.Variable(tf.ones([1, 2]), name='w')
delta = tf.constant(value=0.5, shape=[1, 2])
tf_to_dot(tf.get_default_graph())
w += delta
tf_to_dot(tf.get_default_graph())
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    w_current = sess.run(w)
    print(w_current)
# Graph construction
# Init
# Run<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12833517089305402, 'fraction_numerical': 0.013891951488423374, 'mean_word_length': 3.581818181818182, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '202617', 'license': 'MIT', 'n_tokens_mistral': 1656, 'n_tokens_neox': 1605, 'n_words': 330, 'path': 'chapter2-tensorflow-for-ai/tensorflow-for-ai.ipynb', 'repo_name': 'amirziai/ai-with-tensorflow', 'size': '4535'}