In [1]:
%matplotlib inline
import tensorflow as tf
import matplotlib.pyplot as plt
In [2]:
a = tf.constant([5])
b = tf.constant([2])
create another TensorFlow object applying the sum (+) operation:
In [3]:
#Your code goes here
c = a + b
In [5]:
with tf.Session() as session:
result = session.run(c)
print("The addition of this two constants is: {0}".format(result))
In [6]:
# Your code goes here. Use the multiplication operator.
c = a * b
In [9]:
with tf.Session() as session:
result = session.run(c)
print("The Multiplication of this two constants is: {0}".format(result))
In [10]:
matrixA = tf.constant([[2,3],[3,4]])
matrixB = tf.constant([[2,3],[3,4]])
In [11]:
# Your code goes here
first_operation = matrixA * matrixB
second_operation = tf.matmul(matrixA, matrixB)
In [13]:
with tf.Session() as session:
result = session.run(first_operation)
print("Element-wise multiplication: \n", result)
result = session.run(second_operation)
print("Matrix Multiplication: \n", result)
In [14]:
a=tf.constant(1000)
b=tf.Variable(0)
init_op = tf.global_variables_initializer()
In [15]:
# Your code goes here
update = tf.assign(b, a)
with tf.Session() as session:
session.run(init_op)
session.run(update)
print(b.eval())
Now try to do something more advanced. Try to create a fibonnacci sequence and print the first few values using TensorFlow:</b></h3>
If you don't know, the fibonnacci sequence is defined by the equation:
$$F_{n} = F_{n-1} + F_{n-2}$$
Resulting in a sequence like: 1,1,2,3,5,8,13,21...
In [16]:
# Variables
val_prev = tf.Variable(0)
val_curr = tf.Variable(1)
val_new = tf.Variable(0)
fib_op = val_curr + val_prev
# Update operations
update_new_op = tf.assign(val_new, fib_op)
update_prev_op = tf.assign(val_prev, val_curr)
update_curr_op = tf.assign(val_curr, val_new)
init_op = tf.global_variables_initializer()
num_iterations = 40
fibonacci = []
with tf.Session() as session:
# Init
session.run(init_op)
print(val_curr.eval())
# Iterate
for it in range(num_iterations):
# Update current and previous values
session.run(update_new_op)
session.run(update_prev_op)
session.run(update_curr_op)
fib_val = val_curr.eval()
print(fib_val)
fibonacci.append(fib_val)
plt.plot(fibonacci)
Out[16]:
In [17]:
# Your code goes here
a = tf.placeholder(name='a', dtype=tf.float32)
b = tf.placeholder(name='b', dtype=tf.float32)
my_op = tf.maximum(a, b)
with tf.Session() as session:
val = session.run(my_op, feed_dict={a: [125], b: [2.]})
print(val)
In [18]:
a = tf.constant(5.)
b = tf.constant(2.)
create a variable named c
to receive the result an operation (at your choice):
In [19]:
#your code goes here
c = tf.sin(a) * tf.exp(b)
In [21]:
with tf.Session() as session:
result = session.run(c)
print("c =: {}".format(result))
They're really similar to mathematical functions the only difference is that operations works over tensors.