判别器loss为0_TensorFlow v2.0实现逻辑斯谛回归
生活随笔
收集整理的這篇文章主要介紹了
判别器loss为0_TensorFlow v2.0实现逻辑斯谛回归
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
使用TensorFlow v2.0實現邏輯斯諦回歸
此示例使用簡單方法來更好地理解訓練過程背后的所有機制
MNIST數據集概覽
此示例使用MNIST手寫數字。該數據集包含60,000個用于訓練的樣本和10,000個用于測試的樣本。這些數字已經過尺寸標準化并位于圖像中心,圖像是固定大小(28x28像素),其值為0到255。
在此示例中,每個圖像將轉換為float32,歸一化為[0,1],并展平為784個特征(28 * 28)的1維數組。
from __future__ import absolute_import,division,print_functionimport tensorflow as tfimport numpy as np# MNIST 數據集參數num_classes = 10 # 數字0-9num_features = 784 # 28*28# 訓練參數learning_rate = 0.01training_steps = 1000batch_size = 256display_step = 50# 準備MNIST數據from tensorflow.keras.datasets import mnist(x_train, y_train),(x_test,y_test) = mnist.load_data()# 轉換為float32x_train, x_test = np.array(x_train, np.float32), np.array(x_test, np.float32)# 將圖像平鋪成784個特征的一維向量(28*28)x_train, x_test = x_train.reshape([-1, num_features]), x_test.reshape([-1, num_features])# 將像素值從[0,255]歸一化為[0,1]x_train,x_test = x_train / 255, x_test / 255# 使用tf.data api 對數據隨機分布和批處理train_data = tf.data.Dataset.from_tensor_slices((x_train, y_train))train_data = train_data.repeat().shuffle(5000).batch(batch_size).prefetch(1)# 權值矩陣形狀[784,10],28 * 28圖像特征數和類別數目W = tf.Variable(tf.ones([num_features, num_classes]), name="weight")# 偏置形狀[10], 類別數目b = tf.Variable(tf.zeros([num_classes]), name="bias")# 邏輯斯諦回歸(Wx+b)def logistic_regression(x): #應用softmax將logits標準化為概率分布 return tf.nn.softmax(tf.matmul(x,W) + b)# 交叉熵損失函數def cross_entropy(y_pred, y_true): # 將標簽編碼為一個獨熱編碼向量 y_true = tf.one_hot(y_true, depth=num_classes) # 壓縮預測值以避免log(0)錯誤 y_pred = tf.clip_by_value(y_pred, 1e-9, 1.) # 計算交叉熵 return tf.reduce_mean(-tf.reduce_sum(y_true * tf.math.log(y_pred)))# 準確率度量def accuracy(y_pred, y_true): # 預測的類別是預測向量中最高分的索引(即argmax) correct_prediction = tf.equal(tf.argmax(y_pred, 1), tf.cast(y_true, tf.int64)) return tf.reduce_mean(tf.cast(correct_prediction, tf.float32))# 隨機梯度下降優化器optimizer = tf.optimizers.SGD(learning_rate)# 優化過程def run_optimization(x, y): #將計算封裝在GradientTape中以實現自動微分 with tf.GradientTape() as g: pred = logistic_regression(x) loss = cross_entropy(pred, y) # 計算梯度 gradients = g.gradient(loss, [W, b]) # 根據gradients更新 W 和 b optimizer.apply_gradients(zip(gradients, [W, b]))# 針對給定訓練步驟數開始訓練for step, (batch_x,batch_y) in enumerate(train_data.take(training_steps), 1): # 運行優化以更新W和b值 run_optimization(batch_x, batch_y) if step % display_step == 0: pred = logistic_regression(batch_x) loss = cross_entropy(pred, batch_y) acc = accuracy(pred, batch_y) print("step: %i, loss: %f, accuracy: %f" % (step, loss, acc))output:
step: 50, loss: 608.584717, accuracy: 0.824219step: 100, loss: 828.206482, accuracy: 0.765625step: 150, loss: 716.329407, accuracy: 0.746094step: 200, loss: 584.887634, accuracy: 0.820312step: 250, loss: 472.098114, accuracy: 0.871094step: 300, loss: 621.834595, accuracy: 0.832031step: 350, loss: 567.288818, accuracy: 0.714844step: 400, loss: 489.062988, accuracy: 0.847656step: 450, loss: 496.466675, accuracy: 0.843750step: 500, loss: 465.342224, accuracy: 0.875000step: 550, loss: 586.347168, accuracy: 0.855469step: 600, loss: 95.233109, accuracy: 0.906250step: 650, loss: 88.136490, accuracy: 0.910156step: 700, loss: 67.170349, accuracy: 0.937500step: 750, loss: 79.673691, accuracy: 0.921875step: 800, loss: 112.844872, accuracy: 0.914062step: 850, loss: 92.789581, accuracy: 0.894531step: 900, loss: 80.116165, accuracy: 0.921875step: 950, loss: 45.706650, accuracy: 0.925781step: 1000, loss: 72.986969, accuracy: 0.925781# 在驗證集上測試模型pred = logistic_regression(x_test)print("Test Accuracy: %f" % accuracy(pred, y_test))output:
Test Accuracy: 0.901100# 可視化預測import matplotlib.pyplot as plt# 在驗證集上中預測5張圖片n_images = 5test_images = x_test[:n_images]predictions = logistic_regression(test_images)# 可視化圖片和模型預測結果for i in range(n_images): plt.imshow(np.reshape(test_images[i],[28,28]), cmap='gray') plt.show() print("Model prediction: %i" % np.argmax(predictions.numpy()[i]))output:
Model prediction: 7Model prediction: 2Model prediction: 1Model prediction: 0Model prediction: 4總結
以上是生活随笔為你收集整理的判别器loss为0_TensorFlow v2.0实现逻辑斯谛回归的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python iloc函数_python
- 下一篇: dicom格式怎么转换_QQ音乐下载的歌