> c3 : 2"> > c3 : 2">

Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tensorflow Lecture 박 영 택 컴퓨터학부.

Similar presentations


Presentation on theme: "Tensorflow Lecture 박 영 택 컴퓨터학부."— Presentation transcript:

1 Tensorflow Lecture 박 영 택 컴퓨터학부

2 tensorflow start # tensorflow start import tensorflow as tf
c1 = tf.constant(1) c2 = tf.constant(2) c3 = tf.mul(c1, c2) with tf.Session() as sess: print "c3 : ", sess.run(c3) >> c3 : 2

3 tensorflow constants # tensroflow constants import tensorflow as tf
matrix1 = tf.constant([[3,3]]) matrix2 = tf.constant([[2],[2]]) product = tf.matmul(matrix1, matrix2) sess = tf.Session() print sess.run(matrix1) with sess : #sess.run(t) = t.eval() print matrix2.eval() print product.eval() >> [ [3 3] ] [ [2] [2] ] [ [12] ]

4 tensorflow variables # tensorflow variables import tensorflow as tf state = tf.Variable(0, name = "counter" ) print "state : ", state init_op= tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) print "sess.run(state) : ", sess.run(state) state.assign(1) print "state.assign(1) : ", sess.run(state) assign_op = state.assign(1) sess.run(assign_op) print "sess.run(state) after run(assign_op) : ", sess.run(state) print state >> state : <tensorflow.python.ops.variables. Variable object at 0x1078e8390> sess.run(state) : 0 state.assign(1) : 0 sess.run(state) after run(assign_op) : 1 <tensorflow.python.ops.variables.

5 a simple example >> state : 0
import tensorflow as tf state = tf.Variable(0, name="counter") one = tf.constant(1) new_value = tf.add(state,one) update = tf.assign(state, new_value) init_op = tf.initialize_all_variables() with tf.Session() as sess : sess.run(init_op) print "state : ", sess.run(state) sess.run(update) print "state after update : ", state print "state after update : ", sess.run(state) >> state : 0 state after update : <tensorflow.python.ops.variables. Variable object at 0x107bc7490> state after update : 1

6 representation of shape
import tensorflow as tf a = tf.zeros([5,]) a1 = tf.zeros((5,)) b = tf.zeros([2,3]) c = tf.zeros((1,2,3)) with tf.Session() as sess: print "a : ", a print "sess.run(a) : ", sess.run(a) print "sess.run(b) : ", sess.run(b) print "sess.run(c) : ", sess.run(c) print "shape of a :", a.get_shape() print "shape of b :", b.get_shape() print "shape of c :", c.get_shape() >> a : Tensor("zeros_18:0", shape=TensorShape([Dimension(5)]), dtype=float32) sess.run(a) : [ ] sess.run(b) : [[ ] [ ]] sess.run(c) : [[[ ] [ ]]] shape of a : TensorShape([Dimension(5)]) shape of b : TensorShape([Dimension(2), Dimension(3)]) shape of c : TensorShape([Dimension(1), Dimension(2), Dimension(3)])

7 random uniform : shape, min, max
import tensorflow as tf a = tf.random_uniform((2,3),-1,1) b = tf.random_uniform((2,3), 90,100) with tf.Session() as sess: print "a : ", sess.run(a) print "b : ", sess.run(b) print "shape of a : ", a.get_shape() >> a : [[ ] [ ]] b : [[ ] [ ]] shape of a : TensorShape([Dimension(2), Dimension(3)])

8 place holder #place holder import tensorflow as tf
#x = tf.placeholder("float", shape=(42, 4)) #x = tf.placeholder("float", shape=[42, 4]) x = tf.placeholder("float", (2, 3)) y = tf.zeros([2, 3], "float") print(x.get_shape()) print(y.get_shape()) >> TensorShape([Dimension(2), Dimension(3)]) TensorShape([Dimension(2), Dimension(3)])

9 matrix add # matrix add import tensorflow as tf
x = tf.constant(([1,2], [3,4])) y = tf.constant([[10,20], [30,40]]) z = tf.add(x, y) with tf.Session() as sess: print sess.run(z) >> [[ ] [ ]]

10 place holder example #place holder example import tensorflow as tf
x = tf.placeholder("float", (2, 3)) y = tf.zeros([2, 3], "float") z = tf.add(x, y) with tf.Session() as sess: print sess.run(z, feed_dict={x: [[1,2,3], [4,5,6]]}) >> [[ ] [ ]]

11 place holder example #place holder example import tensorflow as tf
x = tf.placeholder("float", (2, 3)) y = tf.placeholder("float", [2, 3]) z = tf.add(x, y) with tf.Session() as sess: print sess.run(z, feed_dict={x: [[1,2,3], [4,5,6]], y:[[10,20,30], [40,50,60]]}) >> [[ ] [ ]]

12 a simple linear regression test
import numpy as np import tensorflow as tf b = tf.Variable(tf.zeros((3,))) # b shape = 1X3 W = tf.Variable(tf.random_uniform((4,3), -1, 1)) # W shape = 4 X 3 x = tf.placeholder(tf.float32, (None, 4)) # x shape = None X 4 h_i = tf.nn.relu(tf.matmul(x,W) + b) c = tf.matmul(x,W) init_op = tf.initialize_all_variables() with tf.Session() as sess: sess.run(init_op) print "h_i : \n", sess.run(h_i, {x:np.random.rand(2,4)}) print "W: \n", sess.run(W) print "c : matmul([1,1,1,1],W) \n", sess.run(c, {x: [[1,1,1,1]]}) >> h_i : [ [ ] [ ] ] W: [ [ ] [ ] [ ] [ ] ] c : matmul([1,1,1,1],W) [ [ ] ]


Download ppt "Tensorflow Lecture 박 영 택 컴퓨터학부."

Similar presentations


Ads by Google