一、卷積神經(jīng)網(wǎng)絡(luò)CNN簡介
站在用戶的角度思考問題,與客戶深入溝通,找到平江網(wǎng)站設(shè)計(jì)與平江網(wǎng)站推廣的解決方案,憑借多年的經(jīng)驗(yàn),讓設(shè)計(jì)與互聯(lián)網(wǎng)技術(shù)結(jié)合,創(chuàng)造個(gè)性化、用戶體驗(yàn)好的作品,建站類型包括:做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣、申請(qǐng)域名、網(wǎng)頁空間、企業(yè)郵箱。業(yè)務(wù)覆蓋平江地區(qū)。卷積神經(jīng)網(wǎng)絡(luò)(ConvolutionalNeuralNetwork,CNN)最初是為解決圖像識(shí)別等問題設(shè)計(jì)的,CNN現(xiàn)在的應(yīng)用已經(jīng)不限于圖像和視頻,也可用于時(shí)間序列信號(hào),比如音頻信號(hào)和文本數(shù)據(jù)等。CNN作為一個(gè)深度學(xué)習(xí)架構(gòu)被提出的最初訴求是降低對(duì)圖像數(shù)據(jù)預(yù)處理的要求,避免復(fù)雜的特征工程。在卷積神經(jīng)網(wǎng)絡(luò)中,第一個(gè)卷積層會(huì)直接接受圖像像素級(jí)的輸入,每一層卷積(濾波器)都會(huì)提取數(shù)據(jù)中最有效的特征,這種方法可以提取到圖像中最基礎(chǔ)的特征,而后再進(jìn)行組合和抽象形成更高階的特征,因此CNN在理論上具有對(duì)圖像縮放、平移和旋轉(zhuǎn)的不變性。
卷積神經(jīng)網(wǎng)絡(luò)CNN的要點(diǎn)就是局部連接(LocalConnection)、權(quán)值共享(WeightsSharing)和池化層(Pooling)中的降采樣(Down-Sampling)。其中,局部連接和權(quán)值共享降低了參數(shù)量,使訓(xùn)練復(fù)雜度大大下降并減輕了過擬合。同時(shí)權(quán)值共享還賦予了卷積網(wǎng)絡(luò)對(duì)平移的容忍性,池化層降采樣則進(jìn)一步降低了輸出參數(shù)量并賦予模型對(duì)輕度形變的容忍性,提高了模型的泛化能力??梢园丫矸e層卷積操作理解為用少量參數(shù)在圖像的多個(gè)位置上提取相似特征的過程。
更多請(qǐng)參見:深度學(xué)習(xí)之卷積神經(jīng)網(wǎng)絡(luò)CNN
二、TensorFlow代碼實(shí)現(xiàn)
#!/usr/bin/env python2 # -*- coding: utf-8 -*- """ Created on Thu Mar 9 22:01:46 2017 @author: marsjhao """ 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() def weight_variable(shape): initial = tf.truncated_normal(shape, stddev=0.1) #標(biāo)準(zhǔn)差為0.1的正態(tài)分布 return tf.Variable(initial) def bias_variable(shape): initial = tf.constant(0.1, shape=shape) #偏差初始化為0.1 return tf.Variable(initial) def conv2d(x, W): return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME') def max_pool_2x2(x): return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') x = tf.placeholder(tf.float32, [None, 784]) y_ = tf.placeholder(tf.float32, [None, 10]) # -1代表先不考慮輸入的圖片例子多少這個(gè)維度,1是channel的數(shù)量 x_image = tf.reshape(x, [-1, 28, 28, 1]) keep_prob = tf.placeholder(tf.float32) # 構(gòu)建卷積層1 W_conv1 = weight_variable([5, 5, 1, 32]) # 卷積核5*5,1個(gè)channel,32個(gè)卷積核,形成32個(gè)featuremap b_conv1 = bias_variable([32]) # 32個(gè)featuremap的偏置 h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 用relu非線性處理 h_pool1 = max_pool_2x2(h_conv1) # pooling池化 # 構(gòu)建卷積層2 W_conv2 = weight_variable([5, 5, 32, 64]) # 注意這里channel值是32 b_conv2 = bias_variable([64]) h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2) h_pool2 = max_pool_2x2(h_conv2) # 構(gòu)建全連接層1 W_fc1 = weight_variable([7*7*64, 1024]) b_fc1 = bias_variable([1024]) h_pool3 = tf.reshape(h_pool2, [-1, 7*7*64]) h_fc1 = tf.nn.relu(tf.matmul(h_pool3, W_fc1) + b_fc1) h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob) # 構(gòu)建全連接層2 W_fc2 = weight_variable([1024, 10]) b_fc2 = bias_variable([10]) y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1])) train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy) correct_prediction = tf.equal(tf.arg_max(y_conv, 1), tf.arg_max(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) tf.global_variables_initializer().run() for i in range(20001): batch = mnist.train.next_batch(50) if i % 100 == 0: train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_:batch[1], keep_prob: 1.0}) print("step %d, training accuracy %g" %(i, train_accuracy)) train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob:0.5}) print("test accuracy %g" %accuracy.eval(feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0}))
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。