(三) tensorflow--alexnet訓(xùn)練mnist

Alexnet是2012年的一個(gè)非常出色的神經(jīng)網(wǎng)絡(luò),是2012年圖像識(shí)別大賽的冠軍。該模型一共有8層,前五層為卷基層,用于提取圖像特征,后三層為全接連層,用于對(duì)圖像進(jìn)行分類。
image

該模型的主要特點(diǎn)如下:
1.提出了ReLU激勵(lì)函數(shù),可以減少梯度消失的風(fēng)險(xiǎn);
2.池化層用于特征降維
3.局部歸一化處理(LRN)
下面還是在MNIST數(shù)據(jù)集上,使用alexnet網(wǎng)絡(luò)進(jìn)行訓(xùn)練,得到分類的結(jié)果。

導(dǎo)入數(shù)據(jù)

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

初始化參數(shù)

leaning_rate = 0.001
num_steps = 5000
batch_size = 128
display_step = 10
num_input = 784
num_classes = 10
dropout = 0.75

定義輸入輸出

X = tf.placeholder(tf.float32,[None,num_input])
Y = tf.placeholder(tf.float32,[None,num_classes])
keep_prob = tf.placeholder(tf.float32)

定義卷積、池化函數(shù)

def conv2d(x,W,b,strides=1):
    x = tf.nn.conv2d(x,W,strides=[1,strides,strides,1],padding='SAME')
    x = tf.nn.bias_add(x,b)
    return tf.nn.relu(x)
def maxpool2d(x,k=2):
    return tf.nn.max_pool(x,ksize=[1,k,k,1],strides=[1,k,k,1],padding='SAME')

定義網(wǎng)絡(luò)架構(gòu)

def alex_net(x,weights,bias,dropout):
    x = tf.reshape(x,[-1,28,28,1])
    conv1 = conv2d(x,weights['wc1'],bias['bc1'])
    conv1 = maxpool2d(conv1,k=2)
    conv2 = conv2d(conv1, weights['wc2'], bias['bc2'])
    conv2 = maxpool2d(conv2, k=2)
    conv3 = conv2d(conv2, weights['wc3'], bias['bc3'])
    conv3 = maxpool2d(conv3, k=2)
    fc1 = tf.reshape(conv3,[-1,weights['wd1'].get_shape().as_list()[0]])
    fc1 = tf.nn.relu(tf.matmul(fc1,weights['wd1'])+bias['bd1'],name='fc1')
    fc2 = tf.nn.relu(tf.matmul(fc1,weights['wd2'])+bias['bd2'],name='fc2')
    out = tf.matmul(fc2,weights['out']+bias['out'])
    return out
weights={
    'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64])),
    'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128])),
    'wc3': tf.Variable(tf.random_normal([3, 3, 128, 256])),
    'wd1': tf.Variable(tf.random_normal([4*4*256, 1024])),
    'wd2': tf.Variable(tf.random_normal([1024, 1024])),
    'out': tf.Variable(tf.random_normal([1024, num_classes]))
}
bias={
    'bc1': tf.Variable(tf.random_normal([64])),
    'bc2': tf.Variable(tf.random_normal([128])),
    'bc3': tf.Variable(tf.random_normal([256])),
    'bd1': tf.Variable(tf.random_normal([1024])),
    'bd2': tf.Variable(tf.random_normal([1024])),
    'out': tf.Variable(tf.random_normal([num_classes]))
}

定義損失函數(shù)和梯度下降方式

loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(
    logits=logits, labels=Y))
optimizer = tf.train.AdamOptimizer(learning_rate=leaning_rate)
train_op = optimizer.minimize(loss_op)

模型評(píng)價(jià)

correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_p

訓(xùn)練模型

init = tf.global_variables_initializer()

# Start training
with tf.Session() as sess:

    # Run the initializer
    sess.run(init)

    for step in range(1, num_steps+1):
        batch_x, batch_y = mnist.train.next_batch(batch_size)
        # Run optimization op (backprop)
        sess.run(train_op, feed_dict={X: batch_x, Y: batch_y, keep_prob: 0.8})
        if step % display_step == 0 :
            # Calculate batch loss and accuracy
            loss, acc = sess.run([loss_op, accuracy], feed_dict={X: batch_x,
                                                                 Y: batch_y,
                                                                 keep_prob: 1.0})
            print("Step " + str(step) + ", Minibatch Loss= " + \
                  "{:.4f}".format(loss) + ", Training Accuracy= " + \
                  "{:.3f}".format(acc))

    print("Optimization Finished!")

    # Calculate accuracy for 256 MNIST test images
    print("Testing Accuracy:", \
        sess.run(accuracy, feed_dict={X: mnist.test.images[:256],
                                      Y: mnist.test.labels[:256],
                                      keep_prob: 1.0}))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容