SlideShare ist ein Scribd-Unternehmen logo
1 von 79
Downloaden Sie, um offline zu lesen
NTU Machine Learning
Tensorflow
By Mark Chang
Outlines
• One-layer Perceptron
• Tensorboard
• Convolutional Neural Networks
• Multi-GPU Implementation
Tensorflow
https://www.tensorflow.org/
How to Install Tensorflow
• Direct step:
– install tensorflow
• Install in virtual environment:
– install pyenv
– install anaconda-2.x.x
– install tensorflow
> sudo pip install tensorflow
> pip install tensorflow
1. install pyenv
• Mac OSX Homebrew
• From Github
(for ubuntu user, please replace .bash_profile with .bashrc)
> cd ~
> git clone https://github.com/yyuu/pyenv.git ~/.pyenv
> echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
> echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
> echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
> source ~/.bash_profile
> brew update
> brew install pyenv
install anaconda-2.x.x and
Tensorflow
• install anaconda-2.4.0
• switch to anaconda 2.4.0
• Install tensorflow
> pyenv install anaconda-2.4.0
> pyenv global anaconda-2.4.0
> pip install tensorflow
Features of Tensorflow
• Computational Graph
– Define computation
• Session
– Run computation
Computational Graph
MNIST
• Image Classification
• Multi-class:0~9
https://www.tensorflow.org/versions/r0.7/images/MNIST.png
Training Data
https://www.tensorflow.org/versions/r0.7/images/MNIST-Matrix.png
28x28 = 784
28
28
Training Data
https://www.tensorflow.org/versions/r0.7/images/mnist-train-xs.png
https://www.tensorflow.org/versions/r0.7/images/mnist-train-ys.png
X
Y
One-layer Perceptron
0
https://www.tensorflow.org/versions/r0.7/images/softmax-regression-scalargraph.png
One-layer Perceptron
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/one_layer_train.ipynb
One-layer Perceptron
x_ = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x_,W) + b)
cross_entropy = -tf.reduce_mean(y_ * tf.log(y))
optimizer = tf.train.GradientDescentOptimizer(0.01)
trainer = optimizer.minimize(cross_entropy)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
for i in range(1000):
sess.run(train, feed_dict={x_:x_data,y_:y_data})
print sess.run(cross_entropy, feed_dict={x_:x_data,y_:y_data})
sess.close()
Computational
Graph
Session
Computation Graph
# placeholder
x_ = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
# variable
W = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([10]))
# operations
y = tf.nn.softmax(tf.matmul(x_, W) + b)
# error function
cross_entropy = -tf.reduce_sum(y_* tf.log(y))
# trainer
optimizer = tf.train.GradientDescentOptimizer(0.01)
trainer = optimizer.minimize(cross_entropy)
# initalizer
init = tf.global_variables_initializer()
Placeholder
0. … 0.
0. … 0.
0. … 0.1
0. … 0.
0. … 0.2
0. … 0.
… … …
x_ y_
x_ = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
0. … 0.
0. … 0.
0. … 1.
1. … 0.
0. … 0.
0. … 0.
… … …
784 10
n
Variable
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
bw
0. …….. 0.
0. …….. 0.
0. …….. 0.
…….. …….. ……..
0. …….. 0.
0. …….. 0.
0.
0.
……..
0.
0.
784
10
10
Matrix Multiplication
y = tf.nn.softmax(tf.matmul(x_, W) + b)
https://www.tensorflow.org/versions/r0.8/images/softmax-regression-scalarequation.png
Matrix Multiplication
y = tf.nn.softmax(tf.matmul(x_, W) + b)
https://www.tensorflow.org/versions/r0.8/images/softmax-regression-vectorequation.png
Batch
• Improve parallelization
• Ex, batch size = 4:
x =
2
6
6
6
4
x
(1)
1 x
(1)
2 x
(1)
3
x
(2)
1 x
(2)
2 x
(2)
3
x
(3)
1 x
(3)
2 x
(3)
3
x
(4)
1 x
(4)
2 x
(4)
3
3
7
7
7
5
Matrix Multiplication with Batch
y = tf.nn.softmax(tf.matmul(x_, W) + b)
x =
2
6
6
6
4
x
(1)
1 x
(1)
2 x
(1)
3
x
(2)
1 x
(2)
2 x
(2)
3
x
(3)
1 x
(3)
2 x
(3)
3
x
(4)
1 x
(4)
2 x
(4)
3
3
7
7
7
5
W =
2
4
W1,1 W1,2 W1,3
W2,1 W2,2 W2,3
W3,1 W3,2 W3,3
3
5 b =
2
4
b1
b2
b3
3
5
matmul(x , W) + b
=
2
6
6
6
4
x
(1)
1 W1,1 + x
(1)
2 W2,1 + x
(1)
3 W3,1 + b1 · · · x
(1)
1 W1,3 + x
(1)
2 W2,3 + x
(1)
3 W3,3 + b3
x
(2)
1 W1,1 + x
(2)
2 W2,1 + x
(2)
3 W3,1 + b1 · · · x
(2)
1 W1,3 + x
(2)
2 W2,3 + x
(2)
3 W3,3 + b3
x
(3)
1 W1,1 + x
(3)
2 W2,1 + x
(3)
3 W3,1 + b1 · · · x
(3)
1 W1,3 + x
(3)
2 W2,3 + x
(3)
3 W3,3 + b3
x
(4)
1 W1,1 + x
(4)
2 W2,1 + x
(4)
3 W3,1 + b1 · · · x
(4)
1 W1,3 + x
(4)
2 W2,3 + x
(4)
3 W3,3 + b3
3
7
7
7
5
Matrix Multiplication with Batch
y = tf.nn.softmax(tf.matmul(x_, W) + b)
wx_
tf.matmul(x_,w)+b
b
0. ….. 0.
0. ….. 0.
….. ….. …..
784
0. ….. 0.
….. ….. …..
0. ….. 0.
10
10
0.
…..
0.
784
n
0. ….. 0.
….. ….. …..
0. ….. 0.
10
n
Softmax
y = tf.nn.softmax(X)
X =
2
6
6
6
4
X
(1)
1 X
(1)
2 X
(1)
3
X
(2)
1 X
(2)
2 X
(2)
3
X
(3)
1 X
(3)
2 X
(3)
3
X
(4)
1 X
(4)
2 X
(4)
3
3
7
7
7
5
y = softmax(X)
=
2
6
6
6
6
6
6
6
6
6
4
eX
(1)
1
eX
(1)
1 +eX
(1)
2 +eX
(1)
3
eX
(1)
2
eX
(1)
1 +eX
(1)
2 +eX
(1)
3
eX
(1)
3
eX
(1)
1 +eX
(1)
2 +eX
(1)
3
eX
(2)
1
eX
(2)
1 +eX
(2)
2 +eX
(2)
3
eX
(2)
2
eX
(2)
1 +eX
(2)
2 +eX
(2)
3
eX
(2)
3
eX
(2)
1 +eX
(2)
2 +eX
(2)
3
eX
(3)
1
eX
(3)
1 +eX
(3)
2 +eX
(3)
3
eX
(3)
2
eX
(3)
1 +eX
(3)
2 +eX
(3)
3
eX
(3)
3
eX
(3)
1 +eX
(3)
2 +eX
(3)
3
eX
(4)
1
eX
(4)
1 +eX
(4)
2 +eX
(4)
3
eX
(4)
2
eX
(4)
1 +eX
(4)
2 +eX
(4)
3
eX
(4)
3
eX
(4)
1 +eX
(4)
2 +eX
(4)
3
3
7
7
7
7
7
7
7
7
7
5
Softmax
y = tf.nn.softmax(tf.matmul(x_, W) + b)
tf.nn.softmax
0. ….. 0.
….. ….. …..
0. ….. 0.
10
n
0. ….. 0.
….. ….. …..
0. ….. 0.
10
n
Error Function
cross_entropy = -tf.reduce_mean(y_* tf.log(y))
y =
2
6
6
4
1 0 0
0 1 0
1 0 0
0 0 1
3
7
7
5 log(y) =
2
6
6
6
4
log(y
(1)
1 ) log(y
(1)
2 ) log(y
(1)
3 )
log(y
(2)
1 ) log(y
(2)
2 ) log(y
(2)
3 )
log(y
(3)
1 ) log(y
(3)
2 ) log(y
(3)
3 )
log(y
(4)
1 ) log(y
(4)
2 ) log(y
(4)
3 )
3
7
7
7
5
y ⇤ log(y) =
2
6
6
6
4
log(y
(1)
1 ) 0 0
0 log(y
(2)
2 ) 0
log(y
(3)
1 ) 0 0
0 0 log(y
(4)
3 )
3
7
7
7
5
Error Function
cross_entropy = -tf.reduce_mean(y_* tf.log(y))
y ⇤ log(y) =
2
6
6
6
4
log(y
(1)
1 ) 0 0
0 log(y
(2)
2 ) 0
log(y
(3)
1 ) 0 0
0 0 log(y
(4)
3 )
3
7
7
7
5
reduce mean(y ⇤ log(y))
=
1
4
log(y
(1)
1 ) + log(y
(2)
2 ) + log(y
(3)
1 ) + log(y
(4)
3 )
Error Function
cross_entropy = -tf.reduce_mean(y_* tf.log(y))
y_ y
1.4331052
-tf.reduce_mean(y_*tf.log(y))
0. ….. 0.
….. ….. …..
0. ….. 0.
10
n
0. ….. 0.
….. ….. …..
0. ….. 0.
10
n
Trainer
optimizer = tf.train.GradientDescentOptimizer(0.1)
train = optimizer.minimize(cross_entropy)
Trainer
w w ⌘
@E(h(X), Y )
@w
b b ⌘
@E(h(X), Y )
@b
Computation Graph
• Initializer
init = tf.global_variables_initializer()
w
b
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
0. …….. 0.
…….. …….. ……..
0. …….. 0.
0.
……..
0.
Session
# create session
sess = tf.Session()
# initializevariable
sess.run(init)
# gradient descent
for step in xrange(1000):
sess.run(train, feed_dict={x_:x_data,y_:y_data})
# fetch variable
print sess.run(cross_entropy, feed_dict={x_:x_data,y_:y_data})
# release resource
sess.close()
Run Operations
sess.run(init)
the Node in
Computational
Graph
Run Operations
for step in xrange(1000):
sess.run(train, feed_dict={x_:x_data,y_:y_data} )
the Node in
Computational
Graph
Input
Data
x_data y_data
0. … 0.
0. … 0.
0. … 0.1
0. … 0.
0. … 0.2
0. … 0.
… … …
0. … 0.
0. … 0.
0. … 1.
1. … 0.
0. … 0.
0. … 0.
… … …
Run Operations
print sess.run(cross_entropy, feed_dict={x_:x_data,y_:y_data})
the Node in
Computational
Graph
Input
Data
x_data y_data
Results
2.4564333 0. … 0.
0. … 0.
0. … 0.1
0. … 0.
0. … 0.2
0. … 0.
… … …
0. … 0.
0. … 0.
0. … 1.
1. … 0.
0. … 0.
0. … 0.
… … …
Evaluation
# computational graph
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
# session
result_accurarcy = sess.run(accuracy, feed_dict={x_: mnist.test.images,
y_: mnist.test.labels})
Evaluation
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
y =
2
6
6
4
0.8 0.2 0
0 1.0 0.0
0.4 0.5 0.1
0 0.1 0.9
3
7
7
5 y =
2
6
6
4
1 0 0
0 1 0
1 0 0
0 0 1
3
7
7
5
argmax(y) =
2
6
6
4
0
1
1
2
3
7
7
5 argmax(y ) =
2
6
6
4
0
1
0
2
3
7
7
5
equal(argmax(y), argmax(y )) =
2
6
6
4
True
True
False
True
3
7
7
5
Evaluation
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
correct prediction =
2
6
6
4
True
True
False
True
3
7
7
5
cast(correct prediction, float32) =
2
6
6
4
1.0
1.0
0.0
1.0
3
7
7
5
reduce mean(cast(correct prediction, float32)) = 0.75
Decomposing Gradient Descent
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/one_layer_compute_gr
adients.ipynb
Decomposing Gradient Descent
grads = optimizer.compute_gradients(cross_entropy)
apply_grads = optimizer.apply_gradients(grads)
w w ⌘
@E(h(X), Y )
@w
b b ⌘
@E(h(X), Y )
@b
Compute Gradients Apply Gradients
w ⌘
@E(h(X), Y )
@w
b ⌘
@E(h(X), Y )
@b
w ⌘
@E(h(X), Y )
@w
b ⌘
@E(h(X), Y )
@b
Save and Load Trained Model
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/one_layer_load.ipynb
Save and Load Trained Model
• Save Trained Model
• Load Trained Model
saver = tf.train.Saver()
saver.save(sess, "model.ckpt")
saver = tf.train.Saver()
saver.restore(sess, "model.ckpt")
Save and Load Trained Model
• .ckpt.data : values of all variables
• .ckpt.meta : structure of the
computational graph
• How to save and load computational
graph?
– https://www.tensorflow.org/versions/r0.12/ho
w_tos/meta_graph/
Tensorboard
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/one_layer_board.ipynb
Tensorboard
Histogram Summary
Scalar Summary Computational Graph
Scalar Summary
summ_ce = tf.summary.scalar("cross_entropy", cross_entropy)
summ_acc = tf.summary.scalar("accuracy", accuracy)
cross_entropy accurarcy
Histogram Summary
summ_W = tf.summary.histogram("weights", W)
summ_b = tf.summary.histogram("biases", b)
weights biases
Summary Writer
summ_merged = tf.summary.merge([summ_W, summ_b, summ_ce])
writer = tf.summary.FileWriter("./", sess.graph_def)
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100)
result1 = sess.run(tf.log(y), feed_dict={x_: batch_xs, y_: batch_ys})
result2 = sess.run(y_ * tf.log(y), feed_dict={x_: batch_xs, y_: batch_ys})
sess.run(trainer, feed_dict={x_: batch_xs, y_: batch_ys})
summ_str = sess.run(summ_merged,feed_dict={x_:batch_xs,y_:batch_ys})
writer.add_summary(summ_str,i)
if (i+1)%5 == 0:
summary_str = sess.run(summ_acc,feed_dict={x_:mnist.test.images,
y_:mnist.test.labels})
writer.add_summary(summ_str,i)
name_scope
with tf.name_scope("cross_entropy") as scope:
cross_entropy = -tf.reduce_mean(y_ * tf.log(y))
Launch Tensorboard
> tensorboard --logdir=./
Starting TensorBoard on port 6006
(You can navigate to http://0.0.0.0:6006)
Viewing Computational Graph
without Tensorboard
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/computational_graph_
py.ipynb
Viewing Computational Graph
without Tensorboard
• tf.get_default_graph()
– get the computational graph
• tf.get_default_graph().get_operations()
– get all nodes in computational graph
• tf.get_default_graph().get_tensor_by_name
("name:0")
– get the node by name
• tf.global_variables()
– get all variables
– What is a local variable in tensorflow?
• https://stackoverflow.com/questions/38910198/what-
is-a-local-variable-in-tensorflow
Convolutional Neural Networks
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/convnet_train.ipynb
Create Variables & Operators
def weight_variable(shape):
return tf.Variable(tf.truncated_normal(shape,stddev=0.1))
def bias_variable(shape):
return tf.Variable(tf.constant(0, shape=shape))
def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
Computational Graph
x_ = tf.placeholder(tf.float32, [None, 784], name="x_")
y_ = tf.placeholder(tf.float32, [None, 10], name="y_”)
x_image = tf.reshape(x_, [-1,28,28,1])
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)
W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y= tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
Convolutional Neural Networks
nx28x28x1
nx28x28x32
nx14x14x32
nx14x14x64
nx7x7x64
nx1024
nx10
x_image
h_conv1
h_pool1
h_conv2
h_pool2
h_fc1
y
Reshape
x_image = tf.reshape(x_, [-1,28,28,1])
x
n
784
n
28
1
Convolutional Layer
W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
5
1
32
32
5x5
1
32
32
W_conv1
W_conv1
b_conv1
b_conv1
Convolutional Layer
tf.nn.conv2d(x, W , strides=[1, 1, 1, 1], padding='SAME')+b
1
5x5 1x1
28
28
28
28
strides=1
padding='SAME'
[ batch, in_height, in_width, in_channels ]
Convolutional Layer
tf.nn.conv2d(x, W , strides=[1, 1, 1, 1], padding='SAME')+b
nx28x28x1 nx28x28x32
28
28
28
28
ReLU
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
ReLU:
⇢
nin if nin > 0
0 otherwise
-0.5 0.2 0.3 -0.1
0.2 -0.3 -0.4 -1.1
2.1 -2.1 0.1 1.2
0.2 3.0 -0.3 0.5
0 0.2 0.3 0
0.2 0 0 0
2.1 0 0.1 1.2
0.2 3.0 0 0.5
Pooling Layer
tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
1x2x2x1
1
1
1
1
2
2x2 1x1
Pooling Layer
tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')
2
strides=2
padding='SAME'
28
28
14
14
Pooling Layer
h_pool1 = max_pool_2x2(h_conv1)
nx28x28x32 nx14x14x32
28
28
14
14
Reshape
h_pool2_
flat
n
7*7*64
7
64
n
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
Preventing Overfitting
• Dropout
• Early Stop
Dropout
• Randomly remove neurons in hidden
layers
• ex: 25%的Dropout Rate
x
(2)
1
x
(2)
2
x
(1)
1
x
(1)
2
x
(2)
1
x
(2)
2
x
(1)
1
x
(1)
2
y(1)
y(2)
y(1)
y(2)
Dropout
• During testing, all weights should be
multiply by (1 – dropout_rate)
x
(2)
1
x
(2)
2
y(1)
w w(1 dropout rate)
Dropout
• keep_prob = 1- dropout_rate
W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1)
keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
W_fc2 = weight_variable([1024,10])
b_fc2 = bias_variable([10])
y= tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
Early Stop
Validation Loss
Training Loss
Stop Training
t
Early Stop
patience = 20
best_accurarcy = 0
i = 0
while True:
i += 1
batch_xs, batch_ys = mnist.train.next_batch(100)
sess.run(trainer, feed_dict={x_: batch_xs, y_: batch_ys, keep_prob:0.5})
if i%100== 0:
valid_accurarcy = sess.run(accuracy,
feed_dict={x_: mnist.validation.images, y_: mnist.validation.labels, keep_prob:1})
print "%s, valid_accurarcy:%s" %(I, valid_accurarcy)
if valid_accurarcy > best_accurarcy:
patience = 20
best_accurarcy = valid_accurarcy
print "save model"
saver.save(sess, "model_conv.ckpt")
else:
patience -= 1
if patience == 0:
print "early stop"
break
Multi-GPU Implementation
• https://github.com/ckmarkoh/ntu_tensorf
low/blob/master/multi_gpu.py
Multi-GPU Implementation
• Variables:
– stored in CPU
– shared by GPUs
– updated by CPU
• Gradients:
– computed by
GPUs
– averaged by CPU
https://www.tensorflow.org/images/Parallelism.png
Multi-GPU Implementation
• create variables stored in CPU
def create_weight_and_bias(weight_shape,bias_shape):
with tf.device('/cpu:0'):
weight = tf.get_variable("weight",initializer=tf.truncated_normal(weight_shape))
bias = tf.get_variable("bias", initializer= tf.constant(0,shape=bias_shape))
return weight, bias
Multi-GPU Implementation
• input data for GPUs
for i in range(num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('tower_%d' % (i)) as scope:
if i > 0:
reuse = True
else:
reuse = False
x_next = x_all[i * batch_size:(i + 1) * batch_size, :]
y_next = y_all[i * batch_size:(i + 1) * batch_size, :]
loss, acc = create_cnn(x_next, y_next, keep_prob,
reuse=reuse)
grads = optimizer.compute_gradients(loss)
tower_grads.append(grads)
tower_acc.append(acc)
batch 1 batch 2
Multi-GPU Implementation
• create model and loss in GPUs
Tower 1 Tower 2
for i in range(num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('tower_%d' % (i)) as scope:
if i > 0:
reuse = True
else:
reuse = False
x_next = x_all[i * batch_size:(i + 1) * batch_size, :]
y_next = y_all[i * batch_size:(i + 1) * batch_size, :]
loss, acc = create_cnn(x_next, y_next, keep_prob,
reuse=reuse)
grads = optimizer.compute_gradients(loss)
tower_grads.append(grads)
tower_acc.append(acc)
Multi-GPU Implementation
• compute gradients in GPUs
tower_grads = []
tower_acc = []
for i in range(num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('tower_%d' % (i)) as scope:
if i > 0:
reuse = True
else:
reuse = False
x_next = x_all[i * batch_size:(i + 1) * batch_size, :]
y_next = y_all[i * batch_size:(i + 1) * batch_size, :]
loss, acc = create_cnn(x_next, y_next, keep_prob,
reuse=reuse)
grads = optimizer.compute_gradients(loss)
tower_grads.append(grads)
tower_acc.append(acc) Tower 1 Tower 2
Multi-GPU Implementation
• average and update gradients in CPU
with graph.as_default(), tf.device('/cpu:0'):
for i in range(num_gpus):
with tf.device('/gpu:%d' % i):
with tf.name_scope('tower_%d' % (i)) as scope:
…
tower_grads.append(grads)
tower_acc.append(acc)
avg_grads = create_average_gradients(tower_grads)
avg_acc = tf.reduce_mean(tower_acc)
trainer = optimizer.apply_gradients(avg_grads)
Tower 1 Tower 2
Multi-GPU Implementation
• train Multi-GPU Implementation
for i in range(3000):
batch_xs, batch_ys = mnist.train.next_batch(batch_size * num_gpus)
sess.run(trainer, feed_dict={x_all: batch_xs, y_all: batch_ys, keep_prob: 0.5})
講師資訊
• Email: ckmarkoh at gmail dot com
• Blog: http://cpmarkchang.logdown.com
• Github: https://github.com/ckmarkoh
• Facebook: https://www.facebook.com/ckmarkoh.chang
• Slideshare: http://www.slideshare.net/ckmarkohchang
Mark Chang
HTC Research & Healthcare
79

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchAhmed BESBES
 
Explanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expertExplanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expert홍배 김
 
Graph convolutional networks in apache spark
Graph convolutional networks in apache sparkGraph convolutional networks in apache spark
Graph convolutional networks in apache sparkEmiliano Martinez Sanchez
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyanAndreeKurtL
 
Gentlest Introduction to Tensorflow
Gentlest Introduction to TensorflowGentlest Introduction to Tensorflow
Gentlest Introduction to TensorflowKhor SoonHin
 
Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.UA Mobile
 
Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3Khor SoonHin
 
Machine Learning Introduction
Machine Learning IntroductionMachine Learning Introduction
Machine Learning IntroductionAkira Sosa
 
Sliced Wasserstein距離と生成モデル
Sliced Wasserstein距離と生成モデルSliced Wasserstein距離と生成モデル
Sliced Wasserstein距離と生成モデルohken
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Sciencehenrygarner
 
Learning Deep Learning
Learning Deep LearningLearning Deep Learning
Learning Deep Learningsimaokasonse
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189Mahmoud Samir Fayed
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data ScienceMike Anderson
 
Machine Learning Live
Machine Learning LiveMachine Learning Live
Machine Learning LiveMike Anderson
 
Machine Learning: Make Your Ruby Code Smarter
Machine Learning: Make Your Ruby Code SmarterMachine Learning: Make Your Ruby Code Smarter
Machine Learning: Make Your Ruby Code SmarterAstrails
 
Procedural Content Generation with Clojure
Procedural Content Generation with ClojureProcedural Content Generation with Clojure
Procedural Content Generation with ClojureMike Anderson
 
Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...DefCamp
 
Let’s talk about microbenchmarking
Let’s talk about microbenchmarkingLet’s talk about microbenchmarking
Let’s talk about microbenchmarkingAndrey Akinshin
 

Was ist angesagt? (20)

Introduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from ScratchIntroduction to Neural Networks and Deep Learning from Scratch
Introduction to Neural Networks and Deep Learning from Scratch
 
Explanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expertExplanation on Tensorflow example -Deep mnist for expert
Explanation on Tensorflow example -Deep mnist for expert
 
Graph convolutional networks in apache spark
Graph convolutional networks in apache sparkGraph convolutional networks in apache spark
Graph convolutional networks in apache spark
 
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericosKristhyan kurtlazartezubia evidencia1-metodosnumericos
Kristhyan kurtlazartezubia evidencia1-metodosnumericos
 
Gentlest Introduction to Tensorflow
Gentlest Introduction to TensorflowGentlest Introduction to Tensorflow
Gentlest Introduction to Tensorflow
 
Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.Gems of GameplayKit. UA Mobile 2017.
Gems of GameplayKit. UA Mobile 2017.
 
Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3Gentlest Introduction to Tensorflow - Part 3
Gentlest Introduction to Tensorflow - Part 3
 
Machine Learning Introduction
Machine Learning IntroductionMachine Learning Introduction
Machine Learning Introduction
 
Sliced Wasserstein距離と生成モデル
Sliced Wasserstein距離と生成モデルSliced Wasserstein距離と生成モデル
Sliced Wasserstein距離と生成モデル
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
 
Learning Deep Learning
Learning Deep LearningLearning Deep Learning
Learning Deep Learning
 
The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189The Ring programming language version 1.6 book - Part 62 of 189
The Ring programming language version 1.6 book - Part 62 of 189
 
Clojure for Data Science
Clojure for Data ScienceClojure for Data Science
Clojure for Data Science
 
Machine Learning Live
Machine Learning LiveMachine Learning Live
Machine Learning Live
 
Python book
Python bookPython book
Python book
 
Machine Learning: Make Your Ruby Code Smarter
Machine Learning: Make Your Ruby Code SmarterMachine Learning: Make Your Ruby Code Smarter
Machine Learning: Make Your Ruby Code Smarter
 
Procedural Content Generation with Clojure
Procedural Content Generation with ClojureProcedural Content Generation with Clojure
Procedural Content Generation with Clojure
 
Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...Secure and privacy-preserving data transmission and processing using homomorp...
Secure and privacy-preserving data transmission and processing using homomorp...
 
Logic gates
Logic gatesLogic gates
Logic gates
 
Let’s talk about microbenchmarking
Let’s talk about microbenchmarkingLet’s talk about microbenchmarking
Let’s talk about microbenchmarking
 

Andere mochten auch

Generative Adversarial Networks
Generative Adversarial NetworksGenerative Adversarial Networks
Generative Adversarial NetworksMark Chang
 
[系列活動] 一日搞懂生成式對抗網路
[系列活動] 一日搞懂生成式對抗網路[系列活動] 一日搞懂生成式對抗網路
[系列活動] 一日搞懂生成式對抗網路台灣資料科學年會
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹台灣資料科學年會
 
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材teddysoft
 
Neural Language Model Tutorial
Neural Language Model Tutorial Neural Language Model Tutorial
Neural Language Model Tutorial Mark Chang
 
淺談深度學習
淺談深度學習淺談深度學習
淺談深度學習Mark Chang
 
TensorFlow 深度學習講座
TensorFlow 深度學習講座TensorFlow 深度學習講座
TensorFlow 深度學習講座Mark Chang
 
Python的50道陰影
Python的50道陰影Python的50道陰影
Python的50道陰影Tim (文昌)
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的機器學習入門 scikit-learn 連淡水阿嬤都聽得懂的機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn Cicilia Lee
 
Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...
Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...
Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...Chris Fregly
 
The Genome Assembly Problem
The Genome Assembly ProblemThe Genome Assembly Problem
The Genome Assembly ProblemMark Chang
 
Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...
Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...
Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...Chris Fregly
 
陸永祥/全球網路攝影機帶來的機會與挑戰
陸永祥/全球網路攝影機帶來的機會與挑戰陸永祥/全球網路攝影機帶來的機會與挑戰
陸永祥/全球網路攝影機帶來的機會與挑戰台灣資料科學年會
 
Machine Learning Essentials (dsth Meetup#3)
Machine Learning Essentials (dsth Meetup#3)Machine Learning Essentials (dsth Meetup#3)
Machine Learning Essentials (dsth Meetup#3)Data Science Thailand
 
高嘉良/Open Innovation as Strategic Plan
高嘉良/Open Innovation as Strategic Plan高嘉良/Open Innovation as Strategic Plan
高嘉良/Open Innovation as Strategic Plan台灣資料科學年會
 
Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...
Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...
Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...Chris Fregly
 
Machine Learning Preliminaries and Math Refresher
Machine Learning Preliminaries and Math RefresherMachine Learning Preliminaries and Math Refresher
Machine Learning Preliminaries and Math Refresherbutest
 

Andere mochten auch (20)

Google TensorFlow Tutorial
Google TensorFlow TutorialGoogle TensorFlow Tutorial
Google TensorFlow Tutorial
 
Generative Adversarial Networks
Generative Adversarial NetworksGenerative Adversarial Networks
Generative Adversarial Networks
 
[系列活動] 一日搞懂生成式對抗網路
[系列活動] 一日搞懂生成式對抗網路[系列活動] 一日搞懂生成式對抗網路
[系列活動] 一日搞懂生成式對抗網路
 
[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰[系列活動] Python爬蟲實戰
[系列活動] Python爬蟲實戰
 
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
[系列活動] 無所不在的自然語言處理—基礎概念、技術與工具介紹
 
Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材Design Patterns這樣學就會了:入門班 Day1 教材
Design Patterns這樣學就會了:入門班 Day1 教材
 
Neural Language Model Tutorial
Neural Language Model Tutorial Neural Language Model Tutorial
Neural Language Model Tutorial
 
淺談深度學習
淺談深度學習淺談深度學習
淺談深度學習
 
TENSORFLOW深度學習講座講義(很硬的課程)
TENSORFLOW深度學習講座講義(很硬的課程)TENSORFLOW深度學習講座講義(很硬的課程)
TENSORFLOW深度學習講座講義(很硬的課程)
 
TensorFlow 深度學習講座
TensorFlow 深度學習講座TensorFlow 深度學習講座
TensorFlow 深度學習講座
 
Python的50道陰影
Python的50道陰影Python的50道陰影
Python的50道陰影
 
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的機器學習入門 scikit-learn 連淡水阿嬤都聽得懂的機器學習入門 scikit-learn
連淡水阿嬤都聽得懂的 機器學習入門 scikit-learn
 
Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...
Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...
Deploy Spark ML and Tensorflow AI Models from Notebooks to Microservices - No...
 
The Genome Assembly Problem
The Genome Assembly ProblemThe Genome Assembly Problem
The Genome Assembly Problem
 
Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...
Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...
Big Data Spain - Nov 17 2016 - Madrid Continuously Deploy Spark ML and Tensor...
 
陸永祥/全球網路攝影機帶來的機會與挑戰
陸永祥/全球網路攝影機帶來的機會與挑戰陸永祥/全球網路攝影機帶來的機會與挑戰
陸永祥/全球網路攝影機帶來的機會與挑戰
 
Machine Learning Essentials (dsth Meetup#3)
Machine Learning Essentials (dsth Meetup#3)Machine Learning Essentials (dsth Meetup#3)
Machine Learning Essentials (dsth Meetup#3)
 
高嘉良/Open Innovation as Strategic Plan
高嘉良/Open Innovation as Strategic Plan高嘉良/Open Innovation as Strategic Plan
高嘉良/Open Innovation as Strategic Plan
 
Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...
Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...
Advanced Spark and TensorFlow Meetup 08-04-2016 One Click Spark ML Pipeline D...
 
Machine Learning Preliminaries and Math Refresher
Machine Learning Preliminaries and Math RefresherMachine Learning Preliminaries and Math Refresher
Machine Learning Preliminaries and Math Refresher
 

Ähnlich wie NTU ML TENSORFLOW

Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNetScaling Deep Learning with MXNet
Scaling Deep Learning with MXNetAI Frontiers
 
TensorFlow 深度學習快速上手班--機器學習
TensorFlow 深度學習快速上手班--機器學習TensorFlow 深度學習快速上手班--機器學習
TensorFlow 深度學習快速上手班--機器學習Mark Chang
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialJin-Hwa Kim
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a ElixirSvet Ivantchev
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlowOswald Campesato
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPaulo Sergio Lemes Queiroz
 
Python profiling
Python profilingPython profiling
Python profilingdreampuf
 
[신경망기초] 합성곱신경망
[신경망기초] 합성곱신경망[신경망기초] 합성곱신경망
[신경망기초] 합성곱신경망jaypi Ko
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowAndrew Ferlitsch
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITiansAshish Bansal
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnnTaiga Nomi
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015ihji
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014PyData
 
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowIntroduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowOswald Campesato
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowSri Ambati
 
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...Codemotion
 
From Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerFrom Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerGuy Hadash
 

Ähnlich wie NTU ML TENSORFLOW (20)

Scaling Deep Learning with MXNet
Scaling Deep Learning with MXNetScaling Deep Learning with MXNet
Scaling Deep Learning with MXNet
 
TensorFlow 深度學習快速上手班--機器學習
TensorFlow 深度學習快速上手班--機器學習TensorFlow 深度學習快速上手班--機器學習
TensorFlow 深度學習快速上手班--機器學習
 
Simple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorialSimple, fast, and scalable torch7 tutorial
Simple, fast, and scalable torch7 tutorial
 
Writing Faster Python 3
Writing Faster Python 3Writing Faster Python 3
Writing Faster Python 3
 
Introducción a Elixir
Introducción a ElixirIntroducción a Elixir
Introducción a Elixir
 
Deep Learning and TensorFlow
Deep Learning and TensorFlowDeep Learning and TensorFlow
Deep Learning and TensorFlow
 
Pythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPUPythonbrasil - 2018 - Acelerando Soluções com GPU
Pythonbrasil - 2018 - Acelerando Soluções com GPU
 
Python profiling
Python profilingPython profiling
Python profiling
 
[신경망기초] 합성곱신경망
[신경망기초] 합성곱신경망[신경망기초] 합성곱신경망
[신경망기초] 합성곱신경망
 
Machine Learning - Introduction to Tensorflow
Machine Learning - Introduction to TensorflowMachine Learning - Introduction to Tensorflow
Machine Learning - Introduction to Tensorflow
 
TensorFlow for IITians
TensorFlow for IITiansTensorFlow for IITians
TensorFlow for IITians
 
Deep learning with C++ - an introduction to tiny-dnn
Deep learning with C++  - an introduction to tiny-dnnDeep learning with C++  - an introduction to tiny-dnn
Deep learning with C++ - an introduction to tiny-dnn
 
Yoyak ScalaDays 2015
Yoyak ScalaDays 2015Yoyak ScalaDays 2015
Yoyak ScalaDays 2015
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
TensorFlow Tutorial.pdf
TensorFlow Tutorial.pdfTensorFlow Tutorial.pdf
TensorFlow Tutorial.pdf
 
Introduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and TensorflowIntroduction to Deep Learning, Keras, and Tensorflow
Introduction to Deep Learning, Keras, and Tensorflow
 
Introduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlowIntroduction to Deep Learning, Keras, and TensorFlow
Introduction to Deep Learning, Keras, and TensorFlow
 
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
Lucio Floretta - TensorFlow and Deep Learning without a PhD - Codemotion Mila...
 
From Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow EagerFrom Tensorflow Graph to Tensorflow Eager
From Tensorflow Graph to Tensorflow Eager
 
H2 o berkeleydltf
H2 o berkeleydltfH2 o berkeleydltf
H2 o berkeleydltf
 

Mehr von Mark Chang

Modeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential EquationModeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential EquationMark Chang
 
Modeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential EquationModeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential EquationMark Chang
 
Information in the Weights
Information in the WeightsInformation in the Weights
Information in the WeightsMark Chang
 
Information in the Weights
Information in the WeightsInformation in the Weights
Information in the WeightsMark Chang
 
PAC Bayesian for Deep Learning
PAC Bayesian for Deep LearningPAC Bayesian for Deep Learning
PAC Bayesian for Deep LearningMark Chang
 
PAC-Bayesian Bound for Deep Learning
PAC-Bayesian Bound for Deep LearningPAC-Bayesian Bound for Deep Learning
PAC-Bayesian Bound for Deep LearningMark Chang
 
Domain Adaptation
Domain AdaptationDomain Adaptation
Domain AdaptationMark Chang
 
Applied Deep Learning 11/03 Convolutional Neural Networks
Applied Deep Learning 11/03 Convolutional Neural NetworksApplied Deep Learning 11/03 Convolutional Neural Networks
Applied Deep Learning 11/03 Convolutional Neural NetworksMark Chang
 
DRAW: Deep Recurrent Attentive Writer
DRAW: Deep Recurrent Attentive WriterDRAW: Deep Recurrent Attentive Writer
DRAW: Deep Recurrent Attentive WriterMark Chang
 
Variational Autoencoder
Variational AutoencoderVariational Autoencoder
Variational AutoencoderMark Chang
 
TensorFlow 深度學習快速上手班--深度學習
 TensorFlow 深度學習快速上手班--深度學習 TensorFlow 深度學習快速上手班--深度學習
TensorFlow 深度學習快速上手班--深度學習Mark Chang
 
TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用Mark Chang
 
Neural Art (English Version)
Neural Art (English Version)Neural Art (English Version)
Neural Art (English Version)Mark Chang
 
AlphaGo in Depth
AlphaGo in Depth AlphaGo in Depth
AlphaGo in Depth Mark Chang
 
Image completion
Image completionImage completion
Image completionMark Chang
 
自然語言處理簡介
自然語言處理簡介自然語言處理簡介
自然語言處理簡介Mark Chang
 
民主的網路世代—推動罷免的工程師們
民主的網路世代—推動罷免的工程師們民主的網路世代—推動罷免的工程師們
民主的網路世代—推動罷免的工程師們Mark Chang
 
NeuralArt 電腦作畫
NeuralArt 電腦作畫NeuralArt 電腦作畫
NeuralArt 電腦作畫Mark Chang
 
Language Understanding for Text-based Games using Deep Reinforcement Learning
Language Understanding for Text-based Games using Deep Reinforcement LearningLanguage Understanding for Text-based Games using Deep Reinforcement Learning
Language Understanding for Text-based Games using Deep Reinforcement LearningMark Chang
 

Mehr von Mark Chang (20)

Modeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential EquationModeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential Equation
 
Modeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential EquationModeling the Dynamics of SGD by Stochastic Differential Equation
Modeling the Dynamics of SGD by Stochastic Differential Equation
 
Information in the Weights
Information in the WeightsInformation in the Weights
Information in the Weights
 
Information in the Weights
Information in the WeightsInformation in the Weights
Information in the Weights
 
PAC Bayesian for Deep Learning
PAC Bayesian for Deep LearningPAC Bayesian for Deep Learning
PAC Bayesian for Deep Learning
 
PAC-Bayesian Bound for Deep Learning
PAC-Bayesian Bound for Deep LearningPAC-Bayesian Bound for Deep Learning
PAC-Bayesian Bound for Deep Learning
 
Domain Adaptation
Domain AdaptationDomain Adaptation
Domain Adaptation
 
Applied Deep Learning 11/03 Convolutional Neural Networks
Applied Deep Learning 11/03 Convolutional Neural NetworksApplied Deep Learning 11/03 Convolutional Neural Networks
Applied Deep Learning 11/03 Convolutional Neural Networks
 
DRAW: Deep Recurrent Attentive Writer
DRAW: Deep Recurrent Attentive WriterDRAW: Deep Recurrent Attentive Writer
DRAW: Deep Recurrent Attentive Writer
 
Variational Autoencoder
Variational AutoencoderVariational Autoencoder
Variational Autoencoder
 
TensorFlow 深度學習快速上手班--深度學習
 TensorFlow 深度學習快速上手班--深度學習 TensorFlow 深度學習快速上手班--深度學習
TensorFlow 深度學習快速上手班--深度學習
 
TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用TensorFlow 深度學習快速上手班--自然語言處理應用
TensorFlow 深度學習快速上手班--自然語言處理應用
 
Neural Doodle
Neural DoodleNeural Doodle
Neural Doodle
 
Neural Art (English Version)
Neural Art (English Version)Neural Art (English Version)
Neural Art (English Version)
 
AlphaGo in Depth
AlphaGo in Depth AlphaGo in Depth
AlphaGo in Depth
 
Image completion
Image completionImage completion
Image completion
 
自然語言處理簡介
自然語言處理簡介自然語言處理簡介
自然語言處理簡介
 
民主的網路世代—推動罷免的工程師們
民主的網路世代—推動罷免的工程師們民主的網路世代—推動罷免的工程師們
民主的網路世代—推動罷免的工程師們
 
NeuralArt 電腦作畫
NeuralArt 電腦作畫NeuralArt 電腦作畫
NeuralArt 電腦作畫
 
Language Understanding for Text-based Games using Deep Reinforcement Learning
Language Understanding for Text-based Games using Deep Reinforcement LearningLanguage Understanding for Text-based Games using Deep Reinforcement Learning
Language Understanding for Text-based Games using Deep Reinforcement Learning
 

Kürzlich hochgeladen

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024BookNet Canada
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 

Kürzlich hochgeladen (20)

Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 

NTU ML TENSORFLOW

  • 2. Outlines • One-layer Perceptron • Tensorboard • Convolutional Neural Networks • Multi-GPU Implementation
  • 4. How to Install Tensorflow • Direct step: – install tensorflow • Install in virtual environment: – install pyenv – install anaconda-2.x.x – install tensorflow > sudo pip install tensorflow > pip install tensorflow
  • 5. 1. install pyenv • Mac OSX Homebrew • From Github (for ubuntu user, please replace .bash_profile with .bashrc) > cd ~ > git clone https://github.com/yyuu/pyenv.git ~/.pyenv > echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile > echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile > echo 'eval "$(pyenv init -)"' >> ~/.bash_profile > source ~/.bash_profile > brew update > brew install pyenv
  • 6. install anaconda-2.x.x and Tensorflow • install anaconda-2.4.0 • switch to anaconda 2.4.0 • Install tensorflow > pyenv install anaconda-2.4.0 > pyenv global anaconda-2.4.0 > pip install tensorflow
  • 7. Features of Tensorflow • Computational Graph – Define computation • Session – Run computation
  • 9. MNIST • Image Classification • Multi-class:0~9 https://www.tensorflow.org/versions/r0.7/images/MNIST.png
  • 14. One-layer Perceptron x_ = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x_,W) + b) cross_entropy = -tf.reduce_mean(y_ * tf.log(y)) optimizer = tf.train.GradientDescentOptimizer(0.01) trainer = optimizer.minimize(cross_entropy) init = tf.global_variables_initializer() sess = tf.Session() sess.run(init) for i in range(1000): sess.run(train, feed_dict={x_:x_data,y_:y_data}) print sess.run(cross_entropy, feed_dict={x_:x_data,y_:y_data}) sess.close() Computational Graph Session
  • 15. Computation Graph # placeholder x_ = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) # variable W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) # operations y = tf.nn.softmax(tf.matmul(x_, W) + b) # error function cross_entropy = -tf.reduce_sum(y_* tf.log(y)) # trainer optimizer = tf.train.GradientDescentOptimizer(0.01) trainer = optimizer.minimize(cross_entropy) # initalizer init = tf.global_variables_initializer()
  • 16. Placeholder 0. … 0. 0. … 0. 0. … 0.1 0. … 0. 0. … 0.2 0. … 0. … … … x_ y_ x_ = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) 0. … 0. 0. … 0. 0. … 1. 1. … 0. 0. … 0. 0. … 0. … … … 784 10 n
  • 17. Variable W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) bw 0. …….. 0. 0. …….. 0. 0. …….. 0. …….. …….. …….. 0. …….. 0. 0. …….. 0. 0. 0. …….. 0. 0. 784 10 10
  • 18. Matrix Multiplication y = tf.nn.softmax(tf.matmul(x_, W) + b) https://www.tensorflow.org/versions/r0.8/images/softmax-regression-scalarequation.png
  • 19. Matrix Multiplication y = tf.nn.softmax(tf.matmul(x_, W) + b) https://www.tensorflow.org/versions/r0.8/images/softmax-regression-vectorequation.png
  • 20. Batch • Improve parallelization • Ex, batch size = 4: x = 2 6 6 6 4 x (1) 1 x (1) 2 x (1) 3 x (2) 1 x (2) 2 x (2) 3 x (3) 1 x (3) 2 x (3) 3 x (4) 1 x (4) 2 x (4) 3 3 7 7 7 5
  • 21. Matrix Multiplication with Batch y = tf.nn.softmax(tf.matmul(x_, W) + b) x = 2 6 6 6 4 x (1) 1 x (1) 2 x (1) 3 x (2) 1 x (2) 2 x (2) 3 x (3) 1 x (3) 2 x (3) 3 x (4) 1 x (4) 2 x (4) 3 3 7 7 7 5 W = 2 4 W1,1 W1,2 W1,3 W2,1 W2,2 W2,3 W3,1 W3,2 W3,3 3 5 b = 2 4 b1 b2 b3 3 5 matmul(x , W) + b = 2 6 6 6 4 x (1) 1 W1,1 + x (1) 2 W2,1 + x (1) 3 W3,1 + b1 · · · x (1) 1 W1,3 + x (1) 2 W2,3 + x (1) 3 W3,3 + b3 x (2) 1 W1,1 + x (2) 2 W2,1 + x (2) 3 W3,1 + b1 · · · x (2) 1 W1,3 + x (2) 2 W2,3 + x (2) 3 W3,3 + b3 x (3) 1 W1,1 + x (3) 2 W2,1 + x (3) 3 W3,1 + b1 · · · x (3) 1 W1,3 + x (3) 2 W2,3 + x (3) 3 W3,3 + b3 x (4) 1 W1,1 + x (4) 2 W2,1 + x (4) 3 W3,1 + b1 · · · x (4) 1 W1,3 + x (4) 2 W2,3 + x (4) 3 W3,3 + b3 3 7 7 7 5
  • 22. Matrix Multiplication with Batch y = tf.nn.softmax(tf.matmul(x_, W) + b) wx_ tf.matmul(x_,w)+b b 0. ….. 0. 0. ….. 0. ….. ….. ….. 784 0. ….. 0. ….. ….. ….. 0. ….. 0. 10 10 0. ….. 0. 784 n 0. ….. 0. ….. ….. ….. 0. ….. 0. 10 n
  • 23. Softmax y = tf.nn.softmax(X) X = 2 6 6 6 4 X (1) 1 X (1) 2 X (1) 3 X (2) 1 X (2) 2 X (2) 3 X (3) 1 X (3) 2 X (3) 3 X (4) 1 X (4) 2 X (4) 3 3 7 7 7 5 y = softmax(X) = 2 6 6 6 6 6 6 6 6 6 4 eX (1) 1 eX (1) 1 +eX (1) 2 +eX (1) 3 eX (1) 2 eX (1) 1 +eX (1) 2 +eX (1) 3 eX (1) 3 eX (1) 1 +eX (1) 2 +eX (1) 3 eX (2) 1 eX (2) 1 +eX (2) 2 +eX (2) 3 eX (2) 2 eX (2) 1 +eX (2) 2 +eX (2) 3 eX (2) 3 eX (2) 1 +eX (2) 2 +eX (2) 3 eX (3) 1 eX (3) 1 +eX (3) 2 +eX (3) 3 eX (3) 2 eX (3) 1 +eX (3) 2 +eX (3) 3 eX (3) 3 eX (3) 1 +eX (3) 2 +eX (3) 3 eX (4) 1 eX (4) 1 +eX (4) 2 +eX (4) 3 eX (4) 2 eX (4) 1 +eX (4) 2 +eX (4) 3 eX (4) 3 eX (4) 1 +eX (4) 2 +eX (4) 3 3 7 7 7 7 7 7 7 7 7 5
  • 24. Softmax y = tf.nn.softmax(tf.matmul(x_, W) + b) tf.nn.softmax 0. ….. 0. ….. ….. ….. 0. ….. 0. 10 n 0. ….. 0. ….. ….. ….. 0. ….. 0. 10 n
  • 25. Error Function cross_entropy = -tf.reduce_mean(y_* tf.log(y)) y = 2 6 6 4 1 0 0 0 1 0 1 0 0 0 0 1 3 7 7 5 log(y) = 2 6 6 6 4 log(y (1) 1 ) log(y (1) 2 ) log(y (1) 3 ) log(y (2) 1 ) log(y (2) 2 ) log(y (2) 3 ) log(y (3) 1 ) log(y (3) 2 ) log(y (3) 3 ) log(y (4) 1 ) log(y (4) 2 ) log(y (4) 3 ) 3 7 7 7 5 y ⇤ log(y) = 2 6 6 6 4 log(y (1) 1 ) 0 0 0 log(y (2) 2 ) 0 log(y (3) 1 ) 0 0 0 0 log(y (4) 3 ) 3 7 7 7 5
  • 26. Error Function cross_entropy = -tf.reduce_mean(y_* tf.log(y)) y ⇤ log(y) = 2 6 6 6 4 log(y (1) 1 ) 0 0 0 log(y (2) 2 ) 0 log(y (3) 1 ) 0 0 0 0 log(y (4) 3 ) 3 7 7 7 5 reduce mean(y ⇤ log(y)) = 1 4 log(y (1) 1 ) + log(y (2) 2 ) + log(y (3) 1 ) + log(y (4) 3 )
  • 27. Error Function cross_entropy = -tf.reduce_mean(y_* tf.log(y)) y_ y 1.4331052 -tf.reduce_mean(y_*tf.log(y)) 0. ….. 0. ….. ….. ….. 0. ….. 0. 10 n 0. ….. 0. ….. ….. ….. 0. ….. 0. 10 n
  • 29. Trainer w w ⌘ @E(h(X), Y ) @w b b ⌘ @E(h(X), Y ) @b
  • 30. Computation Graph • Initializer init = tf.global_variables_initializer() w b W = tf.Variable(tf.zeros([784, 10])) b = tf.Variable(tf.zeros([10])) 0. …….. 0. …….. …….. …….. 0. …….. 0. 0. …….. 0.
  • 31. Session # create session sess = tf.Session() # initializevariable sess.run(init) # gradient descent for step in xrange(1000): sess.run(train, feed_dict={x_:x_data,y_:y_data}) # fetch variable print sess.run(cross_entropy, feed_dict={x_:x_data,y_:y_data}) # release resource sess.close()
  • 32. Run Operations sess.run(init) the Node in Computational Graph
  • 33. Run Operations for step in xrange(1000): sess.run(train, feed_dict={x_:x_data,y_:y_data} ) the Node in Computational Graph Input Data x_data y_data 0. … 0. 0. … 0. 0. … 0.1 0. … 0. 0. … 0.2 0. … 0. … … … 0. … 0. 0. … 0. 0. … 1. 1. … 0. 0. … 0. 0. … 0. … … …
  • 34. Run Operations print sess.run(cross_entropy, feed_dict={x_:x_data,y_:y_data}) the Node in Computational Graph Input Data x_data y_data Results 2.4564333 0. … 0. 0. … 0. 0. … 0.1 0. … 0. 0. … 0.2 0. … 0. … … … 0. … 0. 0. … 0. 0. … 1. 1. … 0. 0. … 0. 0. … 0. … … …
  • 35. Evaluation # computational graph correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # session result_accurarcy = sess.run(accuracy, feed_dict={x_: mnist.test.images, y_: mnist.test.labels})
  • 36. Evaluation correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) y = 2 6 6 4 0.8 0.2 0 0 1.0 0.0 0.4 0.5 0.1 0 0.1 0.9 3 7 7 5 y = 2 6 6 4 1 0 0 0 1 0 1 0 0 0 0 1 3 7 7 5 argmax(y) = 2 6 6 4 0 1 1 2 3 7 7 5 argmax(y ) = 2 6 6 4 0 1 0 2 3 7 7 5 equal(argmax(y), argmax(y )) = 2 6 6 4 True True False True 3 7 7 5
  • 37. Evaluation accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) correct prediction = 2 6 6 4 True True False True 3 7 7 5 cast(correct prediction, float32) = 2 6 6 4 1.0 1.0 0.0 1.0 3 7 7 5 reduce mean(cast(correct prediction, float32)) = 0.75
  • 38. Decomposing Gradient Descent • https://github.com/ckmarkoh/ntu_tensorf low/blob/master/one_layer_compute_gr adients.ipynb
  • 39. Decomposing Gradient Descent grads = optimizer.compute_gradients(cross_entropy) apply_grads = optimizer.apply_gradients(grads) w w ⌘ @E(h(X), Y ) @w b b ⌘ @E(h(X), Y ) @b Compute Gradients Apply Gradients w ⌘ @E(h(X), Y ) @w b ⌘ @E(h(X), Y ) @b w ⌘ @E(h(X), Y ) @w b ⌘ @E(h(X), Y ) @b
  • 40. Save and Load Trained Model • https://github.com/ckmarkoh/ntu_tensorf low/blob/master/one_layer_load.ipynb
  • 41. Save and Load Trained Model • Save Trained Model • Load Trained Model saver = tf.train.Saver() saver.save(sess, "model.ckpt") saver = tf.train.Saver() saver.restore(sess, "model.ckpt")
  • 42. Save and Load Trained Model • .ckpt.data : values of all variables • .ckpt.meta : structure of the computational graph • How to save and load computational graph? – https://www.tensorflow.org/versions/r0.12/ho w_tos/meta_graph/
  • 45. Scalar Summary summ_ce = tf.summary.scalar("cross_entropy", cross_entropy) summ_acc = tf.summary.scalar("accuracy", accuracy) cross_entropy accurarcy
  • 46. Histogram Summary summ_W = tf.summary.histogram("weights", W) summ_b = tf.summary.histogram("biases", b) weights biases
  • 47. Summary Writer summ_merged = tf.summary.merge([summ_W, summ_b, summ_ce]) writer = tf.summary.FileWriter("./", sess.graph_def) for i in range(1000): batch_xs, batch_ys = mnist.train.next_batch(100) result1 = sess.run(tf.log(y), feed_dict={x_: batch_xs, y_: batch_ys}) result2 = sess.run(y_ * tf.log(y), feed_dict={x_: batch_xs, y_: batch_ys}) sess.run(trainer, feed_dict={x_: batch_xs, y_: batch_ys}) summ_str = sess.run(summ_merged,feed_dict={x_:batch_xs,y_:batch_ys}) writer.add_summary(summ_str,i) if (i+1)%5 == 0: summary_str = sess.run(summ_acc,feed_dict={x_:mnist.test.images, y_:mnist.test.labels}) writer.add_summary(summ_str,i)
  • 48. name_scope with tf.name_scope("cross_entropy") as scope: cross_entropy = -tf.reduce_mean(y_ * tf.log(y))
  • 49. Launch Tensorboard > tensorboard --logdir=./ Starting TensorBoard on port 6006 (You can navigate to http://0.0.0.0:6006)
  • 50. Viewing Computational Graph without Tensorboard • https://github.com/ckmarkoh/ntu_tensorf low/blob/master/computational_graph_ py.ipynb
  • 51. Viewing Computational Graph without Tensorboard • tf.get_default_graph() – get the computational graph • tf.get_default_graph().get_operations() – get all nodes in computational graph • tf.get_default_graph().get_tensor_by_name ("name:0") – get the node by name • tf.global_variables() – get all variables – What is a local variable in tensorflow? • https://stackoverflow.com/questions/38910198/what- is-a-local-variable-in-tensorflow
  • 52. Convolutional Neural Networks • https://github.com/ckmarkoh/ntu_tensorf low/blob/master/convnet_train.ipynb
  • 53. Create Variables & Operators def weight_variable(shape): return tf.Variable(tf.truncated_normal(shape,stddev=0.1)) def bias_variable(shape): return tf.Variable(tf.constant(0, shape=shape)) def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
  • 54. Computational Graph x_ = tf.placeholder(tf.float32, [None, 784], name="x_") y_ = tf.placeholder(tf.float32, [None, 10], name="y_”) x_image = tf.reshape(x_, [-1,28,28,1]) W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) h_pool1 = max_pool_2x2(h_conv1) W_conv2 = weight_variable([5, 5, 32, 64]) b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y= tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
  • 56. Reshape x_image = tf.reshape(x_, [-1,28,28,1]) x n 784 n 28 1
  • 57. Convolutional Layer W_conv1 = weight_variable([5, 5, 1, 32]) b_conv1 = bias_variable([32]) 5 1 32 32 5x5 1 32 32 W_conv1 W_conv1 b_conv1 b_conv1
  • 58. Convolutional Layer tf.nn.conv2d(x, W , strides=[1, 1, 1, 1], padding='SAME')+b 1 5x5 1x1 28 28 28 28 strides=1 padding='SAME' [ batch, in_height, in_width, in_channels ]
  • 59. Convolutional Layer tf.nn.conv2d(x, W , strides=[1, 1, 1, 1], padding='SAME')+b nx28x28x1 nx28x28x32 28 28 28 28
  • 60. ReLU h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) ReLU: ⇢ nin if nin > 0 0 otherwise -0.5 0.2 0.3 -0.1 0.2 -0.3 -0.4 -1.1 2.1 -2.1 0.1 1.2 0.2 3.0 -0.3 0.5 0 0.2 0.3 0 0.2 0 0 0 2.1 0 0.1 1.2 0.2 3.0 0 0.5
  • 61. Pooling Layer tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 1x2x2x1 1 1 1 1 2 2x2 1x1
  • 62. Pooling Layer tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') 2 strides=2 padding='SAME' 28 28 14 14
  • 63. Pooling Layer h_pool1 = max_pool_2x2(h_conv1) nx28x28x32 nx14x14x32 28 28 14 14
  • 66. Dropout • Randomly remove neurons in hidden layers • ex: 25%的Dropout Rate x (2) 1 x (2) 2 x (1) 1 x (1) 2 x (2) 1 x (2) 2 x (1) 1 x (1) 2 y(1) y(2) y(1) y(2)
  • 67. Dropout • During testing, all weights should be multiply by (1 – dropout_rate) x (2) 1 x (2) 2 y(1) w w(1 dropout rate)
  • 68. Dropout • keep_prob = 1- dropout_rate W_fc1 = weight_variable([7 * 7 * 64, 1024]) b_fc1 = bias_variable([1024]) h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat,W_fc1) + b_fc1) keep_prob = tf.placeholder(tf.float32) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) W_fc2 = weight_variable([1024,10]) b_fc2 = bias_variable([10]) y= tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)
  • 69. Early Stop Validation Loss Training Loss Stop Training t
  • 70. Early Stop patience = 20 best_accurarcy = 0 i = 0 while True: i += 1 batch_xs, batch_ys = mnist.train.next_batch(100) sess.run(trainer, feed_dict={x_: batch_xs, y_: batch_ys, keep_prob:0.5}) if i%100== 0: valid_accurarcy = sess.run(accuracy, feed_dict={x_: mnist.validation.images, y_: mnist.validation.labels, keep_prob:1}) print "%s, valid_accurarcy:%s" %(I, valid_accurarcy) if valid_accurarcy > best_accurarcy: patience = 20 best_accurarcy = valid_accurarcy print "save model" saver.save(sess, "model_conv.ckpt") else: patience -= 1 if patience == 0: print "early stop" break
  • 72. Multi-GPU Implementation • Variables: – stored in CPU – shared by GPUs – updated by CPU • Gradients: – computed by GPUs – averaged by CPU https://www.tensorflow.org/images/Parallelism.png
  • 73. Multi-GPU Implementation • create variables stored in CPU def create_weight_and_bias(weight_shape,bias_shape): with tf.device('/cpu:0'): weight = tf.get_variable("weight",initializer=tf.truncated_normal(weight_shape)) bias = tf.get_variable("bias", initializer= tf.constant(0,shape=bias_shape)) return weight, bias
  • 74. Multi-GPU Implementation • input data for GPUs for i in range(num_gpus): with tf.device('/gpu:%d' % i): with tf.name_scope('tower_%d' % (i)) as scope: if i > 0: reuse = True else: reuse = False x_next = x_all[i * batch_size:(i + 1) * batch_size, :] y_next = y_all[i * batch_size:(i + 1) * batch_size, :] loss, acc = create_cnn(x_next, y_next, keep_prob, reuse=reuse) grads = optimizer.compute_gradients(loss) tower_grads.append(grads) tower_acc.append(acc) batch 1 batch 2
  • 75. Multi-GPU Implementation • create model and loss in GPUs Tower 1 Tower 2 for i in range(num_gpus): with tf.device('/gpu:%d' % i): with tf.name_scope('tower_%d' % (i)) as scope: if i > 0: reuse = True else: reuse = False x_next = x_all[i * batch_size:(i + 1) * batch_size, :] y_next = y_all[i * batch_size:(i + 1) * batch_size, :] loss, acc = create_cnn(x_next, y_next, keep_prob, reuse=reuse) grads = optimizer.compute_gradients(loss) tower_grads.append(grads) tower_acc.append(acc)
  • 76. Multi-GPU Implementation • compute gradients in GPUs tower_grads = [] tower_acc = [] for i in range(num_gpus): with tf.device('/gpu:%d' % i): with tf.name_scope('tower_%d' % (i)) as scope: if i > 0: reuse = True else: reuse = False x_next = x_all[i * batch_size:(i + 1) * batch_size, :] y_next = y_all[i * batch_size:(i + 1) * batch_size, :] loss, acc = create_cnn(x_next, y_next, keep_prob, reuse=reuse) grads = optimizer.compute_gradients(loss) tower_grads.append(grads) tower_acc.append(acc) Tower 1 Tower 2
  • 77. Multi-GPU Implementation • average and update gradients in CPU with graph.as_default(), tf.device('/cpu:0'): for i in range(num_gpus): with tf.device('/gpu:%d' % i): with tf.name_scope('tower_%d' % (i)) as scope: … tower_grads.append(grads) tower_acc.append(acc) avg_grads = create_average_gradients(tower_grads) avg_acc = tf.reduce_mean(tower_acc) trainer = optimizer.apply_gradients(avg_grads) Tower 1 Tower 2
  • 78. Multi-GPU Implementation • train Multi-GPU Implementation for i in range(3000): batch_xs, batch_ys = mnist.train.next_batch(batch_size * num_gpus) sess.run(trainer, feed_dict={x_all: batch_xs, y_all: batch_ys, keep_prob: 0.5})
  • 79. 講師資訊 • Email: ckmarkoh at gmail dot com • Blog: http://cpmarkchang.logdown.com • Github: https://github.com/ckmarkoh • Facebook: https://www.facebook.com/ckmarkoh.chang • Slideshare: http://www.slideshare.net/ckmarkohchang Mark Chang HTC Research & Healthcare 79