# TensorFlow - Exercise 2 - Variables in TensorFlow

Exercise : Generate a random array of size 100 and find mean of z as obtained from below equations: 1. y = xx + 5x 2. z = yy

Implement both the equations seperately and * means element wise multiplication and not matrix multiplication. Below are some restrictions for this exercise:

1. y should be of type tf.Variable()
2. Use tf.random_normal() to get random tensor of size 100. Use seed as 12.

# Caution : Solution for exercise is below. Please try solving problem by yourself before looking below

``````import tensorflow as tf

# get 100 random tensors. 12 is used as seed.
x = tf.random_normal(, seed=12,name="x")
y = tf.Variable(x*x+5*x,name="y")
z = tf.multiply(y,y,name="z")
zmean = tf.reduce_mean(z)

init_op = tf.global_variables_initializer()

with tf.Session() as sess:
sess.run(init_op)
zout = sess.run(zmean)
print(zout)
``````
``````27.7974
``````

Summary of what each line does in above code:

Line 1 : Import tensorflow library with alias tf.

Line 4 : Generate random tensor of size 100.

Line 5 : Create a variable y. Initial value of y is given as xx+5x.

Line 6 : Multiply y and y.

Line 7 : Calculate mean of z.

Line 9 : When you launch the graph, variables have to be explicitly initialized before you can run Ops that use their value. You can initialize a variable by running its initializer.

Line 11 : Create a new session to run our graph.

Line 12 : Run the operation to initialize global variables.

Line 13 : Run graph to calculate value of zmean. This will calculate x, y and finally zmean.

For your own unerstanding you can try looking at graph of this code in tensorboard.