# 用于 MNIST 分類的基于 TensorFlow 的 MLP
首先,加載 MNIST 數據集,并使用以下代碼定義訓練和測試特征以及目標:
```py
from tensorflow.examples.tutorials.mnist import input_data
mnist_home = os.path.join(datasetslib.datasets_root, 'mnist')
mnist = input_data.read_data_sets(mnist_home, one_hot=True)
X_train = mnist.train.images
X_test = mnist.test.images
Y_train = mnist.train.labels
Y_test = mnist.test.labels
num_outputs = 10 # 0-9 digits
num_inputs = 784 # total pixels
```
我們創建了三個輔助函數,它們將幫助我們創建一個只有一個隱藏層的簡單 MLP,然后是一個更大的 MLP,每層有多個層和多個神經元。
`mlp()`函數使用以下邏輯構建網絡層:
1. `mlp()`函數需要五個輸入:
* `x`是輸入特征張量
* `num_inputs`是輸入特征的數量
* `num_outputs`是輸出目標的數量
* `num_layers`是所需隱藏層數
* `num_neurons`是包含每層神經元數量的列表
2. 將權重和偏差列表設置為空:
```py
w=[]
b=[]
```
1. 為隱藏層的數量運行循環以創建權重和偏移張量并將它們附加到各自的列表:
* 張量分別名為`w_<layer_num>`和`b_<layer_num>`。命名張量有助于調試和查找代碼問題。
* 使用`tf.random_normal()`以正態分布初始化張量。
* 權重張量的第一個維度是來自前一層的輸入數量。對于第一個隱藏層,第一個維度是`num_inputs`。權重張量的第二維是當前層中的神經元的數量。
* 偏差都是一維張量,其中維度等于當前層中的神經元數量。
```py
for i in range(num_layers):
# weights
w.append(tf.Variable(tf.random_normal(
[num_inputs if i == 0 else num_neurons[i - 1],
num_neurons[i]]),
name="w_{0:04d}".format(i)
))
# biases
b.append(tf.Variable(tf.random_normal(
[num_neurons[i]]),
name="b_{0:04d}".format(i)
))
```
1. 為最后一個隱藏層創建權重和偏差。在這種情況下,權重張量的維度等于最后隱藏層中的神經元數量和輸出目標的數量。偏差是一個張量,具有輸出特征數量大小的單一維度:
```py
w.append(tf.Variable(tf.random_normal(
[num_neurons[num_layers - 1] if num_layers > 0 else num_inputs,
num_outputs]), name="w_out"))
b.append(tf.Variable(tf.random_normal([num_outputs]),
name="b_out"))
```
1. 現在開始定義層。首先,將`x`視為第一個最明顯的輸入層:
```py
# x is input layer
layer = x
```
1. 在循環中添加隱藏的層。每個隱藏層表示通過激活函數`tf.nn.relu()`使線性函數`tf.matmul(layer, w[i]) + b[i]`非線性化:
```py
# add hidden layers
for i in range(num_layers):
layer = tf.nn.relu(tf.matmul(layer, w[i]) + b[i])
```
1. 添加輸出層。輸出層和隱藏層之間的一個區別是輸出層中沒有激活函數:
```py
layer = tf.matmul(layer, w[num_layers]) + b[num_layers]
```
1. 返回包含 MLP 網絡的`layer`對象:
```py
return layer
```
整個 MLP 函數的完整代碼如下:
```py
def mlp(x, num_inputs, num_outputs, num_layers, num_neurons):
w = []
b = []
for i in range(num_layers):
# weights
w.append(tf.Variable(tf.random_normal(
[num_inputs if i == 0 else num_neurons[i - 1],
num_neurons[i]]),
name="w_{0:04d}".format(i)
))
# biases
b.append(tf.Variable(tf.random_normal(
[num_neurons[i]]),
name="b_{0:04d}".format(i)
))
w.append(tf.Variable(tf.random_normal(
[num_neurons[num_layers - 1] if num_layers > 0 else num_inputs,
num_outputs]), name="w_out"))
b.append(tf.Variable(tf.random_normal([num_outputs]), name="b_out"))
# x is input layer
layer = x
# add hidden layers
for i in range(num_layers):
layer = tf.nn.relu(tf.matmul(layer, w[i]) + b[i])
# add output layer
layer = tf.matmul(layer, w[num_layers]) + b[num_layers]
return layer
```
輔助函數`mnist_batch_func()`為 MNIST 數據集包裝 TensorFlow 的批量函數,以提供下一批圖像:
```py
def mnist_batch_func(batch_size=100):
X_batch, Y_batch = mnist.train.next_batch(batch_size)
return [X_batch, Y_batch]
```
此函數不言自明。 TensorFlow 為 MNIST 數據集提供此函數;但是,對于其他數據集,我們可能必須編寫自己的批量函數。
輔助函數`tensorflow_classification()`訓練并評估模型。
1. `tensorflow_classification()` 函數有幾個輸入:
* * `n_epochs`是要運行的訓練循環的數量
* `n_batches`是應該運行每個循環中的訓練的隨機抽樣批次的數量
* `batch_size`是每批中的樣品數
* `batch_func`是`batch_size`并返回`X`和`Y`樣本批次的函數
* `model`是具有神經元的實際神經網絡或層
* `optimizer`是使用 TensorFlow 定義的優化函數
* `loss`是優化器優化參數的成本函數損失
* `accuracy_function`是計算準確率分數的函數
* `X_test`和`Y_test`是測試的數據集
1. 啟動 TensorFlow 會話以運行訓練循環:
```py
with tf.Session() as tfs:
tf.global_variables_initializer().run()
```
1. 運行`n_epoch`循環的訓練:
```py
for epoch in range(n_epochs):
```
1. 在每個循環中,取樣本集的`n_batches`數量并訓練模型,計算每批的損失,計算每個周期的平均損失:
```py
epoch_loss = 0.0
for batch in range(n_batches):
X_batch, Y_batch = batch_func(batch_size)
feed_dict = {x: X_batch, y: Y_batch}
_, batch_loss = tfs.run([optimizer, loss], feed_dict)
epoch_loss += batch_loss
average_loss = epoch_loss / n_batches
print("epoch: {0:04d} loss = {1:0.6f}".format(
epoch, average_loss))
```
1. 完成所有周期循環后,計算并打印用`accuracy_function`計算的精度分數:
```py
feed_dict = {x: X_test, y: Y_test}
accuracy_score = tfs.run(accuracy_function,
feed_dict=feed_dict)
print("accuracy={0:.8f}".format(accuracy_score))
```
`tensorflow_classification()`函數的完整代碼如下:
```py
def tensorflow_classification(n_epochs, n_batches,
batch_size, batch_func,
model, optimizer, loss, accuracy_function,
X_test, Y_test):
with tf.Session() as tfs:
tfs.run(tf.global_variables_initializer())
for epoch in range(n_epochs):
epoch_loss = 0.0
for batch in range(n_batches):
X_batch, Y_batch = batch_func(batch_size)
feed_dict = {x: X_batch, y: Y_batch}
_, batch_loss = tfs.run([optimizer, loss], feed_dict)
epoch_loss += batch_loss
average_loss = epoch_loss / n_batches
print("epoch: {0:04d} loss = {1:0.6f}".format(
epoch, average_loss))
feed_dict = {x: X_test, y: Y_test}
accuracy_score = tfs.run(accuracy_function, feed_dict=feed_dict)
print("accuracy={0:.8f}".format(accuracy_score))
```
現在讓我們定義輸入和輸出占位符,`x`和`y`以及其他超參數:
```py
# input images
x = tf.placeholder(dtype=tf.float32, name="x",
shape=[None, num_inputs])
# target output
y = tf.placeholder(dtype=tf.float32, name="y",
shape=[None, num_outputs])
num_layers = 0
num_neurons = []
learning_rate = 0.01
n_epochs = 50
batch_size = 100
n_batches = int(mnist.train.num_examples/batch_size)
```
參數如下所述:
* `num_layers`是隱藏層數。我們首先練習沒有隱藏層,只有輸入和輸出層。
* `num_neurons`是空列表,因為沒有隱藏層。
* `learning_rate`是 0.01,隨機選擇的小數。
* `num_epochs`代表 50 次迭代,以學習將輸入連接到輸出的唯一神經元的參數。
* `batch_size`保持在 100,這也是一個選擇問題。較大的批量大小不一定提供更高的好處。您可能需要探索不同的批量大小,以找到神經網絡的最佳批量大小。
* `n_batches`:批次數大致計算為示例數除以批次中的樣本數。
現在讓我們將所有內容放在一起,使用到目前為止定義的變量定義網絡,`loss`函數,`optimizer`函數和`accuracy`函數。
```py
model = mlp(x=x,
num_inputs=num_inputs,
num_outputs=num_outputs,
num_layers=num_layers,
num_neurons=num_neurons)
loss = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=y))
optimizer = tf.train.GradientDescentOptimizer(
learning_rate=learning_rate).minimize(loss)
predictions_check = tf.equal(tf.argmax(model, 1), tf.argmax(y, 1))
accuracy_function = tf.reduce_mean(tf.cast(predictions_check, tf.float32))
```
在這段代碼中,我們使用一個新的 tensorflow 函數來定義損失函數:
```py
tf.nn.softmax_cross_entropy_with_logits(logits=model, labels=y)
```
當使用`softmax_cross_entropy_with_logits()`函數時,請確保輸出未縮放且尚未通過`softmax`激活函數。 此函數在內部使用`softmax`來縮放輸出。
該函數計算模型之間的 softmax 熵(估計值`y`)和`y`的實際值。當輸出屬于一個類而不是一個類時,使用熵函數。在我們的示例中,圖像只能屬于其中一個數字。
有關此熵函數的更多信息,請參閱?[https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits](https://www.tensorflow.org/api_docs/python/tf/nn/softmax_cross_entropy_with_logits).
一旦定義了所有內容,運行 `tensorflow_classification` 函數來訓練和評估模型:
```py
tensorflow_classification(n_epochs=n_epochs,
n_batches=n_batches,
batch_size=batch_size,
batch_func=mnist_batch_func,
model = model,
optimizer = optimizer,
loss = loss,
accuracy_function = accuracy_function,
X_test = mnist.test.images,
Y_test = mnist.test.labels
)
```
我們從運行分類得到以下輸出:
```py
epoch: 0000 loss = 8.364567
epoch: 0001 loss = 4.347608
epoch: 0002 loss = 3.085622
epoch: 0003 loss = 2.468341
epoch: 0004 loss = 2.099220
epoch: 0005 loss = 1.853206
--- Epoch 06 to 45 output removed for brevity ---
epoch: 0046 loss = 0.684285
epoch: 0047 loss = 0.678972
epoch: 0048 loss = 0.673685
epoch: 0049 loss = 0.668717
accuracy=0.85720009
```
我們看到單個神經元網絡在 50 次迭代中緩慢地將損失從 8.3 降低到 0.66,最終得到幾乎 85%的準確率。對于這個具體的例子,這是非常糟糕的準確性,因為這只是使用 TensorFlow 進行分類使用 MLP 的演示。
我們使用更多層和神經元運行相同的代碼,并獲得以下準確性:
| 層數 | 每個隱藏層中的神經元數量 | 準確性 |
| --- | --- | --- |
| 0 | 0 | 0.857 |
| 1 | 8 | 0.616 |
| 2 | 256 | 0.936 |
因此,通過在每層添加兩行和 256 個神經元,我們將精度提高到 0.936。我們鼓勵您嘗試使用不同變量值的代碼來觀察它如何影響損失和準確性。
- TensorFlow 101
- 什么是 TensorFlow?
- TensorFlow 核心
- 代碼預熱 - Hello TensorFlow
- 張量
- 常量
- 操作
- 占位符
- 從 Python 對象創建張量
- 變量
- 從庫函數生成的張量
- 使用相同的值填充張量元素
- 用序列填充張量元素
- 使用隨機分布填充張量元素
- 使用tf.get_variable()獲取變量
- 數據流圖或計算圖
- 執行順序和延遲加載
- 跨計算設備執行圖 - CPU 和 GPU
- 將圖節點放置在特定的計算設備上
- 簡單放置
- 動態展示位置
- 軟放置
- GPU 內存處理
- 多個圖
- TensorBoard
- TensorBoard 最小的例子
- TensorBoard 詳情
- 總結
- TensorFlow 的高級庫
- TF Estimator - 以前的 TF 學習
- TF Slim
- TFLearn
- 創建 TFLearn 層
- TFLearn 核心層
- TFLearn 卷積層
- TFLearn 循環層
- TFLearn 正則化層
- TFLearn 嵌入層
- TFLearn 合并層
- TFLearn 估計層
- 創建 TFLearn 模型
- TFLearn 模型的類型
- 訓練 TFLearn 模型
- 使用 TFLearn 模型
- PrettyTensor
- Sonnet
- 總結
- Keras 101
- 安裝 Keras
- Keras 中的神經網絡模型
- 在 Keras 建立模型的工作流程
- 創建 Keras 模型
- 用于創建 Keras 模型的順序 API
- 用于創建 Keras 模型的函數式 API
- Keras 層
- Keras 核心層
- Keras 卷積層
- Keras 池化層
- Keras 本地連接層
- Keras 循環層
- Keras 嵌入層
- Keras 合并層
- Keras 高級激活層
- Keras 正則化層
- Keras 噪音層
- 將層添加到 Keras 模型
- 用于將層添加到 Keras 模型的順序 API
- 用于向 Keras 模型添加層的函數式 API
- 編譯 Keras 模型
- 訓練 Keras 模型
- 使用 Keras 模型進行預測
- Keras 的附加模塊
- MNIST 數據集的 Keras 序列模型示例
- 總結
- 使用 TensorFlow 進行經典機器學習
- 簡單的線性回歸
- 數據準備
- 構建一個簡單的回歸模型
- 定義輸入,參數和其他變量
- 定義模型
- 定義損失函數
- 定義優化器函數
- 訓練模型
- 使用訓練的模型進行預測
- 多元回歸
- 正則化回歸
- 套索正則化
- 嶺正則化
- ElasticNet 正則化
- 使用邏輯回歸進行分類
- 二分類的邏輯回歸
- 多類分類的邏輯回歸
- 二分類
- 多類分類
- 總結
- 使用 TensorFlow 和 Keras 的神經網絡和 MLP
- 感知機
- 多層感知機
- 用于圖像分類的 MLP
- 用于 MNIST 分類的基于 TensorFlow 的 MLP
- 用于 MNIST 分類的基于 Keras 的 MLP
- 用于 MNIST 分類的基于 TFLearn 的 MLP
- 使用 TensorFlow,Keras 和 TFLearn 的 MLP 總結
- 用于時間序列回歸的 MLP
- 總結
- 使用 TensorFlow 和 Keras 的 RNN
- 簡單循環神經網絡
- RNN 變種
- LSTM 網絡
- GRU 網絡
- TensorFlow RNN
- TensorFlow RNN 單元類
- TensorFlow RNN 模型構建類
- TensorFlow RNN 單元包裝器類
- 適用于 RNN 的 Keras
- RNN 的應用領域
- 用于 MNIST 數據的 Keras 中的 RNN
- 總結
- 使用 TensorFlow 和 Keras 的時間序列數據的 RNN
- 航空公司乘客數據集
- 加載 airpass 數據集
- 可視化 airpass 數據集
- 使用 TensorFlow RNN 模型預處理數據集
- TensorFlow 中的簡單 RNN
- TensorFlow 中的 LSTM
- TensorFlow 中的 GRU
- 使用 Keras RNN 模型預處理數據集
- 使用 Keras 的簡單 RNN
- 使用 Keras 的 LSTM
- 使用 Keras 的 GRU
- 總結
- 使用 TensorFlow 和 Keras 的文本數據的 RNN
- 詞向量表示
- 為 word2vec 模型準備數據
- 加載和準備 PTB 數據集
- 加載和準備 text8 數據集
- 準備小驗證集
- 使用 TensorFlow 的 skip-gram 模型
- 使用 t-SNE 可視化單詞嵌入
- keras 的 skip-gram 模型
- 使用 TensorFlow 和 Keras 中的 RNN 模型生成文本
- TensorFlow 中的 LSTM 文本生成
- Keras 中的 LSTM 文本生成
- 總結
- 使用 TensorFlow 和 Keras 的 CNN
- 理解卷積
- 了解池化
- CNN 架構模式 - LeNet
- 用于 MNIST 數據的 LeNet
- 使用 TensorFlow 的用于 MNIST 的 LeNet CNN
- 使用 Keras 的用于 MNIST 的 LeNet CNN
- 用于 CIFAR10 數據的 LeNet
- 使用 TensorFlow 的用于 CIFAR10 的 ConvNets
- 使用 Keras 的用于 CIFAR10 的 ConvNets
- 總結
- 使用 TensorFlow 和 Keras 的自編碼器
- 自編碼器類型
- TensorFlow 中的棧式自編碼器
- Keras 中的棧式自編碼器
- TensorFlow 中的去噪自編碼器
- Keras 中的去噪自編碼器
- TensorFlow 中的變分自編碼器
- Keras 中的變分自編碼器
- 總結
- TF 服務:生產中的 TensorFlow 模型
- 在 TensorFlow 中保存和恢復模型
- 使用保護程序類保存和恢復所有圖變量
- 使用保護程序類保存和恢復所選變量
- 保存和恢復 Keras 模型
- TensorFlow 服務
- 安裝 TF 服務
- 保存 TF 服務的模型
- 提供 TF 服務模型
- 在 Docker 容器中提供 TF 服務
- 安裝 Docker
- 為 TF 服務構建 Docker 鏡像
- 在 Docker 容器中提供模型
- Kubernetes 中的 TensorFlow 服務
- 安裝 Kubernetes
- 將 Docker 鏡像上傳到 dockerhub
- 在 Kubernetes 部署
- 總結
- 遷移學習和預訓練模型
- ImageNet 數據集
- 再訓練或微調模型
- COCO 動物數據集和預處理圖像
- TensorFlow 中的 VGG16
- 使用 TensorFlow 中預訓練的 VGG16 進行圖像分類
- TensorFlow 中的圖像預處理,用于預訓練的 VGG16
- 使用 TensorFlow 中的再訓練的 VGG16 進行圖像分類
- Keras 的 VGG16
- 使用 Keras 中預訓練的 VGG16 進行圖像分類
- 使用 Keras 中再訓練的 VGG16 進行圖像分類
- TensorFlow 中的 Inception v3
- 使用 TensorFlow 中的 Inception v3 進行圖像分類
- 使用 TensorFlow 中的再訓練的 Inception v3 進行圖像分類
- 總結
- 深度強化學習
- OpenAI Gym 101
- 將簡單的策略應用于 cartpole 游戲
- 強化學習 101
- Q 函數(在模型不可用時學習優化)
- RL 算法的探索與開發
- V 函數(模型可用時學習優化)
- 強化學習技巧
- 強化學習的樸素神經網絡策略
- 實現 Q-Learning
- Q-Learning 的初始化和離散化
- 使用 Q-Table 進行 Q-Learning
- Q-Network 或深 Q 網絡(DQN)的 Q-Learning
- 總結
- 生成性對抗網絡
- 生成性對抗網絡 101
- 建立和訓練 GAN 的最佳實踐
- 使用 TensorFlow 的簡單的 GAN
- 使用 Keras 的簡單的 GAN
- 使用 TensorFlow 和 Keras 的深度卷積 GAN
- 總結
- 使用 TensorFlow 集群的分布式模型
- 分布式執行策略
- TensorFlow 集群
- 定義集群規范
- 創建服務器實例
- 定義服務器和設備之間的參數和操作
- 定義并訓練圖以進行異步更新
- 定義并訓練圖以進行同步更新
- 總結
- 移動和嵌入式平臺上的 TensorFlow 模型
- 移動平臺上的 TensorFlow
- Android 應用中的 TF Mobile
- Android 上的 TF Mobile 演示
- iOS 應用中的 TF Mobile
- iOS 上的 TF Mobile 演示
- TensorFlow Lite
- Android 上的 TF Lite 演示
- iOS 上的 TF Lite 演示
- 總結
- R 中的 TensorFlow 和 Keras
- 在 R 中安裝 TensorFlow 和 Keras 軟件包
- R 中的 TF 核心 API
- R 中的 TF 估計器 API
- R 中的 Keras API
- R 中的 TensorBoard
- R 中的 tfruns 包
- 總結
- 調試 TensorFlow 模型
- 使用tf.Session.run()獲取張量值
- 使用tf.Print()打印張量值
- 用tf.Assert()斷言條件
- 使用 TensorFlow 調試器(tfdbg)進行調試
- 總結
- 張量處理單元