張量tensor:多維數組(列表) 階:張量的維數
維數? ? ? ? 階? ? ? ? 名字? ? ? ? ? ? ? ? ? ? ? ? 例子
0-D? ? ? ? ? 0? ? ? ? ?標量 scalar? ? ? ? ? ? ?s = 1, 2, 3
1-D? ? ? ? ? 1? ? ? ? ?向量 vector? ? ? ? ? ? ?v = [1, 2, 3]
2-D? ? ? ? ? 2? ? ? ? ?矩陣 matrix? ? ? ? ? ? ?m = [[1,2,3],[4,5,6]]
n-D? ? ? ? ? n? ? ? ? ?張量?tensor? ? ? ? ? ? ?t = [[[]]]
tensorflow 數據類型
tf.int32, tf.float32, tf.float64
tf.bool tf.constant([True, False])
tf.string tf.constant("hello world")
創建張量 tf.constant(張量內容, dtype=數據類型(可選))
import tensorflow as tf
a = tf.constant([1,5], dtype=tf.int64)
print(a)
print(a.dtype)
print(a.shape)
1.將numpy的數據類型轉化為Tensor數據類型
tf.convert_to_tensor(數據名,dtype=數據類型(可選))
import tensorflow as tf
import numpy as np
a = np.arange(0,5)
b = tf.convert_to_tensor(a, dtype=tf.int64)
print(a)
print(b)
2.tf,zeros(維度) 全為0 tf.ones(維度)全為1 tf.fill(維度,指定值—)
a = tf.zero([2, 3])
b = tf.ones(4)
c = tf.fill([2, 2], 9)
print(a)
print(b)
print(c)
3.生成符合正態分布的隨機數,默認值為0,標準差為1
tf.random.normal(維度,mean=均值, stddev= 標準差)
4.生成截斷式正態分布的隨機數,集中一些,保證在范圍內
tf.random.truncated_normal(維度,mean=均值, stddev=標準差)
5.生成均勻分布的隨機數
tf.random.uniform(維度,minval=最小值, maxval = 最大值)[min, max)
6.強制類型轉換
tf.cast(張量名, dtype=數據類型)
7.計算張量維度上的元素的最小值
tf.reduce_min(張量名)
8.計算張量維度上元素的最大值
tf.reduce_max(張量名)?
9.axis=0,對縱向操作, axis = 1 橫向操作
10. tf,Variable()將變量標記為可訓練的,被標記的變量會在反向傳播中記錄梯度信息。
11.四則運算, math等,維度相同
12.特征和標簽配對的函數,切分傳入張量的第一個維度,生成輸入特征/標簽對,構建數據集
data = tf.data.Dataset.from_tensor_slices((輸入特征,標簽))
?13.求導函數
with結構記錄計算過程,GradientTape求出張量的梯度
?14.enumerate,遍歷序列,并在元素前配置索引號,內置函數
15.tf.one_hot 獨熱編碼:在分類問題中,常用獨熱碼做標簽,標記類別:1表示是,0表示非
tf.one_hot(待轉化的數據,depth=幾分類)
classes = 3
label = tf.constant([1,0,2])
output = tf.one_hot(labels, depth=classes)
print(output)
16.當神經網絡完成前向傳播時,計算出概率,當概率符合概率分布時,才能作為概率輸出tf.nn.softmax
當n分類的n個輸出通過tf.nn.softmax,便符合概率分布,即可輸出,其總和滿足1
任意的x P(X = x)∈[0,1],?????????
y = tf.constant([1.01, 2.01, -0.66])
y_pro = tf.nn.softmax(y)
print(y_pro)
17.assign_sub,賦值操作,更新參數的值并返回
在調用assign_sub前,先用tf.Variable 定義變量w為可訓練(可自更新)
w.assgin_sub(w要自減的內容)
?18.tf.argmax 返回0/1方向上最大值的索引號
tf.argmax(張量名,axis=操作軸)