/
helloworld-tensorflow.py
94 lines (77 loc) · 2.73 KB
/
helloworld-tensorflow.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import tensorflow as tf
graph1 = tf.Graph()
with graph1.as_default():
a = tf.constant([2], name = 'constant_a')
b = tf.constant([3], name = 'constant_b')
c = tf.add(a, b)
# Printing the value of a
sess = tf.compat.v1.Session(graph = graph1)
result = sess.run(c)
print(result)
sess.close()
graph2 = tf.Graph()
with graph2.as_default():
Scalar = tf.constant(2)
Vector = tf.constant([5,6,2])
Matrix = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
Tensor = tf.constant( [ [[1,2,3],[2,3,4],[3,4,5]] , [[4,5,6],[5,6,7],[6,7,8]] , [[7,8,9],[8,9,10],[9,10,11]] ] )
with tf.compat.v1.Session(graph = graph2) as sess:
result = sess.run(Scalar)
print ("Scalar (1 entry):\n %s \n" % result)
result = sess.run(Vector)
print ("Vector (3 entries) :\n %s \n" % result)
result = sess.run(Matrix)
print ("Matrix (3x3 entries):\n %s \n" % result)
result = sess.run(Tensor)
print ("Tensor (3x3x3 entries) :\n %s \n" % result)
graph3 = tf.Graph()
with graph3.as_default():
Matrix_one = tf.constant([[1,2,3],[2,3,4],[3,4,5]])
Matrix_two = tf.constant([[2,2,2],[2,2,2],[2,2,2]])
add_1_operation = tf.add(Matrix_one, Matrix_two)
add_2_operation = Matrix_one + Matrix_two
with tf.compat.v1.Session(graph =graph3) as sess:
result = sess.run(add_1_operation)
print ("Defined using tensorflow function :")
print(result)
result = sess.run(add_2_operation)
print ("Defined using normal expressions :")
print(result)
graph4 = tf.Graph()
with graph4.as_default():
Matrix_one = tf.constant([[2,3],[3,4]])
Matrix_two = tf.constant([[2,3],[3,4]])
mul_operation = tf.matmul(Matrix_one, Matrix_two)
with tf.compat.v1.Session(graph = graph4) as sess:
result = sess.run(mul_operation)
print ("Defined using tensorflow function :")
print(result)
v = tf.Variable(0)
update = tf.compat.v1.assign (v, v+1)
init_op = tf.compat.v1.global_variables_initializer()
with tf.compat.v1.Session() as session:
session.run(init_op)
print(session.run(v))
for _ in range(3):
session.run(update)
print(session.run(v))
a = tf.compat.v1.placeholder(tf.float32)
b = a * 2
with tf.compat.v1.Session() as sess:
result = sess.run(b,feed_dict={a:3.5})
print (result)
dictionary={a: [ [ [1,2,3],[4,5,6],[7,8,9],[10,11,12] ] , [ [13,14,15],[16,17,18],[19,20,21],[22,23,24] ] ] }
with tf.compat.v1.Session() as sess:
result = sess.run(b,feed_dict=dictionary)
print (result)
graph5 = tf.Graph()
with graph5.as_default():
a = tf.constant([5])
b = tf.constant([2])
c = tf.add(a,b)
d = tf.subtract(a,b)
with tf.compat.v1.Session(graph = graph5) as sess:
result = sess.run(c)
print ('c =: %s' % result)
result = sess.run(d)
print ('d =: %s' % result)