# Exercises on mathematical operations in TensorFlow 1.4

EN121220190807

Practice makes perfect

In [1]:
import pandas as pd
import tensorflow as tf
import matplotlib.pyplot as plt


### Exercise 1: Perform the following equation on the tensor.

In [2]:
R = tf.constant(12, tf.int16, name="twelve")
print(R)

Tensor("twelve:0", shape=(), dtype=int16)

In [3]:
F = tf.constant(4, tf.int16, name="twelve")
print(F)

Tensor("twelve_1:0", shape=(), dtype=int16)

In [4]:
KOT = tf.div (R, F)
print(KOT)

Tensor("div:0", shape=(), dtype=int16)

In [5]:
with tf.Session() as sess:
result_2 = KOT.eval()
print(result_2)

3

In [6]:
sess = tf.Session()
print(sess.run(KOT))

3


### Exercise 2: Perform the following equation on the tensor

In [7]:
F = tf.constant(8)
R = tf.constant(2)
FFT = tf.multiply (F, R)

In [8]:
sess = tf.Session()
print(sess.run(FFT))

16

In [9]:
with tf.Session() as sess:
result_2 = FFT.eval()
print(result_2)

16


### Exercise 3: Mathematical operations on tensors

In [10]:
A = tf.constant(5)
B = tf.constant(2)
PKO = tf.pow(A, B)

In [11]:
sess = tf.Session()
print(sess.run(PKO))

25


### Exercise 4: Mathematical operations on tensors

You do not need to specify the data type because tensorflow alone guesses what type of data was used in the constant.

In [12]:
A = tf.constant(15.9)
B = tf.constant(2.1)
SKO = tf.subtract(A, B)

In [13]:
sess = tf.Session()
print(sess.run(SKO))

13.799999


### Exercise 5: Perform the following equation on the tensor

Tensorflow does not like to have different data formats.
We create a mathematical formula in Python:

In [14]:
A = tf.constant(1.7, tf.float32)
B = tf.constant(2.4, tf.float32)
C = tf.constant(15, tf.float32)


In [15]:
sess = tf.Session()
print(sess.run(SZK))

14.3


### Exercise 5: Perform the following equation on the tensor

Tensorflow does not like to have different data formats.
We create a mathematical formula in Python:

In [16]:
A = tf.constant(2.07)
B = tf.constant(1.3)
C = tf.constant(2.7)

SSF = tf.subtract(A, B)
PKO = tf.pow(SSF,C)

In [17]:
with tf.Session() as sess:
result_2 = PKO.eval()
print(result_2)

0.49377024


### Exercise 6: Mathematical operations on tensors

We create a mathematical formula in Python:

We can use the math library resources:
https://docs.python.org/3/library/math.html

In [18]:
import math
a = math.pi
a

Out[18]:
3.141592653589793
In [19]:
C = tf.constant(2.1)
D = tf.constant(math.pi)

GG = tf.multiply(C, D)
PKK = tf.sqrt(GG)

In [20]:
with tf.Session() as sess:
result_2 = PKK.eval()
print("The x value sought is: ",result_2)

The x value sought is:  2.5685296


### Exercise 7: actions on Tensorflow tensors

Please calculate in Tensorflow the value of n:

Let’s check how much it will be:

In [21]:
math.pow(15,(1/3))

Out[21]:
2.46621207433047

In Tensorflow, be careful about the fixed format.

In [22]:
C = tf.constant(15, tf.float32)
A = tf.constant(3, tf.float32)
H = tf.constant(1, tf.float32)
G = tf.div(H,A)

PK = tf.pow(C,G)

In [23]:
with tf.Session() as sess:
result_2 = G.eval()
print(result_2)

0.33333334

In [24]:
with tf.Session() as sess:
result_2 = PK.eval()
print(result_2)

2.466212


### Exercise 8: actions on Tensorflow tensors

Please calculate in Tensorflow the value of n:

We can use the math library resources: https://docs.python.org/3/library/math.html

In [25]:
math.e

Out[25]:
2.718281828459045
In [26]:
math.pow(math.e,(1/4))

Out[26]:
1.2840254166877414
In [27]:
C = tf.constant(math.e, tf.float32)
A = tf.constant(4, tf.float32)
H = tf.constant(1, tf.float32)
G = tf.div(H,A)

ZHP = tf.pow(C,G)

In [28]:
with tf.Session() as sess:
result_2 = ZHP.eval()
print(result_2)

1.2840254


# Exercise 9. Change of the tensor type from float to int

TensorFlow automatically selects the data type when the argument is not specified when creating the tensor.

In [29]:
PKP = tf.constant(3.123456789, tf.float32)
ZNP = tf.cast(PKP, dtype=tf.int32)

print(PKP.dtype)
print(ZNP.dtype)

<dtype: 'float32'>
<dtype: 'int32'>