Tensorflow2實現三層神經網路的前向傳輸

2021-10-23 20:44:21 字數 3266 閱讀 4802

匯入所需要的包

import tensorflow as tf
匯入資料集,本次採用的tensorflow提供的經典是mnist手寫資料集

# x:[60k,28,28],

# y:[60k]

(x, y)

,_ = tf.keras.datasets.mnist.load_data(

)# x:[0-255]->[0,1] y:[0-9]

# 將x,y 轉換為tensor,並且將x歸一化

x = tf.convert_to_tensor(x, dtype=tf.float32)

/255

.y = tf.convert_to_tensor(y, dtype=tf.int32)

print

(x.shape,y.shape,x.dtype,y.dtype)

輸出x,y的shape為下圖,x表示60000張28*28 的,y對應60000個標籤,範圍為【0-9】設定batch為128,即一次訓練128條資料。

# 設定batch為128

train_db = tf.data.dataset.from_tensor_slices(

(x,y)

).batch(

128)

train_iter =

iter

(train_db)

sample =

next

(train_iter)

# 乙個batch的形狀

print

('batch:'

,sample[0]

.shape,sample[1]

.shape)

以下為訓練所需引數和過程,本次設計為三層神經網路。輸入層為28*28的,節點為784,第二層為256個節點,第三層為128個節點,輸出層為10個節點,注釋中,b為訓練資料的個數(維數)。

# 建立權值

# 降維過程 [b,784]->[b,256]->[b,128]->[b,10]

# [dim_in, dim_out],[dim_out]

# 隨機生成乙個權重矩陣,並且初始化每一層的偏置

# 由於下文中的梯度下降法,tape預設只會跟蹤tf.variable型別的資訊,所以進行轉換。

w1 = tf.variable(tf.random.truncated_normal(

[784

,256

],stddev=

0.1)

)b1 = tf.variable(tf.zeros(

[256])

)w2 = tf.variable(tf.random.truncated_normal(

[256

,128

],stddev=

0.1)

)b2 = tf.variable(tf.zeros(

[128])

)w3 = tf.variable(tf.random.truncated_normal(

[128,10

],stddev=

0.1)

)b3 = tf.variable(tf.zeros([10

]))lr =1e-

3#0.001 10的-3次方

訓練過程如下**,設定epoch為10:

for epoch in

range(10

):# enumerate處理後可以返回當前步驟的step,便於列印當前資訊

print

('epoch'

,epoch)

for step,

(x,y)

inenumerate

(train_db)

:#x :[128,28,28]

#y :[128]

x = tf.reshape(x,[-

1,28*

28])with tf.gradienttape(

)as tape:

# x :[128,28*28]

# h1 = x@w1+b1

# [b,784]@[784*256]+[256]->[b,256]+[256]->[b,256]+[b,256]

h1 = x@w1 +tf.broadcast_to(b1,

[x.shape[0]

,256])

h1 = tf.nn.relu(h1)

h2 = h1@w2 + b2

h2 = tf.nn.relu(h2)

out = h2@w3 + b3

# compute loss 計算誤差

# out:[b,10]

y_onehot = tf.one_hot(y,depth=10)

# mse = mean(sum(y-out)^2)

loss = tf.square(y_onehot-out)

# mean: scalar

loss = tf.reduce_mean(loss)

# compute gradients

grads = tape.gradient(loss,

[w1,b1,w2,b2,w3,b3]

)# w1 = w1 - lr * w1_grad

w1.assign_sub(lr * grads[0]

)# 保持w1原地更新,保持引用不變,型別不變

b1.assign_sub(lr * grads[1]

) w2.assign_sub(lr * grads[2]

) b2.assign_sub(lr * grads[3]

) w3.assign_sub(lr * grads[4]

) b3.assign_sub(lr * grads[5]

)if step %

100==0:

print

(step,

' loss:'

,float

(loss)

)

執行結果如下圖:

tensorflow2實現線性回歸例子

tensorflow version 2.x import tensorflow as tf from tensorflow import keras from tensorflow.keras import layers from tensorflow import initializers as...

基於tensorflow2實現卷積神經網路

利用tensorflow2中的api實現乙個簡單的卷積神經網路,完成梯度下降的操作並繪製訓練集和測試集準確率曲線。資料集在這裡 資料分布 訓練集數量為209,測試集數量為50 import numpy as np import matplotlib.pyplot as plt import tens...

tensorflow2實現yolov3過程

本篇文章為閱讀 源 後整理的筆記。部分變數我還是理解的不到位 如下面打問號的地方 希望有大神能多多指教!配置引數準備 train input size 大小 416 strides 跨度 取值 8,16,32 類別 80個類別 batch size 4 train output sizes 計算方法...