机器学习(一)——熟悉tensorflow2.0
生活随笔
收集整理的這篇文章主要介紹了
机器学习(一)——熟悉tensorflow2.0
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
張量的生成
import tensorflow as tf import numpy as np ###創建一個張量, 指定類型,判斷是幾維張量,直接數[的個數 # a = tf.constant([1, 5], dtype= tf.int64) # print(a) # print(a.dtype) # print(a.shape)###將numpy數據轉換為tensor # a = np.arange(0, 5) # b = tf.convert_to_tensor(a, dtype=tf.int64) # print(a) # print(b)#創建全為0的張量 # a = tf.zeros([2, 3])#指定維度 # #創建全為1的張量 # b = tf.ones(4)#只有一個數則表示1維 # #創建全為指定值的張量 # c = tf.fill([2, 3], 66)##指定維度與值 # print(a) # print(b) # print(c)###生成正態分布的隨機數,默認均值為0, 標準差為1 # d = tf.random.normal([2, 3], mean = 0.5, stddev = 1)##指定維度,均值,標準差 # ###生成截斷式正態分布的隨機數 # e = tf.random.truncated_normal([2, 3], mean = 0.5, stddev = 1) # ##該函數保證生成隨機數在均值正負2倍標準差之間,更集中 # print(d) # print(e)###生成均勻分布隨機數 f = tf.random.uniform([2,2], minval = 0, maxval = 1)#指定維度,最小值,最大值 print(f)常用函數1.
import tensorflow as tf import numpy as np # # a = tf.constant([1.0,2.0,3.0], dtype=tf.float16) # print(a) # ##強制tensor轉換數據類型 # b = tf.cast(a, tf.int64) # print(b) # #計算該張量維度上的最小值 # print(tf.reduce_min(b)) # #同上計算最大值 # print(tf.reduce_max(b))# a = tf.ones([3,2]) # b = tf.fill([2,3], 4.) # print(tf.matmul(b, a))#矩陣乘法# x = tf.constant([[1,2,3], # [4,2,3]]) # print(x) # ##求平均值 # print(tf.reduce_mean(x)) # ## # print(tf.reduce_mean(x, axis=1))#指定維度求平均值,1表示按行求 # # ###tf.Variable 將變量標記為可訓練,被標記的函數會在反向傳播中記錄梯度信息。神經網絡 # #訓練中,常用該函數標記待訓練參數 # w = tf.Variable(tf.random.normal([2,2], mean=0, stddev=1))# a = tf.ones([1,3]) # b = tf.fill([1,3], 3.) # print(a) # print(b) # print(tf.add(a,b))#加法 # print(tf.subtract(a,b))#減法 # print(tf.multiply(a,b))#乘法 # print(tf.divide(b,a))#除法 tf.matmul是矩陣乘法# a = tf.fill([1,2], 3.) # print(a) # print(tf.pow(a, 3))#計算3次方 # print(tf.square(a))#計算平方 # print(tf.sqrt(a))#開方###data.Dataset.from_tensor_slices的作用是把特征和標簽配對,對numpy格式也適用 # features = tf.constant([12,23,15,16]) # lables = tf.constant([0,1,1,0]) # dataset = tf.data.Dataset.from_tensor_slices((features, lables)) # print(dataset) # for item in dataset: # print(item)###tf.GradientTape()能夠對 loss = tf.pow(w, 2)的參數求導 # with tf.GradientTape() as tape: # w = tf.Variable(tf.constant(3.0))#tf.Variabble將w標記為可訓練 # loss = tf.pow(w, 2) # grad = tape.gradient(loss, w) # print(grad)###enumerate枚舉出每個元素并配上索引號 # seq = ['one', 'two', 'three'] # for i, element in enumerate(seq): # print(i, element)###tf.one_hot將數據轉換為獨熱碼 # classes = 3 # lables = tf.constant([1, 0, 2]) # output = tf.one_hot(lables, depth=classes)#參數分別是帶轉換數據與幾分類 # print(output)###tf.nn.softmax函數能夠使得張量里的數據符合概率分布,方便后續操作 # y = tf.constant([1.22, 3.04, -0.77])##假設只是標簽所得的分數,不符合正態分布,用不了 # y_pro = tf.nn.softmax(y) # print(y_pro)###assign_sub實現自更新參數 # w = tf.Variable(100)##必須先標記為可更新才行 # w.assign_sub(1)#自減1 # print(w)###np.argmax 根據axis返回 縱向與橫向的最大值參數索引 # test = np.array([[1,2,3],[4,5,6],[7,8,9]]) # print(test) # print(np.argmax(test, axis=0)) # print(np.argmax(test, axis=1))常用函數2.
import tensorflow as tf import numpy as np # a = tf.constant([1,3,2,2,5]) # b = tf.constant([0,4,1,1,6]) # c = tf.where(tf.greater(a,b), a, b)#tf.where 若a > b則返回a, 否則返回b # print("c:", c)# rdm = np.random.RandomState(seed=1)#seed等于常數是使得每次產生的隨機數相同 # a = rdm.rand()##不給參數,默認生成一個0到1之間的隨機數 # b = rdm.rand(3,4)##生成一個二維的 # print(a) # print(b)# a = np.array([1,2,3]) # b = np.array([4,5,6]) # c = np.vstack((a,b))#縱向疊加 # d = np.hstack((a,b))#橫向疊加 # print(c) # print(d)x,y = np.mgrid[1:3:1, 2:4:0.5]#1是起始, 到3不包含3,步長為1 后面的一樣,返回一個二維的,列要保持一致 ###.ravel()將其拉直為一維數組 c_將兩個數組配成對輸出 grid = np.c_[x.ravel(), y.ravel()] print("x:", x) print("y:", y) print("grid:\n", grid)總結
以上是生活随笔為你收集整理的机器学习(一)——熟悉tensorflow2.0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++(四)——类和对象(下)
- 下一篇: 机器学习(二)——鸢尾花案例