步驟和上一篇一樣,step0~step5
【step0】
準(zhǔn)備工作:下載并檢測(cè)數(shù)據(jù)集判莉,tensorflow.tutorial自帶minist數(shù)據(jù)集,如果下載不成功,可以網(wǎng)頁(yè)手動(dòng)下載后放到代碼已經(jīng)為你創(chuàng)建的文件夾里带迟。
mnist = input_data.read_data_sets('./mnist', one_hot=True)
test_x = mnist.test.images[:2000]
test_y = mnist.test.labels[:2000]
# plot one example,to see if it's right
print(mnist.train.images.shape) # (55000, 28 * 28)
print(mnist.train.labels.shape) # (55000, 10)
plt.imshow(mnist.train.images[0].reshape((28, 28)), cmap='gray')
plt.title('%i' % np.argmax(mnist.train.labels[0]))
plt.show()
別忘了定義batch_size和學(xué)習(xí)速率
BATCH_SIZE = 50
LR = 0.001
【step1】
定義輸入:batchsize不確定,所以用到None囱桨,-1等參數(shù)仓犬,見(jiàn)tf.placeholder()函數(shù)和tf.reshape()函數(shù),注意經(jīng)過(guò)reshape后的參數(shù)image才是網(wǎng)絡(luò)真正的輸入
tf_x = tf.placeholder(tf.float32, [None, 28*28]) / 255
image = tf.reshape(tf_x, [-1, 28, 28, 1]) # (batch, height, width, channel)
tf_y = tf.placeholder(tf.int32, [None, 10]) # input y
【step2】
定義學(xué)習(xí)參數(shù):最重要的一步舍肠,學(xué)習(xí)參數(shù)是我們的卷積核參數(shù)搀继,很經(jīng)典的結(jié)構(gòu),卷+池+卷+池+全
網(wǎng)絡(luò)結(jié)構(gòu)如圖
代碼如下:
conv1 = tf.layers.conv2d( # shape (28, 28, 1)
inputs=image,
filters=16,
kernel_size=5,
strides=1,
padding='same',
activation=tf.nn.relu
) # -> (28, 28, 16)
pool1 = tf.layers.max_pooling2d(
conv1,
pool_size=2,
strides=2,
) # -> (14, 14, 16)
conv2 = tf.layers.conv2d(pool1, 32, 5, 1, 'same', activation=tf.nn.relu) # -> (14, 14, 32)
pool2 = tf.layers.max_pooling2d(conv2, 2, 2) # -> (7, 7, 32)
flat = tf.reshape(pool2, [-1, 7*7*32]) # -> (7*7*32, )
output = tf.layers.dense(flat, 10) # output layer
【step3】
定義學(xué)習(xí)方法:最重要的是新學(xué)到的softmax函數(shù)翠语,它把網(wǎng)絡(luò)的輸出叽躯,10維轉(zhuǎn)化成1維,其原理是肌括,在這10維度中找到維度最大的那一維(說(shuō)明落到該維度可能性最大)点骑,并因此得到一個(gè)1維參數(shù),就是維度序號(hào)谍夭。
Adam優(yōu)化器適合解決更復(fù)雜的問(wèn)題
loss = tf.losses.softmax_cross_entropy(onehot_labels=tf_y, logits=output) # compute cost
train_op = tf.train.AdamOptimizer(LR).minimize(loss)
accuracy = tf.metrics.accuracy( # return (acc, update_op), and create 2 local variables
labels=tf.argmax(tf_y, axis=1), predictions=tf.argmax(output, axis=1),)[1]
【step4】
初始化模型并訓(xùn)練:比線性回歸的訓(xùn)練回合要多
sess = tf.Session()
init_op = tf.group(tf.global_variables_initializer(), tf.local_variables_initializer()) # the local var is for accuracy_op
sess.run(init_op) # initialize var in graph
for step in range(600):
b_x, b_y = mnist.train.next_batch(BATCH_SIZE)
_, loss_ = sess.run([train_op, loss], {tf_x: b_x, tf_y: b_y})
if step % 50 == 0:
accuracy_, flat_representation = sess.run([accuracy, flat], {tf_x: test_x, tf_y: test_y})
print('Step:', step, '| train loss: %.4f' % loss_, '| test accuracy: %.2f' % accuracy_)
【step5】
測(cè)試:測(cè)試模型我們?cè)趕tep0就確定好了喔
test_output = sess.run(output, {tf_x: test_x[:10]})
pred_y = np.argmax(test_output, 1)
print(pred_y, 'prediction number')
print(np.argmax(test_y[:10], 1), 'real number')