TensorFlow部分函数理解(一)
本篇介绍函数包括:
tf.conv2d
tf.nn.relu
tf.nn.max_pool
tf.nn.droupout
tf.nn.sigmoid_cross_entropy_with_logits
tf.truncated_normal
tf.constant
tf.placeholder
tf.nn.bias_add
tf.reduce_mean
tf.squared_difference
tf.square tf.Variable
tf.conv2d
import tensorflow as tf a = tf.constant([1,1,1,0,0,0,1,1,1,0,0,0,1,1,1,0,0,1,1,0,0,1,1,0,0],dtype=tf.float32,shape=[1,5,5,1]) b = tf.constant([1,0,1,0,1,0,1,0,1],dtype=tf.float32,shape=[3,3,1,1]) c = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='VALID') d = tf.nn.conv2d(a,b,strides=[1, 2, 2, 1],padding='SAME') with tf.Session() as sess: print ("c shape:") print (c.shape) print ("c value:") print (sess.run(c)) print ("d shape:") print (d.shape) print ("d value:") print (sess.run(d))
然后执行:
cd /home/ubuntu; python conv2d.py
执行结果:
c shape: (1, 3, 3, 1) c value: [[[[ 4.] [ 3.] [ 4.]] [[ 2.] [ 4.] [ 3.]] [[ 2.] [ 3.] [ 4.]]]] d shape: (1, 5, 5, 1) d value: [[[[ 2.] [ 2.] [ 3.] [ 1.] [ 1.]] [[ 1.] [ 4.] [ 3.] [ 4.] [ 1.]] [[ 1.] [ 2.] [ 4.] [ 3.] [ 3.]] [[ 1.] [ 2.] [ 3.] [ 4.] [ 1.]] [[ 0.] [ 2.] [ 2.] [ 1.] [ 1.]]]]
tf.nn.relu:
import tensorflow as tf a = tf.constant([1,-2,0,4,-5,6]) b = tf.nn.relu(a) with tf.Session() as sess: print (sess.run(b))
然后执行:
cd /home/ubuntu; python relu.py
执行结果:
[1 0 0 4 0 6]
tf.nn.max_pool
import tensorflow as tf a = tf.constant([1,3,2,1,2,9,1,1,1,3,2,3,5,6,1,2],dtype=tf.float32,shape=[1,4,4,1]) b = tf.nn.max_pool(a,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='VALID') c = tf.nn.max_pool(a,ksize=[1, 2, 2, 1],strides=[1, 2, 2, 1],padding='SAME') with tf.Session() as sess: print ("b shape:") print (b.shape) print ("b value:") print (sess.run(b)) print ("c shape:") print (c.shape) print ("c value:") print (sess.run(c))
然后执行:
cd /home/ubuntu; python max_pool.py
执行结果:
b shape: (1, 2, 2, 1) b value: [[[[ 9.] [ 2.]] [[ 6.] [ 3.]]]] c shape: (1, 2, 2, 1) c value: [[[[ 9.] [ 2.]] [[ 6.] [ 3.]]]]
tf.nn.droupout
import tensorflow as tf a = tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32) b = tf.placeholder(tf.float32) c = tf.nn.dropout(a,b,[2,1],1) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) print (sess.run(c,feed_dict={b:0.75}))
然后执行:
cd /home/ubuntu; python dropout.py
执行结果:
[[ 0. 0. 0. ] [ 5.33333349 6.66666651 8. ]]
tf.nn.sigmoid_cross_entropy_with_logits
import tensorflow as tf x = tf.constant([1,2,3,4,5,6,7],dtype=tf.float64) y = tf.constant([1,1,1,0,0,1,0],dtype=tf.float64) loss = tf.nn.sigmoid_cross_entropy_with_logits(labels = y,logits = x) with tf.Session() as sess: print (sess.run(loss))
然后执行:
cd /home/ubuntu; python sigmoid_cross_entropy_with_logits.py
执行结果:
[ 3.13261688e-01 1.26928011e-01 4.85873516e-02 4.01814993e+00 5.00671535e+00 2.47568514e-03 7.00091147e+00]
tf.truncated_normal
import tensorflow as tf initial = tf.truncated_normal(shape=[3,3], mean=0, stddev=1) print(tf.Session().run(initial))
然后执行:
python /home/ubuntu/truncated_normal.py
执行结果:
将得到一个取值范围 [ -2, 2 ] 的 3 * 3 矩阵,您也可以尝试修改源代码看看输出结果有什么变化?
[[-1.01231802 1.25015056 0.39860222]
[ 0.43949991 -0.80240148 0.81758308]
[-0.76539534 1.95935833 1.20631492]]
tf.constant
#!/usr/bin/python import tensorflow as tf import numpy as np a = tf.constant([1,2,3,4,5,6],shape=[2,3]) b = tf.constant(-1,shape=[3,2]) c = tf.matmul(a,b) e = tf.constant(np.arange(1,13,dtype=np.int32),shape=[2,2,3]) f = tf.constant(np.arange(13,25,dtype=np.int32),shape=[2,3,2]) g = tf.matmul(e,f) with tf.Session() as sess: print (sess.run(a)) print ("##################################") print (sess.run(b)) print ("##################################") print (sess.run(c)) print ("##################################") print (sess.run(e)) print ("##################################") print (sess.run(f)) print ("##################################") print (sess.run(g))
然后执行:
python /home/ubuntu/constant.py
执行结果:
a: 2x3 维张量; b: 3x2 维张量; c: 2x2 维张量; e: 2x2x3 维张量; f: 2x3x2 维张量; g: 2x2x2 维张量。
tf.placeholder
#!/usr/bin/python import tensorflow as tf import numpy as np x = tf.placeholder(tf.float32,[None,3]) y = tf.matmul(x,x) with tf.Session() as sess: rand_array = np.random.rand(3,3) print(sess.run(y,feed_dict={x:rand_array}))
然后执行:
python /home/ubuntu/placeholder.py
执行结果:
输出一个 3x3 的张量
[[ 1.04605961 0.45888701 0.6270988 ]
[ 0.86465603 0.87210596 0.71620005]
[ 0.54584444 0.44113758 0.6248076 ]]
tf.nn.bias_add
#!/usr/bin/python import tensorflow as tf import numpy as np a = tf.constant([[1.0, 2.0],[1.0, 2.0],[1.0, 2.0]]) b = tf.constant([2.0,1.0]) c = tf.constant([1.0]) sess = tf.Session() print (sess.run(tf.nn.bias_add(a, b))) #print (sess.run(tf.nn.bias_add(a,c))) error print ("##################################") print (sess.run(tf.add(a, b))) print ("##################################") print (sess.run(tf.add(a, c)))
执行结果:
[[ 3. 3.]
[ 3. 3.]
[ 3. 3.]]
##################################
[[ 3. 3.]
[ 3. 3.]
[ 3. 3.]]
##################################
[[ 2. 3.]
[ 2. 3.]
[ 2. 3.]]
tf.reduce_mean
#!/usr/bin/python import tensorflow as tf import numpy as np initial = [[1.,1.],[2.,2.]] x = tf.Variable(initial,dtype=tf.float32) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) print(sess.run(tf.reduce_mean(x))) print(sess.run(tf.reduce_mean(x,0))) #Column print(sess.run(tf.reduce_mean(x,1))) #row
然后执行:
python /home/ubuntu/reduce_mean.py
执行结果:
1.5 [ 1.5 1.5] [ 1. 2.]
tf.squared_difference
#!/usr/bin/python import tensorflow as tf import numpy as np initial_x = [[1.,1.],[2.,2.]] x = tf.Variable(initial_x,dtype=tf.float32) initial_y = [[3.,3.],[4.,4.]] y = tf.Variable(initial_y,dtype=tf.float32) diff = tf.squared_difference(x,y) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) print(sess.run(diff))
然后执行:
python /home/ubuntu/squared_difference.py
执行结果:
[[ 4. 4.] [ 4. 4.]]
tf.square
#!/usr/bin/python import tensorflow as tf import numpy as np initial_x = [[1.,1.],[2.,2.]] x = tf.Variable(initial_x,dtype=tf.float32) x2 = tf.square(x) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) print(sess.run(x2))
然后执行:
python /home/ubuntu/square.py
执行结果:
[[ 1. 1.] [ 4. 4.]]