Lecturer2_Basic of Python
Lecturer2_Basic of Python
x=2
y=3
op1 = tf.add(x, y)
op2 = tf. multiply(x, y)
op3 = tf.pow(op2, op1)
with tf.Session() as sess:
op3 = sess.run(op3)
Placeholders (inputs)
node1 = tf.placeholder(tf.float32)
node2 = tf.placeholder(tf.float32)
node3 = tf.add(node1,node2)
tf.Session().run(node3, {node1 : 3,
node2 : 4})
You need to specify all placeholders on which the “subgraph you
are using for your computation” depends
Variables (mutable state)
Variables are stateful nodes which output their current
value. State is retained across multiple executions of a graph
(mostly parameters)
W = tf.Variable([.3], dtype=tf.float32)
b = tf.Variable([-.3], dtype=tf.float32)
x = tf.placeholder(tf.float32)
linear_model = W * x + b #Operator Overloading!
init = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init)
sess.run(linear_model)
Neural network (Programming
model)
W b
x
a tensorflow graph
Neural network (Programming
model)
Mathematical operations:
MatMul: Multiply two matrices
Add: Add elementwise
ReLU: Activate with elementwise
rectified linear function
0, x <= 0
ReLu(x) =
x, x > 0
Neural network (Programming
model)
import tensorflow as tf
b = tf.Variable(tf.zeros((100,)))
W = tf.Variable(tf.random_uniform((784, 100), -1, 1))
h = tf.nn.relu(tf.matmul(x, W) + b)
• So far:
• Built a graph using variables and placeholders
• Deploy the graph onto a session, i.e., execution
environment