【TensorFlow-windows】学习笔记一——基础理解
前言
因為Theano已經停止更新了,所以在前面學完Theano搭建RBM,CNN,RNN相關結構以后,還是得選擇一個主流框架的,由于我自身的學習最終是向強化學習靠近,可能用到的仿真環境是openai gym,所以選擇了繼續學習TensorFlow,而非pyTorch,CNTK之類的深度學習框架。
還是按照Theano的學習方法來,先學習一下入門基礎,比如基本存儲容器,變量運算,函數執行與取值等操作,然后學一下手寫數字識別什么的。
國際慣例,參考博客:
一臉懵逼的官方教程
清晰易懂的中文社區
機器學習速成課程中的TensorFlow
TensorFlow官方文檔中文譯
簡介
在使用入門中提到了TensorFlow分為Eager Execution和Graph Execution,此外還有一些必須了解的API,包括高階API、中階API、低階API、核。但是像我們這種做算法的,一般只需關注如何使用某類語言搭建網絡結構即可。
Eager Execution
它是TensorFlow的一種命令式編程環境,特點是無法構建圖,可立即評估操作并輸出具體數值:
#Eager Execution import tensorflow as tf tf.enable_eager_execution() print('是否開啟eager execution:',tf.executing_eagerly())x=[[2.]] m=tf.matmul(x,x) print('hello,{}'.format(m)結果:
是否開啟eager execution: True hello,[[4.]]可以看出,如果我們要使用Eager Execution調試程序,必須先啟動它,啟動了以后,執行很多操作都無需創建圖去執行運算,比如你不啟動它,直接運行計算:
import tensorflow as tf x=[[2.]] m=tf.matmul(x,x) print('hello,{}'.format(m))輸出:
hello,Tensor("MatMul:0", shape=(1, 1), dtype=float32)很容易發現輸出的是數據類型和大小
再來個復雜點的例子,建立一個感知器逼近函數y=3x+2y=3x+2y=3x+2
#為3x+2的函數建模 import tensorflow as tf import tensorflow.contrib.eager as tfe tf.enable_eager_execution()#啟動eager_execution NUM_EXAMPLES=1000#樣本數 training_inputs=tf.random_normal([NUM_EXAMPLES])#隨機輸入 noise=tf.random_normal([NUM_EXAMPLES])#隨機噪聲 training_outputs=training_inputs*3+2+noise#函數輸出+噪聲 def prediction(input,weight,bias):return input*weight+bias#前向計算def loss(weights,biases):error=prediction(training_inputs,weights,biases)-training_outputsreturn tf.reduce_mean(tf.square(error))#均方差損失def grad(weights,biases):with tf.GradientTape() as tape:loss_value=loss(weights,biases)return tape.gradient(loss_value,[weights,biases])#計算梯度train_steps=200 learning_rate=0.01 W=tfe.Variable(5.) B=tfe.Variable(10.) print('Initial Loss:{:.3f}'.format(loss(W,B)))for i in range(train_steps):dW,dB=grad(W,B)W.assign_sub(dW*learning_rate)#每次循環更新權重B.assign_sub(dB*learning_rate)#每次循環更新偏置if i%20==0:print('Loss at step{:03d}:{:.3f}'.format(i,loss(W,B))) print('Final loss:{:.3f}'.format(loss(W,B))) print('W={},B={}'.format(W.numpy(),B.numpy()))輸出
Initial Loss:71.285 Loss at step000:68.417 Loss at step020:30.346 Loss at step040:13.806 Loss at step060:6.604 Loss at step080:3.461 Loss at step100:2.086 Loss at step120:1.483 Loss at step140:1.218 Loss at step160:1.101 Loss at step180:1.050 Final loss:1.028 W=3.0146498680114746,B=2.108891725540161這個例子很容易看出,在eager execution中:
- 無需為輸入數據建立容器,如theano中的theano.tensor.scalar
- 無需為運算操作建立圖結構,例如theano中的function類似
- 梯度更新使用tf.GradientTape()中的gradient
感覺就是簡單的numpy操作,只不過在tensorflow中實現了,外加了一個自動梯度求解。
對于eager execution暫時了解這么多,印象最深的是:
- 無需為變量建立容器;無需為運算建立計算圖**,**
- 自動梯度的函數是gradient
后續要是用到這種方式書寫程序再做了解,畢竟深度學習框架比較重要的就是計算圖,應該沒誰會一直選擇使用這種方式編寫程序吧,雖然看起來很簡潔。
Graph Execution
先看一句話:使用 Graph Execution時,程序狀態(如變量)存儲在全局集合中,它們的生命周期由 tf.Session對象管理。相反,在 Eager Execution 期間,狀態對象的生命周期由其對應的 Python 對象的生命周期決定。接下來要介紹的大部分內容基本都屬于Graph Execution范圍內的知識。
這里使用Graph execution對上面Eager Execution演示函數y=3x+2y=3x+2y=3x+2進行建模
#生成樣本 NUM_EXAMPLES=1000 training_inputs=np.random.rand(NUM_EXAMPLES) training_outputs=training_inputs*3+2 #定義容器 tf.reset_default_graph() W=tf.get_variable(name='W',shape=[1]) b=tf.get_variable(name='b',shape=[1])input_placeholder=tf.placeholder(shape=[1,NUM_EXAMPLES],dtype=tf.float32) label_placeholder=tf.placeholder(shape=[1,NUM_EXAMPLES],dtype=tf.float32)loss=tf.reduce_mean(tf.square(input_placeholder*W+b-label_placeholder)) optimizer=tf.train.GradientDescentOptimizer(learning_rate=0.01) update_op=optimizer.minimize(loss) init=tf.initialize_all_variables() #創建計算圖 with tf.Session() as sess:sess.run(init)for i in range(5000):_,new_W,new_b=sess.run([update_op,W,b],feed_dict={input_placeholder:[training_inputs],label_placeholder:[training_outputs]}) print(new_W,new_b) #[2.999767] [2.0001235]但是我不知道為什么要這么多迭代次數才能逼近,畢竟Eager Execution僅需200次迭代就能達到很好效果,這個用了5000次才能逼近。
但是從這個例子可以看出,我們需要學習的有:容器、優化器、迭代優化等相關操作。
高階API: Estimator
暫時理解是這個Estimator封裝好了很多結構,比如分類器、回歸器,具體有哪些,戳這里,同時也封裝好了對應的訓練、評估、預測方法。
還是用函數y=3x+2y=3x+2y=3x+2演示一下Estimator中的LinearRegressor做函數逼近
#創建回歸器 NUM_EXAMPLES=1000 training_inputs=np.random.rand(NUM_EXAMPLES) training_outputs=training_inputs*3+2 column=tf.feature_column.numeric_column('x') classifier = tf.estimator.LinearRegressor(feature_columns=[column]) #訓練 train_input=tf.estimator.inputs.numpy_input_fn(\x={'x':training_inputs},y=training_outputs,shuffle=False,num_epochs=None) classifier.train(train_input,steps=2500)如何取出權重,我暫時也不是特別清楚,因為這個classifier中的參數有點多,可以通過get_variable_names函數看到分類器都有哪些參數名,然后對應取出來就行:
print(classifier.get_variable_names()) print(classifier.get_variable_value('linear/linear_model/bias_weights')) print(classifier.get_variable_value('linear/linear_model/x/weights'))輸出:
['global_step', 'linear/linear_model/bias_weights', 'linear/linear_model/bias_weights/part_0/Ftrl', 'linear/linear_model/bias_weights/part_0/Ftrl_1', 'linear/linear_model/x/weights', 'linear/linear_model/x/weights/part_0/Ftrl', 'linear/linear_model/x/weights/part_0/Ftrl_1'] [2.0108593] [[2.9801264]]初步估計這個權重和偏置的取出是這樣,至于其它參數是什么,還沒懂,以后用到再說。
自定義Estimator暫時不看,這個應該屬于進階了,后續需要再學習
總結
上面通過三種方式對函數y=3x+2y=3x+2y=3x+2建模,展示了tensorflow中構建及運行模型的方法,做一個初步了解就行,其實搞科研第二種算法用的比較多,因為它類似于Theano,只不過封裝了更好的函數操作,調試信息感覺也更加清晰。此外,我暫時不打算使用Eager execution的方法建立模型,感覺和numpy一樣,還有也不打算用Estimator封裝好的各種模型,感覺對理論理解就不夠了。
下面就針對Graph execution構建模型所需了解的入門級知識,按照學Theano的步驟來吧:首先是變量定義(標量、向量、矩陣)和操作(點乘、叉乘之類的)。
入門級操作
數據類型定義與取值
**常量:**使用tf.constant,輸入參數有
tf.constant(value,dtype=None,shape=None,name='Const',verify_shape=False )依次代表:常量值,類型,維度,名稱,是否在每次驗證大小(如果設置成False,以后還能修改大小)
tensor1=tf.constant([1,2,3,4,5,6,7])#一維常量 tensor2=tf.constant([1],shape=[2,7])#二維常量如果想取值看看,就必須在Session中取值,感覺這個是最麻煩的,取值還得去Session,不過這樣就能讓我們保持一個良好習慣,寫任何東西,把Session定義出來就行了:
with tf.Session() as sess:print(tensor1.eval())print(tensor2.eval())'''[1 2 3 4 5 6 7] [[1 1 1 1 1 1 1][1 1 1 1 1 1 1]]'''變量: 使用tf.Variable,輸入參數有
tf.Variable(<initial-value>, name=<optional-name>)包括初始值和名稱,比如定義權重和偏置:
weights=tf.Variable(np.random.rand(2,3),name='weights') bias=tf.Variable(0,name='bias')但是在使用此變量之前,一定要初始化,才能進行其它操作,比如取值,幸好TensorFlow提供了統一初始化函數initialize_all_variables,我們無需一個個初始化了:
weights=tf.Variable(np.random.rand(2,3),name='weights') bias=tf.Variable(0,name='bias') init_op=tf.initialize_all_variables() with tf.Session() as sess:sess.run(init_op)print(weights.eval()) ''' [[0.6229396 0.42244541 0.47293289] [0.70087716 0.42179686 0.09062685]] '''如果想初始化某個變量,可使用內置函數initializer。
內置簡單的賦值和加減操作:主要是Variable的內置函數assign、assign_add、assign_sub
切記一定要在Session中進行操作:
with tf.Session() as sess:sess.run(init_op)sess.run(bias.assign(bias+10))print(bias.eval()) #10占位器:用于存儲變量的placeholder,與Variable的區別在于,占位器無需預先定義數值,它僅僅是一個容器,類似于theano中的tensor.vector之類的,可指定維度,后續可以用feed_dict給它喂數據:
tf.placeholder(dtype,shape=None,name=None )舉個例子:
x = tf.placeholder(tf.float32, shape=(1024, 1024)) y = tf.matmul(x, x)with tf.Session() as sess:print(sess.run(y)) # ERROR: will fail because x was not fed.rand_array = np.random.rand(1024, 1024)print(sess.run(y, feed_dict={x: rand_array})) # Will succeed.再一次說明定義的時候無需指定數值,只需指定大小和類型,那我們設計神經網絡的時候可以常用它來為輸入數據占位。
基本運算
-
abs:如果是實數,就直接取絕對值;如果是復數,就取實部和虛部的平方和的根a2+b2\sqrt{a^2+b^2}a2+b2?
x1=tf.constant([-1,2,3,-4,5]) y1=tf.abs(x1) x2=tf.constant([[-3+4j],[-6+8j]]) y2=tf.abs(x2) with tf.Session() as sess:print(y1.eval())#[1 2 3 4 5]print(y2.eval())'''[[ 5.][10.]]''' -
add:加和操作
x3=tf.constant(2) y3=tf.add(x3,4) with tf.Session() as sess:print(y3.eval())#6 -
angle:角度值,輸入被看成復數a+bja+bja+bj,輸出是atan2(b,a)atan2(b,a)atan2(b,a),如果輸入是實數,輸出就是0
x4=tf.constant([[-2.25+4.75j],[3.25+5.75j]]) y4=tf.angle(x4) with tf.Session() as sess:print(y4.eval())'''[[2.01317055][1.05634501]]''' -
argmax,argmin:返回最大/最小值的位置索引
indx=tf.argmax(x1) with tf.Session() as sess:print(x1.eval())print(indx.eval())'''[-1 2 3 -4 5]4''' -
assign:賦值
-
assign_add:加
-
assign_sub:減
-
sin:正弦,還有對應的asin
-
sinh:雙曲正弦,還有對應的asinh
-
cos:余弦,還有對應的acos
-
cosh:雙曲余弦,還有對應的acosh
-
tan:正切
-
tanh:雙曲正切
-
atan:對每個元素計算反正切
-
atan2:輸入是atan2(y,x),計算y/xy/xy/x的反正切,范圍[?π,π][-\pi,\pi][?π,π]
-
complex:將兩個實數變換成復數
real = tf.constant([2.25, 3.25]) imag = tf.constant([4.75, 5.75]) tf.complex(real, imag) # [[2.25 + 4.75j], [3.25 + 5.75j]] -
real:返回復數的實部
-
imag:返回復數的虛部
-
cross:計算兩個相同維度向量的叉乘
x5=[1,2,3] y5=[4,5,6] temp5=tf.cross(x5,y5) with tf.Session() as sess:print(temp5.eval())#[-3 6 -3] print(np.cross(x5,y5))#[-3 6 -3] -
matmul:點乘
a=tf.constant([1,2,3,4,5,6],shape=[2,3]) b=tf.constant([7,8,9,10,11,12],shape=[3,2]) c=tf.matmul(a,b) with tf.Session() as sess:sess.run(c)print(c.eval())'''[[ 58 64][139 154]]''' -
multiply:逐元素相乘
a=tf.constant([1,2,3,4,5,6],shape=[2,3]) b=tf.constant([7,8,9,10,11,12],shape=[2,3]) c=tf.multiply(a,b) with tf.Session() as sess:sess.run(c)print(c.eval()) ''' [[ 7 16 27][40 55 72]] ''' -
subtract:逐元素相減x?yx-yx?y
-
cumprod:累乘
tf.cumprod(x,axis=0,exclusive=False,reverse=False,name=None )有兩個比較重要參數:exclusive和reverse
tf.cumprod([a, b, c]) # [a, a * b, a * b * c] tf.cumprod([a, b, c], exclusive=True) # [1, a, a * b] tf.cumprod([a, b, c], reverse=True) # [a * b * c, b * c, c] tf.cumprod([a, b, c], exclusive=True, reverse=True) # [b * c, c, 1] -
cumsum:累加
tf.cumsum([a, b, c]) # [a, a + b, a + b + c] tf.cumsum([a, b, c], exclusive=True) # [0, a, a + b] tf.cumsum([a, b, c], reverse=True) # [a + b + c, b + c, c] tf.cumsum([a, b, c], exclusive=True, reverse=True) # [b + c, c, 0] -
div:逐元素除法
-
equal:對應元素是否相等
-
exp:返回y=exy=e^xy=ex
-
fft,fft2d,fft3d:快速傅里葉變換
-
ifft,ifft2d,ifft3d:逆傅里葉變換
-
ceil:不小于xxx的最大整數
-
floor:小于xxx的最大整數
-
floordiv:除法,整數的時候等價于tf.div(x,y),浮點的時候等價于tf.floor(tf.div(x,y))
-
floormod:返回余數x=floor(x/y)?y+mod(x,y)x=floor(x/y)*y+mod(x,y)x=floor(x/y)?y+mod(x,y)
-
greater:如果x>yx>yx>y返回真值
-
greater_equal:如果x>=yx>=yx>=y,返回真值
-
less:如果x<yx<yx<y,返回真值
-
less_equal:如果x<=yx<=yx<=y,返回真值
-
log:返回y=log?exy=\log_exy=loge?x
-
log1p:返回y=log?e(1+x)y=\log_e(1+x)y=loge?(1+x)
-
log_sigmoid:返回y=log?(11+e?x)y=\log (\frac{1}{1+e^{-x}})y=log(1+e?x1?)
-
matrix_inverse:計算方陣的逆或其共軛轉置
tf.matrix_inverse(input,adjoint=False,name=None ) -
maximum:逐元素返回x>y?x:yx>y?x:yx>y?x:y
tf.maximum(x,y,name=None ) -
minimum:逐元素返回x<y?x:yx<y?x:yx<y?x:y
-
negative:逐元素計算負值y=?xy=-xy=?x
-
ones:初始化全1矩陣
tf.ones(shape,dtype=tf.float32,name=None ) #example tf.ones([2, 3], tf.int32) # [[1, 1, 1], [1, 1, 1]] -
zeros:初始化全0矩陣
-
ones_like:初始化與給定矩陣維度相同的全1矩陣
tf.ones_like(tensor,dtype=None,name=None,optimize=True ) #example tensor = tf.constant([[1, 2, 3], [4, 5, 6]]) tf.ones_like(tensor) # [[1, 1, 1], [1, 1, 1]] -
zeros_like:初始化與給定矩陣維度相同的全0矩陣
-
pad填充:
tf.pad(tensor,paddings,mode='CONSTANT',name=None,constant_values=0 )按照paddings去填充tensor,官網解釋有點復雜,感覺paddings是一個二維矩陣,分別指示tensor的上下左右分別要填充多少。mode是填充模式,是常量填充CONSTANT、鏡像填充REFLECT或是對稱填充SYMMETRIC
t = tf.constant([[1, 2, 3], [4, 5, 6]]) paddings = tf.constant([[1, 1,], [2, 2]]) # 'constant_values' is 0. # rank of 't' is 2. tf.pad(t, paddings, "CONSTANT") # [[0, 0, 0, 0, 0, 0, 0],# [0, 0, 1, 2, 3, 0, 0],# [0, 0, 4, 5, 6, 0, 0],# [0, 0, 0, 0, 0, 0, 0]]tf.pad(t, paddings, "REFLECT") # [[6, 5, 4, 5, 6, 5, 4],# [3, 2, 1, 2, 3, 2, 1],# [6, 5, 4, 5, 6, 5, 4],# [3, 2, 1, 2, 3, 2, 1]]tf.pad(t, paddings, "SYMMETRIC") # [[2, 1, 1, 2, 3, 3, 2],# [2, 1, 1, 2, 3, 3, 2],# [5, 4, 4, 5, 6, 6, 5],# [5, 4, 4, 5, 6, 6, 5]] -
pow:逐元素計算xyx^yxy
x = tf.constant([[2, 2], [3, 3]]) y = tf.constant([[8, 16], [2, 3]]) tf.pow(x, y) # [[256, 65536], [9, 27]] -
range:創建序列
tf.range(limit, delta=1, dtype=None, name='range') tf.range(start, limit, delta=1, dtype=None, name='range')其中start是表示開始數字,limit是最后一個數字的最終取值范圍,delta是增量
start = 3 limit = 18 delta = 3 tf.range(start, limit, delta) # [3, 6, 9, 12, 15]start = 3 limit = 1 delta = -0.5 tf.range(start, limit, delta) # [3, 2.5, 2, 1.5]limit = 5 tf.range(limit) # [0, 1, 2, 3, 4] -
reduce_join:合并某些維度
tf.reduce_join(inputs,axis=None,keep_dims=False,separator='',name=None,reduction_indices=None ) # tensor `a` is [["a", "b"], ["c", "d"]] tf.reduce_join(a, 0) ==> ["ac", "bd"] tf.reduce_join(a, 1) ==> ["ab", "cd"] tf.reduce_join(a, -2) = tf.reduce_join(a, 0) ==> ["ac", "bd"] tf.reduce_join(a, -1) = tf.reduce_join(a, 1) ==> ["ab", "cd"] tf.reduce_join(a, 0, keep_dims=True) ==> [["ac", "bd"]] tf.reduce_join(a, 1, keep_dims=True) ==> [["ab"], ["cd"]] tf.reduce_join(a, 0, separator=".") ==> ["a.c", "b.d"] tf.reduce_join(a, [0, 1]) ==> "acbd" tf.reduce_join(a, [1, 0]) ==> "abcd" tf.reduce_join(a, []) ==> [["a", "b"], ["c", "d"]] tf.reduce_join(a) = tf.reduce_join(a, [1, 0]) ==> "abcd" -
reduce_max:按照指定維度計算最大值
-
reduce_min:按照指定維度計算最小值
-
reduce_prob:按照指定維度計算累加
-
reduce_sum:按照指定維度計算累乘
a=tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32) b=tf.reduce_prod(a,0) c=tf.reduce_prod(a,1) with tf.Session() as sess:sess.run(c)print(a.eval())print(b.eval())#[ 4. 10. 18.]print(c.eval())#[ 6. 120.] -
reduce_mean:按照指定維度計算均值
a=tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32) b=tf.reduce_mean(a,0) c=tf.reduce_mean(a,1) with tf.Session() as sess:sess.run(c)print(a.eval())print(b.eval())#[2.5 3.5 4.5]print(c.eval())#[2. 5.] -
reverse:按照指定維度翻轉矩陣
# tensor 't' is [[[[ 0, 1, 2, 3], # [ 4, 5, 6, 7], # [ 8, 9, 10, 11]], # [[12, 13, 14, 15], # [16, 17, 18, 19], # [20, 21, 22, 23]]]] # tensor 't' shape is [1, 2, 3, 4]# 'dims' is [3] or 'dims' is [-1] reverse(t, dims) ==> [[[[ 3, 2, 1, 0],[ 7, 6, 5, 4],[ 11, 10, 9, 8]],[[15, 14, 13, 12],[19, 18, 17, 16],[23, 22, 21, 20]]]]# 'dims' is '[1]' (or 'dims' is '[-3]') reverse(t, dims) ==> [[[[12, 13, 14, 15],[16, 17, 18, 19],[20, 21, 22, 23][[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]]]]# 'dims' is '[2]' (or 'dims' is '[-2]') reverse(t, dims) ==> [[[[8, 9, 10, 11],[4, 5, 6, 7],[0, 1, 2, 3]][[20, 21, 22, 23],[16, 17, 18, 19],[12, 13, 14, 15]]]] -
rint:返回與當前元素最接近的整數值,有一半的時候,取最近偶數
x = tf.constant([0.9, 2.5, 2.3, 1.3, -4.5]) y=tf.round(x) with tf.Session() as sess:sess.run(y)print(y.eval())#[ 1. 2. 2. 1. -4.] -
rsqrt:逐元素y=1xy=\frac{1}{\sqrt{x}}y=x?1?
-
sqrt:逐元素y=xy=\sqrt{x}y=x?
-
square:逐元素y=x2y=x^2y=x2
-
squared_difference:逐元素(x?y)2(x-y)^2(x?y)2
-
sign:返回元素符號,大于0返回1,等于0或者非數返回0,小于0返回-1
-
squeeze:默認去除是維度為1的維度,也可指定
# 't' is a tensor of shape [1, 2, 1, 3, 1, 1] tf.shape(tf.squeeze(t)) # [2, 3] # 't' is a tensor of shape [1, 2, 1, 3, 1, 1] tf.shape(tf.squeeze(t, [2, 4])) # [1, 2, 3, 1] -
stack:堆疊向量或者矩陣
x = tf.constant([1, 4]) y = tf.constant([2, 5]) z = tf.constant([3, 6]) tf.stack([x, y, z]) # [[1, 4], [2, 5], [3, 6]] tf.stack([x, y, z], axis=1) # [[1, 2, 3], [4, 5, 6]] -
svd:奇異值分解
# a is a tensor. # s is a tensor of singular values. # u is a tensor of left singular vectors. # v is a tensor of right singular vectors. s, u, v = svd(a) s = svd(a, compute_uv=False)#還可以返回u,v -
unique:去重
# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8] y, idx = unique(x) y ==> [1, 2, 4, 7, 8] idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]
##條件函數:Assert
tf.Assert(condition,data,summarize=None,name=None )如果條件不成立,就從data中打印張量列表,summerize決定了打印多少個條目。
注意應使用此函數的輸出,如果條件不滿足, 則會記錄一個InvalidArgumentError警告。若要將輸出標記為已用, 請調用其. *mark_used()*方法。
但是如果在圖模式中,為了保證Assert能夠執行,需要添加一個依賴項:
a=tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32) b=tf.constant([0.1,0.2,0.3,0.4,0.5,0.6],shape=[2,3],dtype=tf.float32) assert_op=tf.Assert(tf.less(tf.reduce_max(b),1.),[x]) with tf.control_dependencies([assert_op]):output = tf.reduce_sum(a) with tf.Session() as sess:print(output.eval())#21.0但是如果把上述的less換成greater就會報錯:
a=tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32) b=tf.constant([0.1,0.2,0.3,0.4,0.5,0.6],shape=[2,3],dtype=tf.float32) assert_op=tf.Assert(tf.greater(tf.reduce_max(b),1.),[x]) with tf.control_dependencies([assert_op]):output = tf.reduce_sum(a) with tf.Session() as sess:print(output.eval())#報錯InvalidArgumentError當然這個assert_op也不是必須自己寫,TensorFlow自身提供了一堆:
-
assert_equal:判斷相等
-
assert_greater:判斷大于x>yx>yx>y
-
assert_greater_equal:判斷不小于x>=yx>=yx>=y
-
assert_integer:判斷是否為整數
-
assert_same_float_dtype:判斷是否為相同的浮點型
-
assert_scalar:判斷是否為標量
-
assert_type:判斷與給定類型是否相同
-
assert_less:判斷是否小于x<yx<yx<y
-
assert_less_equal:判斷是否不大于x<=yx<=yx<=y
-
assert_negative:判斷是否為負,x<0x<0x<0
-
assert_none_equal:判斷是否不等于x≠yx\neq yx??=y
-
assert_non_negative:判斷是否非負x>=0x>=0x>=0
-
assert_non_positive:判斷是否為非正數x<=0x<=0x<=0
-
assert_positive:判斷是否為正數x>0x>0x>0
-
assert_proper_iterable:判斷是否可迭代,張量、ndarray、byte / text類型都是 iterables 本身,所以是有用的
-
assert_rank:判斷xxx的秩是否等于給定值
assert_rank (x ,rank ,data = None ,summarize = None ,message = None ,name = None) #例子 with tf.control_dependencies ([ tf.assert_rank(X2)]):output = tf.reduce_sum (x) -
assert_rank_at_least:判斷xxx與給定秩是否相等或者更大
-
assert_variables_initialized:判斷是否已初始化
隨便舉個例子,判斷是否大于:
a=tf.constant([1,2,3,4,5,6],shape=[2,3],dtype=tf.float32) b=tf.constant([0.1,0.2,0.3,0.4,0.5,0.6],shape=[2,3],dtype=tf.float32) with tf.control_dependencies([tf.assert_greater(a,b)]):output = tf.reduce_sum(a) with tf.Session() as sess:print(output.eval())#21.0##隨機操作
random_crop:隨機裁剪
tf.random_crop(value,size,seed=None,name=None )將value隨機裁剪到指定size,比如裁剪圖像的時候用到:
distorted_image = tf.random_crop(image,[height,width,3])一個使用例子可以戳這里
#隨機裁剪 import matplotlib.image as img import matplotlib.pyplot as pltimage=img.imread('F:/Photo/2.jpg') reshaped_image=tf.cast(image,dtype=tf.float32)#轉換數據類型 with tf.Session() as sess:size=tf.cast(tf.shape(reshaped_image).eval(),tf.int32)height=sess.run(size[0]//2)width=sess.run(size[1]//2) distored_image=tf.random_crop(reshaped_image,[height,width,3]) with tf.Session() as sess:#plt.imshow(sess.run(tf.cast(reshaped_image,tf.uint8)))plt.imshow(sess.run(tf.cast(distored_image,tf.uint8)))random_shuffle:隨機打亂次序
tf.random_shuffle(value,seed=None,name=None )實例:
a=tf.constant([[1,2],[3,4],[5,6]]) b=tf.random_shuffle(a) with tf.Session() as sess:sess.run(b)print(b.eval()) ''' [[3 4][1 2][5 6]] '''random_gamma:伽馬分布,輸入形狀參數和逆尺度(倒數?)參數
random_normal:正態分布,輸入大小,均值,方差
random_poisson:泊松分布,輸入λ\lambdaλ和形狀
random_uniform:平均分布,輸入形狀,最小值,最大值
##單熱度編碼:one_hot
這個通常用來制作標簽,對應位置為指定值,一般用0、10、10、1編碼
tf.one_hot(indices,depth,on_value=None,off_value=None,axis=None,dtype=None,name=None )其中:
- indices:是真實數值標簽
- depth:長度,一般代表總類數
- on_value:在編碼中激活該種類時,應該放什么值
- off_value:未激活的是什么值
函數執行方案:Session
正如之前所有的代碼演示一樣,在Graph execution中幾乎所有的運算都必須放到Session中通過run函數才能執行,這應該是tensorflow的約定,這里我們看看兩種Session
標準的tf.Session
這個就不用說了,典型的調用方法:
a=tf.constant([1,2]) b=tf.add(a,2); with tf.Session() as sess:sess.run(b)print(b.eval())#[3 4]或者
import tensorflow as tf a=tf.constant([1,2]) b=tf.add(a,2); sess=tf.Session() print(sess.run(b))交互式的InteractiveSession
前面那個標準的Session每次都要用那個典型的調用方法,感覺很冗余,想辦法把當前環境的Session預先初始化好,后面用的時候直接運行或者調用run就好了,這就是InteractiveSession的用途。
舉個例子:
#程序1 import tensorflow as tf a=tf.constant([1,2]) b=tf.add(a,2); isess=tf.Session() print(b.eval())此程序是無法運行的,報錯
ValueError: Cannot evaluate tensor using `eval()`: No default session is registered. Use `with sess.as_default()` or pass an explicit session to `eval(session=sess)`但是如果把Session換成InteractiveSession()就能執行了:
#程序2 import tensorflow as tf a=tf.constant([1,2]) b=tf.add(a,2); isess=tf.InteractiveSession() print(b.eval())#[3 4]記住,只要執行過InteracitveSession(),那么當前環境的所有操作都無需添加Session了,比如你先運行了程序2,再去運行程序1,是可以執行的,因為交互式環境已被激活。具體理解可以戳這里
后記
作為入門先了解這么多吧, 多了的更復雜了,消化不了,通過實戰了解更多知識比較好。
在本博客中,主要了解了:
- TensorFlow的數據類型、基本運算
- 函數執行方案
本文代碼鏈接:https://pan.baidu.com/s/1NC0QPG6KxSsNq5ndthbdhg 密碼:suxw
總結
以上是生活随笔為你收集整理的【TensorFlow-windows】学习笔记一——基础理解的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第五人格冒险家皮肤大全 冒险家哪个皮肤好
- 下一篇: 日本存在就是为了惊吓我们 岛国最奇葩的手