成人免费无码不卡毛片,亚洲AⅤ无码精品一区二区三区,国产尤物精品视频,久久精品日本亚洲,欧美成人一区三区无码乱码A片,中文字日产幕码一区二区色哟哟,亞洲日韓中文字幕網AV

  • 正文
    • 識別鳶尾花(iris)數(shù)據(jù)集
    • 識別MNIST手寫數(shù)據(jù)集
  • 相關(guān)推薦
申請入駐 產(chǎn)業(yè)圖譜

最簡單用TensorFlow實現(xiàn)神經(jīng)網(wǎng)絡DNN(iris和mnist數(shù)據(jù)集)

2024/12/11
2767
加入交流群
掃碼加入
獲取工程師必備禮包
參與熱點資訊討論

識別鳶尾花(iris)數(shù)據(jù)集

不用keras

無中間層,最簡單一層實現(xiàn)識別,W個數(shù)為4*3,b個數(shù)為3,batch_size為32

# 利用鳶尾花數(shù)據(jù)集,實現(xiàn)前向傳播、反向傳播,可視化loss曲線

# 導入所需模塊
import tensorflow as tf
from sklearn import datasets
from matplotlib import pyplot as plt
import numpy as np

# 導入數(shù)據(jù),分別為輸入特征和標簽
x_data = datasets.load_iris().data
y_data = datasets.load_iris().target

# 隨機打亂數(shù)據(jù)(因為原始數(shù)據(jù)是順序的,順序不打亂會影響準確率)
# seed: 隨機數(shù)種子,是一個整數(shù),當設置之后,每次生成的隨機數(shù)都一樣
np.random.seed(116)  # 使用相同的seed,保證輸入特征和標簽一一對應
np.random.shuffle(x_data)
np.random.seed(116)
np.random.shuffle(y_data)
tf.random.set_seed(116)

# 將打亂后的數(shù)據(jù)集分割為訓練集和測試集,訓練集為前120行,測試集為后30行
x_train = x_data[:-30]
y_train = y_data[:-30]
x_test = x_data[-30:]
y_test = y_data[-30:]

# 轉(zhuǎn)換x的數(shù)據(jù)類型,否則后面矩陣相乘時會因數(shù)據(jù)類型不一致報錯
x_train = tf.cast(x_train, tf.float32)
x_test = tf.cast(x_test, tf.float32)

# from_tensor_slices函數(shù)使輸入特征和標簽值一一對應。(把數(shù)據(jù)集分批次,每個批次batch組數(shù)據(jù))
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(32)

# 生成神經(jīng)網(wǎng)絡的參數(shù),4個輸入特征數(shù),輸入層為4個輸入節(jié)點;因為3分類,故輸出層為3個神經(jīng)元
# 用tf.Variable()標記參數(shù)可訓練
# 使用seed使每次生成的隨機數(shù)相同
w1 = tf.Variable(tf.random.truncated_normal([4, 3], stddev=0.1, seed=1))
b1 = tf.Variable(tf.random.truncated_normal([3], stddev=0.1, seed=1))

lr = 0.1  # 學習率為0.1
train_loss_results = []  # 將每輪的loss記錄在此列表中,為后續(xù)畫loss曲線提供數(shù)據(jù)
test_acc = []  # 將每輪的acc記錄在此列表中,為后續(xù)畫acc曲線提供數(shù)據(jù)
epoch = 500  # 循環(huán)500輪
loss_all = 0  # 每輪分4個step,loss_all記錄四個step生成的4個loss的和

# 訓練部分
for epoch in range(epoch):  #數(shù)據(jù)集級別的循環(huán),每個epoch循環(huán)一次數(shù)據(jù)集
    for step, (x_train, y_train) in enumerate(train_db):  #batch級別的循環(huán) ,每個step循環(huán)一個batch
        with tf.GradientTape() as tape:  # with結(jié)構(gòu)記錄梯度信息
            y = tf.matmul(x_train, w1) + b1  # 神經(jīng)網(wǎng)絡乘加運算
            y = tf.nn.softmax(y)  # 使輸出y符合概率分布(此操作后與獨熱碼同量級,可相減求loss)
            y_ = tf.one_hot(y_train, depth=3)  # 將標簽值轉(zhuǎn)換為獨熱碼格式,方便計算loss和accuracy
            loss = tf.reduce_mean(tf.square(y_ - y))  # 采用均方誤差損失函數(shù)mse = mean(sum(y-out)^2)
            loss_all += loss.numpy()  # 將每個step計算出的loss累加,為后續(xù)求loss平均值提供數(shù)據(jù),這樣計算的loss更準確
        # 計算loss對各個參數(shù)的梯度
        grads = tape.gradient(loss, [w1, b1])

        # 實現(xiàn)梯度更新 w1 = w1 - lr * w1_grad    b = b - lr * b_grad
        w1.assign_sub(lr * grads[0])  # 參數(shù)w1自更新
        b1.assign_sub(lr * grads[1])  # 參數(shù)b自更新

    # 每個epoch,打印loss信息
    print("Epoch {}, loss: {}".format(epoch, loss_all/4))
    train_loss_results.append(loss_all / 4)  # 將4個step的loss求平均記錄在此變量中
    loss_all = 0  # loss_all歸零,為記錄下一個epoch的loss做準備

    # 測試部分
    # total_correct為預測對的樣本個數(shù), total_number為測試的總樣本數(shù),將這兩個變量都初始化為0
    total_correct, total_number = 0, 0
    for x_test, y_test in test_db:
        # 使用更新后的參數(shù)進行預測
        y = tf.matmul(x_test, w1) + b1
        y = tf.nn.softmax(y)
        pred = tf.argmax(y, axis=1)  # 返回y中最大值的索引,即預測的分類
        # 將pred轉(zhuǎn)換為y_test的數(shù)據(jù)類型
        pred = tf.cast(pred, dtype=y_test.dtype)
        # 若分類正確,則correct=1,否則為0,將bool型的結(jié)果轉(zhuǎn)換為int型
        correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)
        # 將每個batch的correct數(shù)加起來
        correct = tf.reduce_sum(correct)
        # 將所有batch中的correct數(shù)加起來
        total_correct += int(correct)
        # total_number為測試的總樣本數(shù),也就是x_test的行數(shù),shape[0]返回變量的行數(shù)
        total_number += x_test.shape[0]
    # 總的準確率等于total_correct/total_number
    acc = total_correct / total_number
    test_acc.append(acc)
    print("Test_acc:", acc)
    print("--------------------------")

# 繪制 loss 曲線
plt.title('Loss Function Curve')  # 圖片標題
plt.xlabel('Epoch')  # x軸變量名稱
plt.ylabel('Loss')  # y軸變量名稱
plt.plot(train_loss_results, label="$Loss$")  # 逐點畫出trian_loss_results值并連線,連線圖標是Loss
plt.legend()  # 畫出曲線圖標
plt.show()  # 畫出圖像

# 繪制 Accuracy 曲線
plt.title('Acc Curve')  # 圖片標題
plt.xlabel('Epoch')  # x軸變量名稱
plt.ylabel('Acc')  # y軸變量名稱
plt.plot(test_acc, label="$Accuracy$")  # 逐點畫出test_acc值并連線,連線圖標是Accuracy
plt.legend()
plt.show()

在這里插入圖片描述

用keras

import tensorflow as tf
from sklearn import datasets
import numpy as np

#拿到數(shù)據(jù)
x_train = datasets.load_iris().data
y_train = datasets.load_iris().target

#數(shù)據(jù)打亂
np.random.seed(116)
np.random.shuffle(x_train)
np.random.seed(116)
np.random.shuffle(y_train)
tf.random.set_seed(116)

#搭建模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(3, activation='softmax', kernel_regularizer=tf.keras.regularizers.l2())
])
#模型設置
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.1),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])
#模型訓練
model.fit(x_train, y_train, batch_size=32, epochs=600, validation_split=0.2, validation_freq=20)

model.summary()

在這里插入圖片描述

識別MNIST手寫數(shù)據(jù)集

不用keras

無中間層,最簡單一層實現(xiàn)識別,W個數(shù)為784*10,b個數(shù)為10,batch_size為512

# -*- coding: UTF-8 -*-
# 利用鳶尾花數(shù)據(jù)集,實現(xiàn)前向傳播、反向傳播,可視化loss曲線

# 導入所需模塊
import tensorflow as tf
from matplotlib import pyplot as plt

mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
x_train = x_train.reshape(-1,784)  # flatten, (60000,28,28)變(60000,784)
x_test = x_test.reshape(-1,784)  # flatten, (10000,28,28)變(10000,784)

# 轉(zhuǎn)換x的數(shù)據(jù)類型,否則后面矩陣相乘時會因數(shù)據(jù)類型不一致報錯
x_train = tf.cast(x_train, tf.float32)
x_test = tf.cast(x_test, tf.float32)

# from_tensor_slices函數(shù)使輸入特征和標簽值一一對應。(把數(shù)據(jù)集分批次,每個批次batch組數(shù)據(jù))
train_db = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(512)
test_db = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(512)

# 生成神經(jīng)網(wǎng)絡的參數(shù),4個輸入特征故,輸入層為4個輸入節(jié)點;因為3分類,故輸出層為3個神經(jīng)元
# 用tf.Variable()標記參數(shù)可訓練
# 使用seed使每次生成的隨機數(shù)相同(方便教學,使大家結(jié)果都一致,在現(xiàn)實使用時不寫seed)
w1 = tf.Variable(tf.random.truncated_normal([784, 10], stddev=0.1, seed=1))
b1 = tf.Variable(tf.random.truncated_normal([10], stddev=0.1, seed=1))

lr = 0.05  # 學習率為0.1
train_loss_results = []  # 將每輪的loss記錄在此列表中,為后續(xù)畫loss曲線提供數(shù)據(jù)
test_acc = []  # 將每輪的acc記錄在此列表中,為后續(xù)畫acc曲線提供數(shù)據(jù)
epoch = 500  # 循環(huán)500輪
loss_all = 0  # 每輪分4個step,loss_all記錄四個step生成的4個loss的和

# 訓練部分
for epoch in range(epoch):  #數(shù)據(jù)集級別的循環(huán),每個epoch循環(huán)一次數(shù)據(jù)集
    for step, (x_train, y_train) in enumerate(train_db):  #batch級別的循環(huán) ,每個step循環(huán)一個batch
        with tf.GradientTape() as tape:  # with結(jié)構(gòu)記錄梯度信息
            y = tf.matmul(x_train, w1) + b1  # 神經(jīng)網(wǎng)絡乘加運算
            y = tf.nn.softmax(y)  # 使輸出y符合概率分布(此操作后與獨熱碼同量級,可相減求loss)
            y_ = tf.one_hot(y_train, depth=10)  # 將標簽值轉(zhuǎn)換為獨熱碼格式,方便計算loss和accuracy
            loss = tf.reduce_mean(tf.square(y_ - y))  # 采用均方誤差損失函數(shù)mse = mean(sum(y-out)^2)
            loss_all += loss.numpy()  # 將每個step計算出的loss累加,為后續(xù)求loss平均值提供數(shù)據(jù),這樣計算的loss更準確
        # 計算loss對各個參數(shù)的梯度
        grads = tape.gradient(loss, [w1, b1])

        # 實現(xiàn)梯度更新 w1 = w1 - lr * w1_grad    b = b - lr * b_grad
        w1.assign_sub(lr * grads[0])  # 參數(shù)w1自更新
        b1.assign_sub(lr * grads[1])  # 參數(shù)b自更新

    # 每個epoch,打印loss信息
    print("Epoch {}, loss: {}".format(epoch, loss_all/118))
    train_loss_results.append(loss_all / 118)  # 將4個step的loss求平均記錄在此變量中
    loss_all = 0  # loss_all歸零,為記錄下一個epoch的loss做準備

    # 測試部分
    # total_correct為預測對的樣本個數(shù), total_number為測試的總樣本數(shù),將這兩個變量都初始化為0
    total_correct, total_number = 0, 0
    for x_test, y_test in test_db:
        # 使用更新后的參數(shù)進行預測
        y = tf.matmul(x_test, w1) + b1
        y = tf.nn.softmax(y)
        pred = tf.argmax(y, axis=1)  # 返回y中最大值的索引,即預測的分類
        # 將pred轉(zhuǎn)換為y_test的數(shù)據(jù)類型
        pred = tf.cast(pred, dtype=y_test.dtype)
        # 若分類正確,則correct=1,否則為0,將bool型的結(jié)果轉(zhuǎn)換為int型
        correct = tf.cast(tf.equal(pred, y_test), dtype=tf.int32)
        # 將每個batch的correct數(shù)加起來
        correct = tf.reduce_sum(correct)
        # 將所有batch中的correct數(shù)加起來
        total_correct += int(correct)
        # total_number為測試的總樣本數(shù),也就是x_test的行數(shù),shape[0]返回變量的行數(shù)
        total_number += x_test.shape[0]
    # 總的準確率等于total_correct/total_number
    acc = total_correct / total_number
    test_acc.append(acc)
    print("Test_acc:", acc)
    print("--------------------------")

# 繪制 loss 曲線
plt.subplot(1,2,1)
plt.title('Loss Function Curve')  # 圖片標題
plt.xlabel('Epoch')  # x軸變量名稱
plt.ylabel('Loss')  # y軸變量名稱
plt.plot(train_loss_results, label="$Loss$")  # 逐點畫出trian_loss_results值并連線,連線圖標是Loss
plt.legend()  # 畫出曲線圖標

# 繪制 Accuracy 曲線
plt.subplot(1,2,2)
plt.title('Acc Curve')  # 圖片標題
plt.xlabel('Epoch')  # x軸變量名稱
plt.ylabel('Acc')  # y軸變量名稱
plt.plot(test_acc, label="$Accuracy$")  # 逐點畫出test_acc值并連線,連線圖標是Accuracy
plt.legend()
plt.show()

在這里插入圖片描述

用keras

2層中間層(flatten層算輸入層),W個數(shù)為784128+12810,b個數(shù)為128+10

import tensorflow as tf

#拿到數(shù)據(jù)
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0

#搭建模型
model = tf.keras.models.Sequential([
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])
#模型設置
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=False),
              metrics=['sparse_categorical_accuracy'])
#模型訓練
model.fit(x_train, y_train, batch_size=32, epochs=5, validation_data=(x_test, y_test), validation_freq=1)

model.summary()

在這里插入圖片描述

相關(guān)推薦

彰化县| 蓬安县| 康平县| 陈巴尔虎旗| 西畴县| 垫江县| 阿克| 双桥区| 全州县| 北辰区| 常熟市| 砀山县| 城口县| 疏附县| 凯里市| 武夷山市| 西充县| 岢岚县| 万宁市| 永川市| 巴里| 麦盖提县| 汉川市| 南投市| 昂仁县| 株洲市| 科技| 康平县| 津南区| 西平县| 桐庐县| 泽库县| 理塘县| 资阳市| 罗山县| 溧水县| 分宜县| 龙山县| 甘孜| 莆田市| 稷山县|