forked from nfmcclure/tensorflow_cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
05_operations.py
49 lines (40 loc) · 1.2 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
48
49
# Operations
#----------------------------------
#
# This function introduces various operations
# in TensorFlow
# Declaring Operations
import matplotlib.pyplot as plt
import numpy as np
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)))
# Tangent
print(sess.run(tf.div(tf.sin(3.1416/4.), tf.cos(3.1416/4.))))
# Custom operation
test_nums = range(15)
#from tensorflow.python.ops import math_ops
#print(sess.run(tf.equal(test_num, 3)))
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)))