Brief Introduction to Tensorflow

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

Write operations between those Tensors.

Initialize your Tensors.

Create a Session.

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. ๐ฉโ๐ซ

To calculate the following formula:

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

# Creating tensors and writing operations between themy_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 operationssession.run(init)โ# printing resultsprint(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.

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.

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()

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 functioncost = tf.nn.sigmoid_cross_entropy_with_logits(logits = z, labels = y)โ# Creating a sessionsess = tf.Session()โ# Running the sessioncost = sess.run(cost, feed_dict = {z: logits, y: labels})โ# Closing the sessionsess.close()โreturn cost