<jupyter_start><jupyter_text>Tensor Flow Basics<jupyter_code>import tensorflow as tf
from tensorflow.python.client import device_lib
print(device_lib.list_local_devices())<jupyter_output><empty_output><jupyter_text>Starting a Session:<jupyter_code>deep_learning = tf.constant('Deep Learning with Tensor Flow')
session = tf.Session()
session.run(deep_learning)
session.close()
# You can use eager execution to execute TensorFlow operations immediately as they are called from Python:
#tf.enable_eager_execution<jupyter_output><empty_output><jupyter_text>Interactive Session:<jupyter_code>tf_session = tf.InteractiveSession()
print(deep_learning.eval())<jupyter_output><empty_output><jupyter_text>Operations:<jupyter_code>a = tf.constant(3)
b = tf.constant(2)
add = tf.add(a, b)
multiply = tf.multiply(a, b)
#session.run(add)
add.eval()
#session.run(multiply)
multiply.eval()<jupyter_output><empty_output><jupyter_text>Creating tensors from Python objects<jupyter_code># Create 0-D Tensor:
tf_0 = tf.convert_to_tensor(8.0,dtype=tf.float64)
tf_0.eval()
#Create 1-D tensor:
import numpy as np
vector_1d = np.array([4,5,8,0,7])
vector_1d.shape
tf_1D = tf.convert_to_tensor(vector_1d, dtype=tf.float64)
tf_1D.eval()
#Create 2-D Tensor:
vector_2d = np.array([(0.5,0.8,1.0,0.6,0.1),
                     (2,5,8,9,1),
                     (11,4,0.8,18,21)])
vector_2d.shape
tf_2D = tf.convert_to_tensor(vector_2d, dtype=tf.float64)
tf_2D.eval()<jupyter_output><empty_output><jupyter_text>Creating Variables<jupyter_code># a variable that describes the weights connecting neurons between two layers
# of a neural network
weights = tf.Variable(tf.random_normal([5, 10],stddev=0.5),name="weights")
weights
weights.shape<jupyter_output><empty_output><jupyter_text>Placeholder Tensors<jupyter_code>x = tf.placeholder(tf.float32, name="x")
y = tf.placeholder(tf.float32, name="y")
z = tf.multiply(x, y)
sess = tf.Session()
output = sess.run(z, feed_dict={x: 4, y: 2})
output<jupyter_output><empty_output><jupyter_text>Variable sharing in Tensorflow<jupyter_code>var=tf.Variable(tf.random_normal([2,3]),name="var1")
var
#open a variable scope named 'scope'
with tf.variable_scope("scope"):
    #add a new variable to the graph
    var=tf.get_variable("var1",[1])
#print the name of variable
print(var.name)
#set the reuse flag to True
with tf.variable_scope("scope1"):
    #declare a variable named var3
    var1 = tf.get_variable("var2",[1])
    #set reuse flag to True
    tf.get_variable_scope().reuse_variables()
    #just an assertion!
    assert tf.get_variable_scope().reuse==True
    #declare another variable with same name
    var2=tf.get_variable("var2",[1])
var1
var2<jupyter_output><empty_output><jupyter_text>Configuring sessions:<jupyter_code>with tf.device('/gpu:0'):
    x = tf.placeholder(tf.float32, name="x", shape=[None, 4])
    W = tf.Variable(tf.random_uniform([4,10], -1, 1), name="W")
    b = tf.Variable(tf.zeros([10]), name="biases")
x.name
output = tf.matmul(x, W) + b
init = tf.global_variables_initializer()
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
sess.run(init)
import numpy as np
xrand = np.random.rand(10, 4)
xrand[:5]
for val in xrand:
    print(sess.run(output, feed_dict={x: xrand}))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14167433302667892, 'fraction_numerical': 0.029132168046611468, 'mean_word_length': 6.413636363636364, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, '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': '233895', 'license': 'MIT', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1216, 'n_words': 231, 'path': 'Computer vision/TensorFlow Basics.ipynb', 'repo_name': 'zbs881314/Deep-Learning-and-Computer-Vision', 'size': '3261'}