# ๐โโ๏ธ Introduction to Tensorflow

Brief Introduction to Tensorflow

# ๐ฉ Main flow of programs in Tensorflow

1. Create Tensors (variables) that are not yet executed/evaluated.

2. Write operations between those Tensors.

4. Create a Session.

5. Run the Session. This will run the operations you'd written above.

To summarize, remember to initialize your variables, create a session and run the operations inside the session. ๐ฉโ๐ซ

# ๐ฉโ๐ป Code Example

To calculate the following formula:

โ$loss=L(\hat{y},y)=(\hat{y}^{(i)}-y^{(i)})^2$โ

# Creating tensors and writing operations between them y_hat = tf.constant(36, name='y_hat')y = tf.constant(39, name='y')loss = tf.Variable((y - y_hat)**2, name='loss')โ# Initializing tensorsinit = tf.global_variables_initializer()โ# Creating sessionwith tf.Session() as session:     # Running the operations    session.run(init) โ    # printing results    print(session.run(loss))

When we created a variable for the loss, we simply defined the loss as a function of other quantities, but did not evaluate its value. To evaluate it, we had to use the initializer.

# โ Deฤiลken Baลlatma (initalization) Hakkฤฑnda Not

For the following code:

a = tf.constant(2)b = tf.constant(10)c = tf.multiply(a,b)print(c)

๐คธโโ๏ธ The output is

Tensor("Mul:0", shape=(), dtype=int32)

As expected, we will not see 20 ๐ค! We got a tensor saying that the result is a tensor that does not have the shape attribute, and is of type "int32". All we did was put in the 'computation graph', but we have not run this computation yet.

# ๐ฆ Placeholders in TF

• A placeholder is an object whose value you can specify only later. To specify values for a placeholder, we can pass in values by using a feed dictionary.

• Below, a placeholder has been created for x. This allows us to pass in a number later when we run the session.

x = tf.placeholder(tf.int64, name = 'x')print(sess.run(2 * x, feed_dict = {x: 3}))sess.close()

# ๐ More examples

Computing sigmoid function with TF

def sigmoid(z):    """    Computes the sigmoid of zโ    Arguments:    z -- input value, scalar or vectorโ    Returns:     results -- the sigmoid of z    """โ    # Creating a placeholder for x. Naming it 'x'.    x =  tf.placeholder(tf.float32, name = 'x')โ    # computing sigmoid(x)    sigmoid = tf.sigmoid(x)โ    # Creating a session, and running it.    with tf.Session() as sess:        # Running session and call the output "result"        result = sess.run(sigmoid, feed_dict = {x: z})โ    return result

Computing cost function with TF

def cost(logits, labels):    """    Computes the cost using the sigmoid cross entropyโ    Arguments:    logits -- vector containing z, output of the last linear unit (before the final sigmoid activation)    labels -- vector of labels y (1 or 0) โ    Returns:    cost -- runs the session of the cost function    """โ    # Creating the placeholders for "logits" (z) and "labels" (y)    z = tf.placeholder(tf.float32, name = 'z')    y = tf.placeholder(tf.float32, name = 'y')โ    # Using the loss function    cost = tf.nn.sigmoid_cross_entropy_with_logits(logits = z,  labels = y)โ    # Creating a session    sess = tf.Session()โ    # Running the session     cost = sess.run(cost, feed_dict = {z: logits, y: labels})โ    # Closing the session    sess.close()โ    return cost