<jupyter_start><jupyter_text>Your first tensorflow program (SHIFT + ENTER TO EXEC a BLOCK)<jupyter_code>import tensorflow as tf #import tensorflow library

# Build a graph.
a = tf.constant(5.0) 
b = tf.constant(6.0) 
c=a*b
# Launch the graph in a session.
sess = tf.Session()
# Evaluate the tensor `c`.
print(sess.run(c))
#print(c)
sess.close()<jupyter_output>30.0<jupyter_text>Initiate a session: Tf.session()<jupyter_code>import tensorflow as tf #import tensorflow library
# Build a graph.
a = 5 #tf.constant(5.0) 
b = tf.constant(6.0) 
c=a*b
# Launch the graph in a session.
with tf.Session() as sess: 
    # Evaluate the tensor `c`.
    print(sess.run(c))
    
# sess will be closed automatically after "with block"<jupyter_output>30.0<jupyter_text>Feeding the execution graphA graph can be parameterized to accept external inputs  A placeholder is a promise to provide a value later, like a function argument<jupyter_code>x = tf.placeholder(tf.float32) 
y = tf.placeholder(tf.float32) 
z=x+y
with tf.Session() as sess:
    print(sess.run(z, feed_dict={x: 3, y: 4.5}))
    print(sess.run(z, feed_dict={x: [1, 3], y: [2, 4]}))
    print(sess.run(z, feed_dict={x: 30, y: 4.5}))<jupyter_output>7.5
[3. 7.]
34.5<jupyter_text>tf.InteractiveSession  an InteractiveSession installs itself as the default session on construction. The methods tf.Tensor.eval and tf.Operation.runwill use that session to run ops.<jupyter_code>sess = tf.InteractiveSession()
a = tf.constant(5.0)
b = tf.constant(6.0)
c = a * b
# We can just use 'c.eval()' without passing 'sess'
print(c.eval())    
print(sess.run(c))
sess.close()<jupyter_output>30.0<jupyter_text>SubgraphsSession does not evaluate every tensors in the graph<jupyter_code>import tensorflow as tf
x = tf.constant(2, name='x')
y = tf.constant(3, name='y')
z = tf.constant(4, name='z')
x_y = tf.add(x, y, name='x_y')
y_z = tf.add(y, z, name='y_z')
x_y_z = tf.add(x, y_z, name='x_y_z')
x_y_y_z = tf.add(x_y, y_z, name='x_y_y_z')
writer = tf.summary.FileWriter('./graphs', tf.get_default_graph())
with tf.Session() as sess:
    print(sess.run(x_y_y_z))
    print(x_y_z)<jupyter_output>12
Tensor("x_y_z_2:0", shape=(), dtype=int32)<jupyter_text>tf operations  tf.rank<jupyter_code>mammal = tf.Variable("Elephant", tf.string) 
ignition = tf.Variable(451, tf.int16)
floating = tf.Variable(3.14159265359, tf.float64)
mystr = tf.Variable(["Hello"], tf.string)
cool_numbers = tf.Variable([3.14159, 2.71828], tf.float32) 
first_primes = tf.Variable([2, 3, 5, 7, 11], tf.int32)
mymat = tf.Variable([[7],[11]], tf.int16)
myxor = tf.Variable([[False, True],[True, False]], tf.bool) 
linear_squares = tf.Variable([[4], [9], [16], [25]], tf.int32) 
#squarish_squares = tf.Variable([ [4, 9, 0], [16, 25, 12] ], tf.int32)
squarish_squares = tf.Variable([ [4, 9, 0]], tf.int32)
print(tf.rank(mammal))<jupyter_output>Tensor("Rank:0", shape=(), dtype=int32)<jupyter_text>Please use tf.rank(your_tensor) to know ranks of all above tensors?<jupyter_code>with tf.Session() as sess:
    print("cool numbers", sess.run(tf.rank(cool_numbers)))
    print("ignition", sess.run(tf.rank(ignition)))
    print("squarish_squares", sess.run(tf.rank(squarish_squares)))
    print("first_primes", sess.run(tf.rank(first_primes)))
    print("cool numbers", sess.run(tf.shape(squarish_squares)))<jupyter_output>cool numbers 1
ignition 0
squarish_squares 2
first_primes 1
cool numbers [1 3]<jupyter_text>Tensor slices<jupyter_code>my_matrix = tf.constant([[[1,2,3],[3,5,6], [7,8,9], [10,11,12]]])
my_row_vector = my_matrix[0]
# shape mymatrix [1, 4, 3]
my_column_vector = my_matrix[0, 1, 1]
# shape = [1, 4]
with tf.Session() as sess:
    x, y, z = sess.run([my_matrix, my_row_vector, my_column_vector])
    #x, y = sess.run([my_matrix, my_row_vector])
    #print('x=', x)
    #print('y=', y)
    #print('z=', z)
    #print(sess.run([tf.shape(my_matrix)]))
    print(sess.run(tf.shape(my_matrix)))
    print(y)
    print(x)
    print(z)
    print(sess.run(tf.shape(my_column_vector)))
my_matrix = tf.Variable([1,2,3,4,5,6,7,8,9], tf.int32)
my_new_matrix = tf.reshape(my_matrix, [1,3,3])
my_new_matrix_2 = tf.reshape(my_matrix, [3,1,3])
my_new_matrix_3 = tf.reshape(my_matrix, [3,3,1])
with tf.Session() as sess:
    #print(sess.run(my_matrix, feed_dict={my_matrix:[1,2,2,2,2,2,2,2,2]}))
    sess.run(tf.global_variables_initializer())
    print(sess.run(my_matrix))
    #print(sess.run(my_new_matrix))
    #print(sess.run(my_new_matrix_2))
    #print(sess.run(my_new_matrix_3))
    #print(sess.run(tf.reshape(my_new_matrix_3,[9])))<jupyter_output>[1 2 3 4 5 6 7 8 9]<jupyter_text>tf.cond(pred, fn1, fn2, name=None)Return either fn1() or fn2() based on the boolean predicate pred.fn1 and fn2 both return lists of output tensors. fn1 and fn2 must have the same non-zero number and type of outputs.<jupyter_code>import tensorflow as tf #import tensorflow library
a = 10
b = 2
x = tf.Variable(5)
y = tf.Variable(30)
z = tf.multiply(a, b)
result = tf.cond(x < y, lambda: tf.add(x, z), lambda: tf.square(y))
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)
    print(result.eval())
rank_three_tensor = tf.ones([2, 3])
matrix = tf.reshape(rank_three_tensor, [6])
with tf.Session() as sess:
    print(sess.run(rank_three_tensor))
    print(sess.run(matrix))
# Initialize session in interactive mode
import tensorflow as tf
#sess = tf.InteractiveSession()

# Some tensor we want to print the value of
a = tf.constant([1.0, 3.0])

# Add print operation
#a = tf.Print(a, [a], message="This is a: ")
#tf.Print(a, [a], message="This is a: ")
#a = tf.print(a, [a], message="This is a: ")

# Add more elements of the graph using a
b = tf.add(a, a)
with tf.Session() as sess:
    print(b.eval())

sess.close()
tmp = tf.zeros([3,4,5], dtype=tf.float32, name=None)
one = tf.ones_like(tmp, dtype=tf.float32)
with tf.Session() as sess:
    print(sess.run(tmp))
    print(sess.run(one))
my_const = tf.constant([1, 2, 3, 4, 5])
with tf.Session() as sess:
    print(sess.graph.as_graph_def())
s = tf.Variable(2, name="scalar")
m = tf.Variable([[0, 1], [2, 3]], name="matrix")
W = tf.Variable(tf.zeros([784,10]))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run([s, m, W]))
# define the graph 
x = tf.Variable(5)
x.assign(50)
# launch the session
with tf.Session() as sess:
    sess.run(x.initializer)
    print(x.eval())
x = tf.Variable(5)
op = x.assign(50)
with tf.Session() as sess:
    sess.run(x.initializer)
    sess.run(op)
    print(x.eval())
# Tf.Variable
number = tf.Variable(2)
multiplier = tf.Variable(1)
multiplier = multiplier.assign_add(1)
init = tf.global_variables_initializer()
result = tf.multiply(number,multiplier)
with tf.Session() as sess:
    sess.run(init)
    for i in range(10):
        x, y, z = sess.run([number, multiplier,result])
        print("Result number: {} * multiplier: {} = {}".format(x, y, z))

# Tf.constant
number = tf.constant(2)
multiplier = tf.constant(1)
multiplier = tf.add(multiplier, 1)
result = tf.multiply(number,multiplier)
with tf.Session() as sess:
    sess.run(init)
    for i in range(10):
        print("Result number * multiplier = ",sess.run(result))

import numpy as np
import tensorflow as tf
with tf.Graph().as_default() as g:
    x = tf.constant(2, tf.int32)
    y = x * x
    g.prevent_feeding(x)e
    g.prevent_feeding(y)

    with tf.Session() as sess:
        print(sess.run(y, feed_dict={x: 4}))
        print(sess.graph.is_feedable(y))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1668444918644972, 'fraction_numerical': 0.03507602027207255, 'mean_word_length': 4.781804163454125, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '139660', 'license': 'Apache-2.0', 'n_tokens_mistral': 3173, 'n_tokens_neox': 2996, 'n_words': 679, 'path': 'notebook/TFAML_L1.ipynb', 'repo_name': 'trungtv/tensorflow_for_applied_machine_learning', 'size': '7498'}