-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtf_ex2_basic_operations.py
More file actions
36 lines (28 loc) · 1.1 KB
/
tf_ex2_basic_operations.py
File metadata and controls
36 lines (28 loc) · 1.1 KB
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
import tensorflow as tf
# Basic constant operations
a = tf.constant(2)
b = tf.constant(3)
# Launch the default graph
with tf.Session() as sess:
print("a: {}, b: {}".format(sess.run(a), sess.run(b)))
print("Addition with constants: {}".format(sess.run(a+b)))
print("Multiplication with constants: {}\n".format(sess.run(a*b)))
# Basic operations with variables as graph inputs
a = tf.placeholder(tf.float32)
b = tf.placeholder(tf.float32)
# Define operations
add = tf.add(a, b)
mul = tf.multiply(a, b)
# Launch the default graph
with tf.Session() as sess:
print("Addition with variables: {}".format(sess.run(add, feed_dict={a: 2, b: 3})))
print("Multiplication with variables: {}\n".format(sess.run(mul, feed_dict={a: 2, b: 3})))
# Create 2 constants for simple matrix multiplication
# A 1x2 matrix and 2x1 matrix
matrix_1 = tf.constant([[3, 3]])
matrix_2 = tf.constant([[2], [2]])
# Define matrix multiplication operation
product = tf.matmul(matrix_1, matrix_2)
# Launch the default graph
with tf.Session() as sess:
print("Result of the matrix multiplication: {}".format(sess.run(product)))