tensorflow2 0學習筆記3 常用函式

2021-10-07 05:19:33 字數 4773 閱讀 1085

筆記基於北大的tensorflow2.0教程,將課程的一些重要內容記下來,方便於學習。

一、常用函式

強制資料型別轉換——tf.cast(張量名,dtype=資料型別)

找張量最小值——tf.reduce_min(張量名)          

找張量最大值——tf.reduce_max(張量名)     

二維張量中,可以通過調整axis控制執行維度;axis=1表示橫向操作;axis=0表示縱向操作 。

計算張量指定維度的平均值——tf.reduce_mean(張量名,axis=操作軸),如果不指定axis,則對所有元素進行操作

計算張量指定維度的和——tf.reduce_sum(張量名,axis=操作軸),如果不指定axis,則對所有元素進行操作

將變數標記為可訓練——tf.variable(初始值);被標記的變數會在反向傳播中記錄梯度資訊。

二、常用計算函式

四則運算:tf.add(張量1,張量2),tf.subtract(張量1,張量2),tf.multiply(張量1,張量2),tf.divide(張量1,張量2);維度相同,才可以進行四則運算

示例:

import tensorflow as tf

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(a,b))

tf.tensor([[1. 1. 1.]], shape=(1, 3), dtype=float32)

tf.tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)

tf.tensor([[4. 4. 4.]], shape=(1, 3), dtype=float32)

tf.tensor([[-2. -2. -2.]], shape=(1, 3), dtype=float32)

tf.tensor([[3. 3. 3.]], shape=(1, 3), dtype=float32)

tf.tensor([[0.33333334 0.33333334 0.33333334]], shape=(1, 3), dtype=float32)

平方、次方與開方:tf.square(張量名),tf.pow(張量名,n次方),tf.sqrt(張量名)

矩陣乘法:tf.matmul(矩陣1,矩陣2)

示例:

import tensorflow as tf

a = tf.ones([3,2])

b = tf.fill([2,3],3.)

print(tf.matmul(a,b))

結果

tf.tensor(

[[6. 6. 6.]

[6. 6. 6.]

[6. 6. 6.]], shape=(3, 3), dtype=float32)

三、輸入特徵與標籤配對

生成輸入特徵/標籤對,構建資料集

data =tf.data.dataset.from_tensor_slices(輸入特徵,標籤)

示例:

import tensorflow as tf

features = tf.constant([12, 23, 10, 17])

labels = tf.constant([0, 1, 1, 0])

dataset = tf.data.dataset.from_tensor_slices((features, labels))

for element in dataset:

print(element)

結果

(, )

(, )

(, )

(, )

可以看出,標籤與特徵配對成功啦!!!

四、求導

1.with結構中利用tf.gradienttapeq()求張量梯度,結構如下:

with tf.gradienttapeq() as tape:

計算過程

grad = tape.gradient(函式,對誰求導)

示例:

import tensorflow as tf

with tf.gradienttape() as tape:

w = tf.variable(tf.constant(3.0))

loss = tf.pow(x, 2)

grad = tape.gradient(loss, w)

print(grad)

執行結果:tf.tensor(6.0, shape=(), dtype=float32)

2.enumerate(列表名)

enumerate可遍歷每個元素,常在for迴圈中使用

示例:

seq = ['one', 'two', 'three']

for i, element in enumerate(seq):

print(i, element)

結果:

0 one

1 two

2 three

3.獨熱碼(one-hot encoding)表示標籤

1表示是,0表示非

tf.one_hot()函式將待轉換資料轉換為one-hot形式的資料輸出。

tf.one_hot(待轉換資料,depth=幾分類)

示例:

import tensorflow as tf

classes = 3

labels = tf.constant([1, 0, 2]) # 輸入的元素值最小為0,最大為2

output = tf.one_hot(labels, depth=classes)

print("result of labels1:", output)

print("\n")

結果:

result of labels1: tf.tensor(

[[0. 1. 0.]

[1. 0. 0.]

[0. 0. 1.]], shape=(3, 3), dtype=float32)

4.輸出概率

tf.nn.softmax()可以將n分類的n輸出轉換為概率輸出

示例:

import tensorflow as tf

y = tf.constant([1.01, 2.01, -0.66])

y_pro = tf.nn.softmax(y)

print("after softmax, y_pro is:", y_pro) # y_pro 符合概率分布

print("the sum of y_pro:", tf.reduce_sum(y_pro)) # 通過softmax後,所有概率加起來和為1

結果:

after softmax, y_pro is: tf.tensor([0.25598174 0.69583046 0.0481878 ], shape=(3,), dtype=float32)  

the sum of y_pro: tf.tensor(1.0, shape=(), dtype=float32)

5.assign_sub(w要自減的大小n) 並將w-n返回 

使用前要利用tf.variable將變數定義為可更新(可訓練)。

示例:

import tensorflow as tf

x = tf.variable(4)

x.assign_sub(1)

print("x:", x) # 4-1=3

結果:  

x:
6.tf.argmax(張量名,axis=操作軸)返回指定方向最大值的索引

示例:

import numpy as np

import tensorflow as tf

test = np.array([[1, 2, 3], [2, 3, 4], [5, 4, 3], [8, 7, 2]])

print("test:\n", test)

print("每一列的最大值的索引:", tf.argmax(test, axis=0)) # 返回每一列最大值的索引

print("每一行的最大值的索引", tf.argmax(test, axis=1)) # 返回每一行最大值的索引

結果:

test:

[[1 2 3]

[2 3 4]

[5 4 3]

[8 7 2]]

每一列的最大值的索引: tf.tensor([3 3 1], shape=(3,), dtype=int64)

每一行的最大值的索引 tf.tensor([2 2 0 0], shape=(4,), dtype=int64)

tensorflow2 0學習筆記(3 2)

自編碼器變種 1 denoising auto encoder 加隨機雜訊 2 dropout auto encoder 防止過擬合 3 adversarial auto encoder 利用額外的判別器網路來判定降維的隱藏變數?是否取樣先驗分布?對抗自編碼器是從下一章要介紹的生成對抗網路演算法衍生...

Tensorflow2 0學習筆記 建立張量

使用constant建立張量 使用constant函式建立張量 其中 1,5 表示張量內容,dtype表示資料型別 a tf.constant 1,5 dtype tf.int32 輸出a,a的資料型別,a的形狀 print a print a.dtype print a.shape 輸出結果 tf...

Tensorflow2 0學習筆記 常用函式(一)

1.資料型別轉換函式 定義乙個張量 a tf.constant 0,1,2 3,4,5 6,7,8 dtype tf.int64 強制轉換型別函式 b tf.cast a,tf.float32 reduce max查詢張量中最大的數,axis x表示對對應的行或者列求和 本例中為二維張量,0對應的對...