1.2經常使用函數數組
本節目標:掌握在創建和操做神經網絡過程當中經常使用的函數網絡
# 經常使用函數 import tensorflow as tf import numpy as np # 強制Tensor的數據類型轉換 x1 = tf.constant([1,2,3],dtype = tf.float64) print(x1) x2 = tf.cast(x1,tf.int32) print(x2) # 計算張量中最小的元素 print(tf.reduce_min(x2)) # 計算張量中最大的元素 print(tf.reduce_max(x2))
輸出結果:
tf.Tensor([1. 2. 3.], shape=(3,), dtype=float64) tf.Tensor([1 2 3], shape=(3,), dtype=int32) tf.Tensor(1, shape=(), dtype=int32) tf.Tensor(3, shape=(), dtype=int32)
# 理解axis,在一個二位張量或者數組中,能夠經過調整axis等於0或者1控制執行維度 # axis=0表明跨行(經度,down),而axis=1跨列(緯度,across) # 不指定axis,則全部元素參與計算 x = tf.constant([[1,2,3], [4,5,6]]) print(x) print(tf.reduce_mean(x)) # 求平均[2,5],平均爲3 print(tf.reduce_sum(x,axis = 1)) # 求總和按行操做
輸出結果:
tf.Tensor( [[1 2 3] [4 5 6]], shape=(2, 3), dtype=int32) tf.Tensor(3, shape=(), dtype=int32) tf.Tensor([ 6 15], shape=(2,), dtype=int32)
# tf.Variable()將變量標記爲可訓練,被標記的變量會在反向傳播中被記錄梯度信息 w = tf.Variable(tf.random.normal([2,2],mean= 0,stddev =1)) print(w)
輸出結果:
<tf.Variable 'Variable:0' shape=(2, 2) dtype=float32, numpy= array([[ 0.47072804, -0.7259878 ], [-1.6562318 , 0.15564619]], dtype=float32)>
# 經常使用的運算函數 # 加法 a = tf.constant([1,2,3],dtype = tf.float32) b = tf.constant([4,5,6],dtype = tf.float32) print(tf.add(a,b)) # 減法 print(tf.subtract(a,b)) # 乘法 print(tf.multiply(a,b)) # 除法 print(tf.divide(b,a)) # 平方 print(tf.square(a)) # 次方 print(tf.pow(a,3)) # 開放 print(tf.sqrt(a)) # 矩陣乘法 c = tf.ones([3,2]) d = tf.fill([2,3],6.) print(tf.matmul(c,d))
輸出結果:
tf.Tensor([5. 7. 9.], shape=(3,), dtype=float32) tf.Tensor([-3. -3. -3.], shape=(3,), dtype=float32) tf.Tensor([ 4. 10. 18.], shape=(3,), dtype=float32) tf.Tensor([4. 2.5 2. ], shape=(3,), dtype=float32) tf.Tensor([1. 4. 9.], shape=(3,), dtype=float32) tf.Tensor([ 1. 8. 27.], shape=(3,), dtype=float32) tf.Tensor([1. 1.4142135 1.7320508], shape=(3,), dtype=float32) tf.Tensor( [[12. 12. 12.] [12. 12. 12.] [12. 12. 12.]], shape=(3, 3), dtype=float32)
# 切分傳入張量的第一維度,生成輸入特徵/標籤配對,構成數據集 features = tf.constant([12,23,10,17]) labels = tf.constant([0,1,1,0]) # 對特徵和標籤進行一一配對 dataset = tf.data.Dataset.from_tensor_slices((features,labels)) print(dataset) for element in dataset: print(element)
輸出結果:
<TensorSliceDataset shapes: ((), ()), types: (tf.int32, tf.int32)> (<tf.Tensor: id=286, shape=(), dtype=int32, numpy=12>, <tf.Tensor: id=287, shape=(), dtype=int32, numpy=0>) (<tf.Tensor: id=288, shape=(), dtype=int32, numpy=23>, <tf.Tensor: id=289, shape=(), dtype=int32, numpy=1>) (<tf.Tensor: id=290, shape=(), dtype=int32, numpy=10>, <tf.Tensor: id=291, shape=(), dtype=int32, numpy=1>) (<tf.Tensor: id=292, shape=(), dtype=int32, numpy=17>, <tf.Tensor: id=293, shape=(), dtype=int32, numpy=0>)
# 求導數運算 with tf.GradientTape() as tape: w= tf.Variable(tf.constant(3.0)) loss = tf.pow(w,2) # 對w2求w的倒數 grad = tape.gradient(loss,w) print(grad)
輸出結果:
tf.Tensor(6.0, shape=(), dtype=float32)
# 求導數運算 with tf.GradientTape() as tape: w= tf.Variable(tf.constant(3.0)) loss = tf.pow(w,2) # 對w2求w的倒數 grad = tape.gradient(loss,w) print(grad)
輸出結果:
0 one 1 two 2 three
# 獨熱編碼:將張量中的每一個元素按照規律獨立編碼,編碼中0爲否1爲是 labels = tf.constant([0,1,2,3]) classes = 4 output = tf.one_hot(labels,depth = classes) print(output)
輸出結果:
tf.Tensor( [[1. 0. 0. 0.] [0. 1. 0. 0.] [0. 0. 1. 0.] [0. 0. 0. 1.]], shape=(4, 4), dtype=float32)
# 用softmax函數使得輸出符合機率分佈,將輸出用e爲底y爲指數,求出每一個輸出的機率 # 對機率進行歸一化操做 y = tf.constant([1.01,2.01,-0.66]) y_pro = tf.nn.softmax(y) print("After softmax,y_pro is:",y_pro)
輸出結果:
After softmax,y_pro is: tf.Tensor([0.25598174 0.69583046 0.0481878 ], shape=(3,), dtype=float32)
# 用assign_sub函數對參數進行自更新,賦值操做(更新參數爲可訓練) w = tf.Variable(4) # 對w進行自減一操做w = w -1 w.assign_sub(1) print(w)
輸出結果:
<tf.Variable 'Variable:0' shape=() dtype=int32, numpy=3>
# 返回張量沿指定維度最大值的索引 test = np.array([[1,2,3], [4,5,6], [7,8,9], [10,11,12]]) print(test) print(tf.argmax(test,axis = 0)) print(tf.argmax(test,axis = 1)) # 判斷兩個數是否相等,bool類型 correct = tf.equal(1,1) print(correct)
輸出結果:
[[ 1 2 3] [ 4 5 6] [ 7 8 9] [10 11 12]] tf.Tensor([3 3 3], shape=(3,), dtype=int64) tf.Tensor([2 2 2 2], shape=(4,), dtype=int64) tf.Tensor(True, shape=(), dtype=bool)
本節對各個函數運用,對神經網絡搭建和操做十分重要,請你們務必掌握。