前一篇文章tensorflow入門應(yīng)用方法——線性回歸和邏輯回歸中,主要闡述了應(yīng)用tensorflow搭建線性回歸和邏輯回歸的相關(guān)代碼刚盈。其中胎许,線性回歸和邏輯回歸模型是一個(gè)簡單的單層網(wǎng)絡(luò)找前。因此丈屹,本文較為深入闡述應(yīng)用tensorflow搭建擁有兩層隱藏層的網(wǎng)絡(luò)方法流程调俘。
具體步驟
- 初始化每層的神經(jīng)元數(shù)量 需要定義出每層的輸出神經(jīng)元數(shù)量
- 輸入輸出初始化 一般用placeholder占位符定義x,y
- 網(wǎng)絡(luò)參數(shù)初始化 w權(quán)值和bias隨機(jī)初始化范圍(-1,1),bias也可初始化為0
- 前向計(jì)算 需要選擇激活函數(shù)
- 損失函數(shù)和最優(yōu)化定義 可以選擇不同的損失函數(shù)和優(yōu)化算法
- 預(yù)測精度函數(shù)定義
- 初始化所有變量 init = tf.global_variables_initializer()
- 迭代運(yùn)算
深度網(wǎng)絡(luò)代碼實(shí)現(xiàn)
首先加載mnist數(shù)據(jù)集用于模型訓(xùn)練
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 加載數(shù)據(jù)
mnist = input_data.read_data_sets('data/', one_hot=True)
然后旺垒,開始搭建深度模型具體步驟
# 神經(jīng)元個(gè)數(shù)初始化
n_hidden_1 = 256
n_hidden_2 = 128
n_input = 784 # 28*28*1的輸入
n_class = 10
# 輸入和輸出
x = tf.placeholder('float', [None, n_input])
y = tf.placeholder('float', [None, n_class])
# 網(wǎng)絡(luò)參數(shù)初始化
stddev = 0.1 # 指定高斯分布方差
weights = {
'w1': tf.Variable(tf.random_normal([n_input, n_hidden_1], stddev=stddev)),
'w2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2], stddev=stddev)),
'out': tf.Variable(tf.random_normal([n_hidden_2, n_class], stddev=stddev))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'out': tf.Variable(tf.random_normal([n_class]))
}
print('network ready')
# 訓(xùn)練網(wǎng)絡(luò)
# 前向計(jì)算
def multilayer_perception(_X, _weight, _biases):
layer1 = tf.nn.sigmoid(tf.add(tf.matmul(_X, _weight['w1']), _biases['b1']))
layer2 = tf.nn.sigmoid(tf.add(tf.matmul(layer1, _weight['w2']), _biases['b2']))
return (tf.matmul(layer2, _weight['out']) +_biases['out'])
pred = multilayer_perception(x, weights, biases)
# 損失函數(shù)和最優(yōu)化
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=y))
optm = tf.train.GradientDescentOptimizer(learning_rate=0.01).minimize(cost)
# 計(jì)算精度
corr = tf.equal(tf.arg_max(pred, 1), tf.arg_max(y, 1))
accr = tf.reduce_mean(tf.cast(corr, 'float'))
# 初始化操作
init = tf.global_variables_initializer()
print('function ready')
# 迭代計(jì)算
training_epochs = 20
batch_size = 100
display_step = 4
sess = tf.Session()
sess.run(init)
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(mnist.train.num_examples/batch_size)
for i in range(total_batch):
batch_xs, batch_ys = mnist.train.next_batch(batch_size)
feeds = {x: batch_xs, y: batch_ys}
sess.run(optm, feed_dict=feeds)
avg_cost += sess.run(cost, feed_dict=feeds)/total_batch
# 顯示結(jié)果
if (epoch+1) % display_step == 0:
print('Epoch: %03d/%03d cost: %.9f' % (epoch+1, training_epochs, avg_cost))
feeds = {x: batch_xs, y: batch_ys}
train_acc = sess.run(accr, feed_dict=feeds)
print('Train accuracy: %.3f' % train_acc)
feeds = {x: mnist.test.images, y: mnist.test.labels}
test_acc = sess.run(accr, feed_dict=feeds)
print('Test accuracy: %.3f' % test_acc)
print('optmization finished')
訓(xùn)練模型時(shí)主要使用梯度下降算法彩库,迭代20次,每4步顯示一次結(jié)果:
Epoch: 004/020 cost: 1.835647699
Train accuracy: 0.720
Test accuracy: 0.682
Epoch: 008/020 cost: 0.946993933
Train accuracy: 0.780
Test accuracy: 0.804
Epoch: 012/020 cost: 0.642496496
Train accuracy: 0.890
Test accuracy: 0.849
Epoch: 016/020 cost: 0.519645967
Train accuracy: 0.800
Test accuracy: 0.869
Epoch: 020/020 cost: 0.452667181
Train accuracy: 0.880
Test accuracy: 0.885
optmization finished
總結(jié)
在本文中先蒋,經(jīng)過20次的迭代訓(xùn)練骇钦,模型在訓(xùn)練集上的精度為88.0%,測試集上的精度為88.5%竞漾。