-
Notifications
You must be signed in to change notification settings - Fork 2.4k
/
05_operations.py
47 lines (35 loc) · 1.05 KB
/
05_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Operations
#----------------------------------
#
# This function introduces various operations
# in TensorFlow
# Declaring Operations
import tensorflow as tf
from tensorflow.python.framework import ops
ops.reset_default_graph()
# Open graph session
sess = tf.Session()
# div() vs truediv() vs floordiv()
print(sess.run(tf.div(3, 4)))
print(sess.run(tf.truediv(3, 4)))
print(sess.run(tf.floordiv(3.0, 4.0)))
# Mod function
print(sess.run(tf.mod(22.0, 5.0)))
# Cross Product
print(sess.run(tf.cross([1., 0., 0.], [0., 1., 0.])))
# Trig functions
print(sess.run(tf.sin(3.1416)))
print(sess.run(tf.cos(3.1416)))
print(sess.run(tf.tan(3.1416/4.)))
# Custom operation
test_nums = range(15)
def custom_polynomial(x_val):
# Return 3x^2 - x + 10
return tf.subtract(3 * tf.square(x_val), x_val) + 10
print(sess.run(custom_polynomial(11)))
# What should we get with list comprehension
expected_output = [3*x*x-x+10 for x in test_nums]
print(expected_output)
# TensorFlow custom function output
for num in test_nums:
print(sess.run(custom_polynomial(num)))