《Tensorflow 实战 Google 深度学习框架》 读书笔记
这是第二遍、第三遍,其实之前也没怎么读懂,在 python 还不怎么熟悉,Tensorflow 还比较晦涩情况下写这篇笔记。Internet 上资料比较少,有种俨然用着大头电脑 ,拍着机械键盘的赶脚!但是,没关系,历史的车轮滚滚向前, 很多时候,很多事情是有很多相似性的,必须找出上一轮相似事情,找到核心,再来解决问题,每一轮都可以解决一点点。这算不算愚公移山呢?其实也不重要啦!有一点很重要的是,每天都狠有目标感,每天都对标自己有多少目标已经完成,有多少目标没有完成这件事情不是一直都很重要的吗?
今天的博客,写几个函数。
import tensorflow as tf tf.compat.v1.placeholder() tf.compat.v1.random.truncated_normal() tf.nn.softmax() tf.matmul() tf.reduce_mean() tf.reduce_sum() tf.math.log() tf.compat.v1.train.AdamOptimizer().minimize() tf.argmax() tf.equal(a,b) tf.cast(a, tf.float32) tf.compat.v1.Session() tf.compat.v1.global_variable_initializer() tf.nn.relu()
1.tf.compat.v1.placeholder()
自己理解,定义变量的函数。变量是算法,以公式的模式展现,在 sess.run() 的时候,真正实例化。
#(1)公式制作; a=tf.compat.v1.placeholder(tf.float32, shape=[3,4]); b=tf.compat.v1.placeholder(tf.float32, shape=[4,1]); m = tf.matmul( a,b ) #(2)实例化 exemplify a1=[]; b1=[] feed_in = {a:a1, b:b1} print( sess.run( m, feed_fict=feed_in ) )
[[14.] [26.]]
2.tf.compat.v1.random.truncated_normal()
解释:Outputs random values from a truncated normal distribution.
注意,(4)tf.matmul() 放入的是实例化的向量。
import tensorflow as tf a = tf.Variable(tf.compat.v1.random.truncated_normal([2, 2], stddev=0.1)) a2= tf.Variable(tf.compat.v1.random.truncated_normal([2, 2], stddev=0.1)) a1=[[3,4], [5,6]] feed_in = {a:a1, a2:a1} a3 = sess.run( a, feed_dict={a: a1} ) a4 = sess.run( a2, feed_dict={a2: a1} ) b = tf.matmul( a3, a4 ) print( sess.run( b ) )
3.tf.nn.softmax()
# tf.nn.softmax() import tensorflow as tf x = tf.constant([[3.0, 1.0, -3.0, ]]) tf.compat.v1.global_variables_initializer() # 初始化所有变量 with tf.compat.v1.Session() as sess: print( sess.run( tf.nn.softmax(x) ) )
[[0.87887824 0.11894324 0.00217852]]
4.tf.reduce_mean()
# tf.reduce_mean() 正常的求均值的函数 import tensorflow as tf x = tf.constant([[3.0, 2.0, -3.0]]) x1 = tf.compat.v1.placeholder( tf.float32 ) # feed 方式没有成功 y = tf.reduce_mean( x, axis=1 ) a1 = tf.constant([[2,3,4,5,6]]) with tf.compat.v1.Session() as sess: print( sess.run(y) )
[0.6666667]
5.tf.reduce_sum()
# tf.reduce_sum() 加和函数 import tensorflow as tf x = tf.constant([[3,2,1], [2,5,4]]) y=tf.reduce_sum( x, axis=0 ) y1=tf.reduce_sum( x, axis=1 ) with tf.compat.v1.Session() as sess: print("Leave a line: ", sess.run( y )) print("Leave a col: ", sess.run( y1 ))
6.tf.math.log()
# tf.math.log() import tensorflow as tf x = tf.constant([[1, 2.718, 10]]) y = tf.math.log( x ) with tf.compat.v1.Session() as sess: print( sess.run( y ) )
7.tf.compat.v1.train.AdamOptimizer()
8.tf.argmax()
# tf.argmax() import tensorflow as tf x = tf.constant([[1,4,3]]) y = tf.argmax(x, axis=1) # 返回 index with tf.compat.v1.Session() as sess: print( sess.run( y ) )
9.tf.equal()
# tf.equal() import tensorflow as tf x1 = tf.constant([[0,1,1]]) x2 = tf.constant([[1,1,1]]) y = tf.equal(x1, x2) with tf.compat.v1.Session() as sess: print( sess.run( y ) )
10.tf.cast()
# tf.cast() import tensorflow as tf x = 1 y1 = tf.cast(x, tf.int32) y2 = tf.cast(x, tf.float32) with tf.compat.v1.Session() as sess: print(sess.run(y1)) print(sess.run(y2))
11.tf.compat.v1.Session()
import tensorflow as tf w1 = tf.Variable( tf.compat.v1.random.truncated_normal( [2, 3], stddev=1, seed=1 ) ) w2 = tf.Variable( tf.compat.v1.random.truncated_normal( [3, 1], stddev=1, seed=1 ) ) x = tf.constant( [[0.7, 0.9]] ) a = tf.matmul(x, w1) y = tf.matmul(a, w2) sess = tf.compat.v1.Session() sess.run( w1.initializer ) sess.run( w2.initializer ) print( sess.run(y) ) sess.close()
12.tf.compat.v1.global_variable_initializer()
这个函数出现在 tf.compat.v1.Session() 中,通常有全局变量的时候,要通知 TensorFlosw 的语句,可认为是一种定义语句,效果是,不使用的话,部分变量报错。
13.tf.nn.relu()
欢迎关注 shoelesscai.com,我们尽所能提供信息。