TensorFlow MNIST AlexNet
生活随笔
收集整理的這篇文章主要介紹了
TensorFlow MNIST AlexNet
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原始的AlexNet用來處理277*277*3的數據集, 并且采用5層卷積,3層全連接層來處理圖像分類。
具體結構和參數信息見
http://blog.csdn.net/chenhaifeng2016/article/details/72758053
這里在前面MNIST CNN LeNet的基礎上,借鑒AlexNet的思想做進一步優化。(非標準AlexNet模型)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data#加載數據 mnist = input_data.read_data_sets('MNIST_data', one_hot=True)sess = tf.InteractiveSession()#訓練數據 inputs = tf.placeholder(tf.float32, shape=[None, 784]) #訓練標簽數據 labels = tf.placeholder(tf.float32, shape=[None, 10]) #dropout keep_prob = tf.placeholder(tf.float32)#把inputs更改為4維張量,第1維代表樣本數量,第2維和第3維代表圖像長寬, 第4維代表圖像通道數, 1表示黑白 x = tf.reshape(inputs, [-1,28,28,1])#第一層卷積 conv1_weights = tf.Variable(tf.random_normal([3, 3, 1, 64])) #卷積核大小為3*3, 當前層深度為1, 過濾器深度為64 #卷積 conv1 = tf.nn.conv2d(x, conv1_weights, strides=[1, 1, 1, 1], padding='SAME') #移動步長為1, 使用全0填充 conv1_biases = tf.Variable(tf.random_normal([64])) #激活函數Relu去線性化 relu1 = tf.nn.relu( tf.nn.bias_add(conv1, conv1_biases) ) #最大池化 pool1 = tf.nn.max_pool(relu1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') #池化層過濾器的大小為2*2, 移動步長為2,使用全0填充 #規范化 norm1 = tf.nn.lrn(pool1, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75) norm1 = tf.nn.dropout(norm1, keep_prob) print(norm1.shape) #14*14*64#第二層卷積 conv2_weights = tf.Variable(tf.random_normal([3, 3, 64, 128])) #卷積核大小為3*3, 當前層深度為64, 過濾器深度為128 conv2 = tf.nn.conv2d(norm1, conv2_weights, strides=[1, 1, 1, 1], padding='SAME') #移動步長為1, 使用全0填充 conv2_biases = tf.Variable(tf.random_normal([128])) relu2 = tf.nn.relu( tf.nn.bias_add(conv2, conv2_biases) ) pool2 = tf.nn.max_pool(relu2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') norm2 = tf.nn.lrn(pool2, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75) norm2 = tf.nn.dropout(norm2, keep_prob) print(norm2.shape) #7*7*128#第三層卷積 conv3_weights = tf.Variable(tf.random_normal([3, 3, 128, 256])) #卷積核大小為3*3, 當前層深度為128, 過濾器深度為256 conv3 = tf.nn.conv2d(norm2, conv3_weights, strides=[1, 1, 1, 1], padding='SAME') #移動步長為1, 使用全0填充 conv3_biases = tf.Variable(tf.random_normal([256])) relu3 = tf.nn.relu( tf.nn.bias_add(conv3, conv3_biases) ) pool3 = tf.nn.max_pool(relu3, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') norm3 = tf.nn.lrn(pool3, depth_radius=4, bias=1.0, alpha=0.001/9.0, beta=0.75) norm3 = tf.nn.dropout(norm3, keep_prob) print(norm3.shape) #4*4*256#全連接層 1 fc1_weights = tf.Variable(tf.random_normal([4*4*256, 1024])) fc1_biases = tf.Variable(tf.random_normal([1024])) fc1 = tf.reshape(norm3, [ -1, fc1_weights.get_shape().as_list()[0] ] ) fc1 = tf.add(tf.matmul(fc1, fc1_weights), fc1_biases) fc1 = tf.nn.relu(fc1)#全連接層 2 fc2_weights = tf.Variable(tf.random_normal([1024, 1024])) fc2_biases = tf.Variable(tf.random_normal([1024])) fc2 = tf.reshape(fc1, [ -1, fc2_weights.get_shape().as_list()[0] ] ) fc2 = tf.add(tf.matmul(fc2, fc2_weights), fc2_biases) fc2 = tf.nn.relu(fc2)#輸出層 out_weights = tf.Variable(tf.random_normal([1024, 10])) out_biases = tf.Variable(tf.random_normal([10])) pred = tf.add(tf.matmul(fc2, out_weights), out_biases)#定義交叉熵損失函數 cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=pred, labels=labels))#選擇優化器 train_op = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)#評估函數 correct_prediction = tf.equal(tf.argmax(pred,1), tf.argmax(labels,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))#訓練模型 sess.run(tf.global_variables_initializer())for i in range(10000):batch = mnist.train.next_batch(100)sess.run(train_op, feed_dict={inputs: batch[0], labels: batch[1], keep_prob: 0.75}) # 訓練階段使用75%的Dropoutif i % 100 == 0:train_accuracy = sess.run(accuracy, feed_dict={inputs:batch[0], labels: batch[1], keep_prob: 1.0}) #評估階段不使用Dropoutprint("step %d, training accuracy %g" % (i, train_accuracy))#評估模型 #只使用256個測試數據,機器太差, 跑10000內存溢出了 print("test accuracy %g" % sess.run(accuracy, feed_dict={inputs: mnist.test.images[:256], labels: mnist.test.labels[:256], keep_prob: 1.0})) #評估階段不使用Dropout總結
以上是生活随笔為你收集整理的TensorFlow MNIST AlexNet的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Keras MNIST
- 下一篇: 区块链技术的五大颠覆性价值