佳木斯湛栽影视文化发展公司

主頁 > 知識庫 > 詳解TensorFlow2實(shí)現(xiàn)前向傳播

詳解TensorFlow2實(shí)現(xiàn)前向傳播

熱門標(biāo)簽:檢查注冊表項(xiàng) 美圖手機(jī) 智能手機(jī) 銀行業(yè)務(wù) 鐵路電話系統(tǒng) 服務(wù)器配置 網(wǎng)站文章發(fā)布 呼叫中心市場需求

概述

前向傳播 (Forward propagation) 是將上一層輸出作為下一層的輸入, 并計(jì)算下一層的輸出, 一直到運(yùn)算到輸出層為止.

會用到的函數(shù)

張量最小值

```reduce_min``函數(shù)可以幫助我們計(jì)算一個張量各個維度上元素的最小值.

格式:

tf.math.reduce_min(
    input_tensor, axis=None, keepdims=False, name=None
)

參數(shù):

  • input_tensor: 傳入的張量
  • axis: 維度, 默認(rèn)計(jì)算所有維度
  • keepdims: 如果為真保留維度, 默認(rèn)為 False
  • name: 數(shù)據(jù)名稱

張量最大值

```reduce_max``函數(shù)可以幫助我們計(jì)算一個張量各個維度上元素的最大值.

格式:

tf.math.reduce_max(
    input_tensor, axis=None, keepdims=False, name=None
)

參數(shù):

  • input_tensor: 傳入的張量
  • axis: 維度, 默認(rèn)計(jì)算所有維度
  • keepdims: 如果為真保留維度, 默認(rèn)為 False
  • name: 數(shù)據(jù)名稱

數(shù)據(jù)集分批

from_tensor_slices可以幫助我們切分傳入 Tensor 的第一個維度. 得到的每個切片都是一個樣本數(shù)據(jù).

格式:

@staticmethod
from_tensor_slices(
    tensors
)

迭代

我們可以調(diào)用iter函數(shù)來生成迭代器.

格式:

iter(object[, sentinel])

參數(shù):
-object: 支持迭代的集合對象

  • sentinel: 如果傳遞了第二個參數(shù), 則參數(shù) object 必須是一個可調(diào)用的對象 (如, 函數(shù)). 此時, iter 創(chuàng)建了一個迭代器對象, 每次調(diào)用這個迭代器對象的__next__()方法時, 都會調(diào)用 object

例子:

list = [1, 2, 3]
i = iter(list)
print(next(i))
print(next(i))
print(next(i))

輸出結(jié)果:

1
2
3

截?cái)嗾龖B(tài)分布

truncated_normal可以幫助我們生成一個截?cái)嗟恼龖B(tài)分布. 生成的正態(tài)分布值會在兩倍的標(biāo)準(zhǔn)差的范圍之內(nèi).

格式:

tf.random.truncated_normal(
    shape, mean=0.0, stddev=1.0, dtype=tf.dtypes.float32, seed=None, name=None
)

參數(shù):

  • shape: 張量的形狀
  • mean: 正態(tài)分布的均值, 默認(rèn) 0.0
  • stddev: 正態(tài)分布的標(biāo)準(zhǔn)差, 默認(rèn)為 1.0
  • dtype: 數(shù)據(jù)類型, 默認(rèn)為 float32
  • seed: 隨機(jī)數(shù)種子
  • name: 數(shù)據(jù)名稱

relu 激活函數(shù)

激活函數(shù)有 sigmoid, maxout, relu 等等函數(shù). 通過激活函數(shù)我們可以使得各個層之間達(dá)成非線性關(guān)系.

激活函數(shù)可以幫助我們提高模型健壯性, 提高非線性表達(dá)能力, 緩解梯度消失問題.

one_hot

tf.one_hot函數(shù)是講 input 準(zhǔn)換為 one_hot 類型數(shù)據(jù)輸出. 相當(dāng)于將多個數(shù)值聯(lián)合放在一起作為多個相同類型的向量.

格式:

tf.one_hot(
    indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None
)

參數(shù):

  • indices: 索引的張量
  • depth: 指定獨(dú)熱編碼維度的標(biāo)量
  • on_value: 索引 indices[j] = i 位置處填充的標(biāo)量,默認(rèn)為 1
  • off_value: 索引 indices[j] != i 所有位置處填充的標(biāo)量, 默認(rèn)為 0
  • axis: 填充的軸, 默認(rèn)為 -1 (最里面的新軸)
  • dtype: 輸出張量的數(shù)據(jù)格式
  • name:數(shù)據(jù)名稱

assign_sub

assign_sub可以幫助我們實(shí)現(xiàn)張量自減.

格式:

tf.compat.v1.assign_sub(
    ref, value, use_locking=None, name=None
)

參數(shù):

  • ref: 多重張量
  • value: 張量
  • use_locking: 鎖
  • name: 數(shù)據(jù)名稱

準(zhǔn)備工作

import tensorflow as tf

# 定義超參數(shù)
batch_size = 256  # 一次訓(xùn)練的樣本數(shù)目
learning_rate = 0.001  # 學(xué)習(xí)率
iteration_num = 20  # 迭代次數(shù)

# 讀取mnist數(shù)據(jù)集
(x, y), _ = tf.keras.datasets.mnist.load_data()  # 讀取訓(xùn)練集的特征值和目標(biāo)值
print(x[:5])  # 調(diào)試輸出前5個圖
print(y[:5])  # 調(diào)試輸出前5個目標(biāo)值數(shù)字
print(x.shape)  # (60000, 28, 28) 單通道
print(y.shape)  # (60000,)

# 轉(zhuǎn)換成常量tensor
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255  # 轉(zhuǎn)換為0~1的形式
y = tf.convert_to_tensor(y, dtype=tf.int32)  # 轉(zhuǎn)換為整數(shù)形式

# 調(diào)試輸出范圍
print(tf.reduce_min(x), tf.reduce_max(x))  # 0~1
print(tf.reduce_min(y), tf.reduce_max(y))  # 0~9

# 分割數(shù)據(jù)集
train_db = tf.data.Dataset.from_tensor_slices((x, y)).batch(batch_size)  # 256為一個batch
train_iter = iter(train_db)  # 生成迭代對象

# 定義權(quán)重和bias [256, 784] => [256, 256] => [256, 128] => [128, 10]
w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))  # 標(biāo)準(zhǔn)差為0.1的截?cái)嗾龖B(tài)分布
b1 = tf.Variable(tf.zeros([256]))  # 初始化為0

w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))  # 標(biāo)準(zhǔn)差為0.1的截?cái)嗾龖B(tài)分布
b2 = tf.Variable(tf.zeros([128]))  # 初始化為0

w3 = tf.Variable(tf.random.truncated_normal([128, 10], stddev=0.1))  # 標(biāo)準(zhǔn)差為0.1的截?cái)嗾龖B(tài)分布
b3 = tf.Variable(tf.zeros([10]))  # 初始化為0

輸出結(jié)果:

[[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]

[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]

[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]

[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]

[[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
...
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]
[0 0 0 ... 0 0 0]]]
[5 0 4 1 9]
(60000, 28, 28)
(60000,)
tf.Tensor(0.0, shape=(), dtype=float32) tf.Tensor(1.0, shape=(), dtype=float32)
tf.Tensor(0, shape=(), dtype=int32) tf.Tensor(9, shape=(), dtype=int32)

train 函數(shù)

def train(epoch):  # 訓(xùn)練
    for step, (x, y) in enumerate(train_db):  # 每一批樣本遍歷
        # 把x平鋪 [256, 28, 28] => [256, 784]
        x = tf.reshape(x, [-1, 784])

        with tf.GradientTape() as tape:  # 自動求解
            # 第一個隱層 [256, 784] => [256, 256]
            # [256, 784]@[784, 256] + [256] => [256, 256] + [256] => [256, 256] + [256, 256] (廣播機(jī)制)
            h1 = x @ w1 + tf.broadcast_to(b1, [x.shape[0], 256])
            h1 = tf.nn.relu(h1)  # relu激活

            # 第二個隱層 [256, 256] => [256, 128]
            h2 = h1 @ w2 + b2
            h2 = tf.nn.relu(h2)  # relu激活

            # 輸出層 [256, 128] => [128, 10]
            out = h2 @ w3 + b3

            # 計(jì)算損失MSE(Mean Square Error)
            y_onehot = tf.one_hot(y, depth=10)  # 轉(zhuǎn)換成one_hot編碼
            loss = tf.square(y_onehot - out)  # 計(jì)算總誤差
            loss = tf.reduce_mean(loss)  # 計(jì)算平均誤差MSE


        # 計(jì)算梯度
        grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])

        # 更新權(quán)重
        w1.assign_sub(learning_rate * grads[0])  # 自減梯度*學(xué)習(xí)率
        b1.assign_sub(learning_rate * grads[1])  # 自減梯度*學(xué)習(xí)率
        w2.assign_sub(learning_rate * grads[2])  # 自減梯度*學(xué)習(xí)率
        b2.assign_sub(learning_rate * grads[3])  # 自減梯度*學(xué)習(xí)率
        w3.assign_sub(learning_rate * grads[4])  # 自減梯度*學(xué)習(xí)率
        b3.assign_sub(learning_rate * grads[5])  # 自減梯度*學(xué)習(xí)率

        if step % 100 == 0:  # 每運(yùn)行100個批次, 輸出一次
            print("epoch:", epoch, "step:", step, "loss:", float(loss))

run 函數(shù)

def run():
    for i in range(iteration_num):  # 迭代20次
        train(i)

完整代碼

import tensorflow as tf

# 定義超參數(shù)
batch_size = 256  # 一次訓(xùn)練的樣本數(shù)目
learning_rate = 0.001  # 學(xué)習(xí)率
iteration_num = 20  # 迭代次數(shù)

# 讀取mnist數(shù)據(jù)集
(x, y), _ = tf.keras.datasets.mnist.load_data()  # 讀取訓(xùn)練集的特征值和目標(biāo)值
print(x[:5])  # 調(diào)試輸出前5個圖
print(y[:5])  # 調(diào)試輸出前5個目標(biāo)值數(shù)字
print(x.shape)  # (60000, 28, 28) 單通道
print(y.shape)  # (60000,)

# 轉(zhuǎn)換成常量tensor
x = tf.convert_to_tensor(x, dtype=tf.float32) / 255  # 轉(zhuǎn)換為0~1的形式
y = tf.convert_to_tensor(y, dtype=tf.int32)  # 轉(zhuǎn)換為整數(shù)形式

# 調(diào)試輸出范圍
print(tf.reduce_min(x), tf.reduce_max(x))  # 0~1
print(tf.reduce_min(y), tf.reduce_max(y))  # 0~9

# 分割數(shù)據(jù)集
train_db = tf.data.Dataset.from_tensor_slices((x, y)).batch(batch_size)  # 256為一個batch
train_iter = iter(train_db)  # 生成迭代對象

# 定義權(quán)重和bias [256, 784] => [256, 256] => [256, 128] => [128, 10]
w1 = tf.Variable(tf.random.truncated_normal([784, 256], stddev=0.1))  # 標(biāo)準(zhǔn)差為0.1的截?cái)嗾龖B(tài)分布
b1 = tf.Variable(tf.zeros([256]))  # 初始化為0

w2 = tf.Variable(tf.random.truncated_normal([256, 128], stddev=0.1))  # 標(biāo)準(zhǔn)差為0.1的截?cái)嗾龖B(tài)分布
b2 = tf.Variable(tf.zeros([128]))  # 初始化為0

w3 = tf.Variable(tf.random.truncated_normal([128, 10], stddev=0.1))  # 標(biāo)準(zhǔn)差為0.1的截?cái)嗾龖B(tài)分布
b3 = tf.Variable(tf.zeros([10]))  # 初始化為0


def train(epoch):  # 訓(xùn)練
    for step, (x, y) in enumerate(train_db):  # 每一批樣本遍歷
        # 把x平鋪 [256, 28, 28] => [256, 784]
        x = tf.reshape(x, [-1, 784])

        with tf.GradientTape() as tape:  # 自動求解
            # 第一個隱層 [256, 784] => [256, 256]
            # [256, 784]@[784, 256] + [256] => [256, 256] + [256] => [256, 256] + [256, 256] (廣播機(jī)制)
            h1 = x @ w1 + tf.broadcast_to(b1, [x.shape[0], 256])
            h1 = tf.nn.relu(h1)  # relu激活

            # 第二個隱層 [256, 256] => [256, 128]
            h2 = h1 @ w2 + b2
            h2 = tf.nn.relu(h2)  # relu激活

            # 輸出層 [256, 128] => [128, 10]
            out = h2 @ w3 + b3

            # 計(jì)算損失MSE(Mean Square Error)
            y_onehot = tf.one_hot(y, depth=10)  # 轉(zhuǎn)換成one_hot編碼
            loss = tf.square(y_onehot - out)  # 計(jì)算總誤差
            loss = tf.reduce_mean(loss)  # 計(jì)算平均誤差MSE


        # 計(jì)算梯度
        grads = tape.gradient(loss, [w1, b1, w2, b2, w3, b3])

        # 更新權(quán)重
        w1.assign_sub(learning_rate * grads[0])  # 自減梯度*學(xué)習(xí)率
        b1.assign_sub(learning_rate * grads[1])  # 自減梯度*學(xué)習(xí)率
        w2.assign_sub(learning_rate * grads[2])  # 自減梯度*學(xué)習(xí)率
        b2.assign_sub(learning_rate * grads[3])  # 自減梯度*學(xué)習(xí)率
        w3.assign_sub(learning_rate * grads[4])  # 自減梯度*學(xué)習(xí)率
        b3.assign_sub(learning_rate * grads[5])  # 自減梯度*學(xué)習(xí)率

        if step % 100 == 0:  # 每運(yùn)行100個批次, 輸出一次
            print("epoch:", epoch, "step:", step, "loss:", float(loss))


def run():
    for i in range(iteration_num):  # 迭代20次
        train(i)


if __name__ == "__main__":
    run()

輸出結(jié)果:

epoch: 0 step: 0 loss: 0.5439826250076294
epoch: 0 step: 100 loss: 0.2263326346874237
epoch: 0 step: 200 loss: 0.19458135962486267
epoch: 1 step: 0 loss: 0.1788959801197052
epoch: 1 step: 100 loss: 0.15782299637794495
epoch: 1 step: 200 loss: 0.1580992043018341
epoch: 2 step: 0 loss: 0.15085121989250183
epoch: 2 step: 100 loss: 0.1432340145111084
epoch: 2 step: 200 loss: 0.14373672008514404
epoch: 3 step: 0 loss: 0.13810500502586365
epoch: 3 step: 100 loss: 0.13337770104408264
epoch: 3 step: 200 loss: 0.1334681361913681
epoch: 4 step: 0 loss: 0.12887853384017944
epoch: 4 step: 100 loss: 0.12551936507225037
epoch: 4 step: 200 loss: 0.125375896692276
epoch: 5 step: 0 loss: 0.12160968780517578
epoch: 5 step: 100 loss: 0.1190723180770874
epoch: 5 step: 200 loss: 0.11880680173635483
epoch: 6 step: 0 loss: 0.11563797295093536
epoch: 6 step: 100 loss: 0.11367204040288925
epoch: 6 step: 200 loss: 0.11331651359796524
epoch: 7 step: 0 loss: 0.11063456535339355
epoch: 7 step: 100 loss: 0.10906648635864258
epoch: 7 step: 200 loss: 0.10866570472717285
epoch: 8 step: 0 loss: 0.10636782646179199
epoch: 8 step: 100 loss: 0.10510052740573883
epoch: 8 step: 200 loss: 0.10468046367168427
epoch: 9 step: 0 loss: 0.10268573462963104
epoch: 9 step: 100 loss: 0.10163718461990356
epoch: 9 step: 200 loss: 0.10121693462133408
epoch: 10 step: 0 loss: 0.09949333965778351
epoch: 10 step: 100 loss: 0.09859145432710648
epoch: 10 step: 200 loss: 0.09819269925355911
epoch: 11 step: 0 loss: 0.0966767817735672
epoch: 11 step: 100 loss: 0.09586615860462189
epoch: 11 step: 200 loss: 0.09550992399454117
epoch: 12 step: 0 loss: 0.09417577087879181
epoch: 12 step: 100 loss: 0.09341947734355927
epoch: 12 step: 200 loss: 0.09310202300548553
epoch: 13 step: 0 loss: 0.09193204343318939
epoch: 13 step: 100 loss: 0.09122277796268463
epoch: 13 step: 200 loss: 0.09092779457569122
epoch: 14 step: 0 loss: 0.0899026170372963
epoch: 14 step: 100 loss: 0.08923697471618652
epoch: 14 step: 200 loss: 0.08895798027515411
epoch: 15 step: 0 loss: 0.08804921805858612
epoch: 15 step: 100 loss: 0.08742769062519073
epoch: 15 step: 200 loss: 0.0871589332818985
epoch: 16 step: 0 loss: 0.08635203540325165
epoch: 16 step: 100 loss: 0.0857706069946289
epoch: 16 step: 200 loss: 0.0855005756020546
epoch: 17 step: 0 loss: 0.08479145169258118
epoch: 17 step: 100 loss: 0.08423925191164017
epoch: 17 step: 200 loss: 0.08396687358617783
epoch: 18 step: 0 loss: 0.08334997296333313
epoch: 18 step: 100 loss: 0.08281457424163818
epoch: 18 step: 200 loss: 0.08254452794790268
epoch: 19 step: 0 loss: 0.08201286941766739
epoch: 19 step: 100 loss: 0.08149122446775436
epoch: 19 step: 200 loss: 0.08122102916240692

到此這篇關(guān)于詳解TensorFlow2實(shí)現(xiàn)前向傳播的文章就介紹到這了,更多相關(guān)TensorFlow2前向傳播內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • 手把手教你使用TensorFlow2實(shí)現(xiàn)RNN
  • tensorflow2.0實(shí)現(xiàn)復(fù)雜神經(jīng)網(wǎng)絡(luò)(多輸入多輸出nn,Resnet)
  • windows系統(tǒng)Tensorflow2.x簡單安裝記錄(圖文)
  • TensorFlow2基本操作之合并分割與統(tǒng)計(jì)
  • Python強(qiáng)化練習(xí)之Tensorflow2 opp算法實(shí)現(xiàn)月球登陸器

標(biāo)簽:上海 沈陽 新疆 滄州 河南 紅河 長治 樂山

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《詳解TensorFlow2實(shí)現(xiàn)前向傳播》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    大关县| 长顺县| 乐清市| 昂仁县| 咸阳市| 灵丘县| 澄江县| 都兰县| 龙陵县| 綦江县| 淮阳县| 沙河市| 衡阳市| 博野县| 登封市| 连城县| 察哈| 张家口市| 桂阳县| 通化市| 年辖:市辖区| 福贡县| 全南县| 东兰县| 韶山市| 瑞昌市| 文山县| 江津市| 咸丰县| 深圳市| 浦城县| 锡林浩特市| 荣成市| 双鸭山市| 麟游县| 滁州市| 水城县| 固安县| 奉贤区| 即墨市| 普宁市|