91欧美超碰AV自拍|国产成年人性爱视频免费看|亚洲 日韩 欧美一厂二区入|人人看人人爽人人操aV|丝袜美腿视频一区二区在线看|人人操人人爽人人爱|婷婷五月天超碰|97色色欧美亚州A√|另类A√无码精品一级av|欧美特级日韩特级

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

【連載】深度學(xué)習(xí)筆記12:卷積神經(jīng)網(wǎng)絡(luò)的Tensorflow實(shí)現(xiàn)

人工智能實(shí)訓(xùn)營(yíng) ? 2018-10-30 18:50 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

在上一講中,我們學(xué)習(xí)了如何利用 numpy 手動(dòng)搭建卷積神經(jīng)網(wǎng)絡(luò)。但在實(shí)際的圖像識(shí)別中,使用 numpy 去手寫 CNN 未免有些吃力不討好。在 DNN 的學(xué)習(xí)中,我們也是在手動(dòng)搭建之后利用 Tensorflow 去重新實(shí)現(xiàn)一遍,一來為了能夠?qū)ι窠?jīng)網(wǎng)絡(luò)的傳播機(jī)制能夠理解更加透徹,二來也是為了更加高效使用開源框架快速搭建起深度學(xué)習(xí)項(xiàng)目。本節(jié)就繼續(xù)和大家一起學(xué)習(xí)如何利用 Tensorflow 搭建一個(gè)卷積神經(jīng)網(wǎng)絡(luò)。

我們繼續(xù)以 NG 課題組提供的 sign 手勢(shì)數(shù)據(jù)集為例,學(xué)習(xí)如何通過 Tensorflow 快速搭建起一個(gè)深度學(xué)習(xí)項(xiàng)目。數(shù)據(jù)集標(biāo)簽共有零到五總共 6 類標(biāo)簽,示例如下:


先對(duì)數(shù)據(jù)進(jìn)行簡(jiǎn)單的預(yù)處理并查看訓(xùn)練集和測(cè)試集維度:

X_train = X_train_orig/255.
X_test = X_test_orig/255.
Y_train = convert_to_one_hot(Y_train_orig, 6).T Y_test = convert_to_one_hot(Y_test_orig, 6).T
print ("number of training examples = " + str(X_train.shape[0]))
print ("number of test examples = " + str(X_test.shape[0]))
print ("X_train shape: " + str(X_train.shape))
print ("Y_train shape: " + str(Y_train.shape))
print ("X_test shape: " + str(X_test.shape))
print ("Y_test shape: " + str(Y_test.shape))

640?wx_fmt=png
可見我們總共有 1080 張 64643 訓(xùn)練集圖像,120 張 64643 的測(cè)試集圖像,共有 6 類標(biāo)簽。下面我們開始搭建過程。

創(chuàng)建 placeholder

首先需要為訓(xùn)練集預(yù)測(cè)變量和目標(biāo)變量創(chuàng)建占位符變量 placeholder ,定義創(chuàng)建占位符變量函數(shù):

def create_placeholders(n_H0, n_W0, n_C0, n_y):  
""" Creates the placeholders for the tensorflow session. Arguments: n_H0 -- scalar, height of an input image n_W0 -- scalar, width of an input image n_C0 -- scalar, number of channels of the input n_y -- scalar, number of classes Returns: X -- placeholder for the data input, of shape [None, n_H0, n_W0, n_C0] and dtype "float" Y -- placeholder for the input labels, of shape [None, n_y] and dtype "float" """ X = tf.placeholder(tf.float32, shape=(None, n_H0, n_W0, n_C0), name='X') Y = tf.placeholder(tf.float32, shape=(None, n_y), name='Y')
return X, Y
參數(shù)初始化

然后需要對(duì)濾波器權(quán)值參數(shù)進(jìn)行初始化:

def initialize_parameters():  
""" Initializes weight parameters to build a neural network with tensorflow. Returns: parameters -- a dictionary of tensors containing W1, W2 """ tf.set_random_seed(1) W1 = tf.get_variable("W1", [4,4,3,8], initializer = tf.contrib.layers.xavier_initializer(seed = 0)) W2 = tf.get_variable("W2", [2,2,8,16], initializer = tf.contrib.layers.xavier_initializer(seed = 0)) parameters = {"W1": W1,
"W2": W2}
return parameters
執(zhí)行卷積網(wǎng)絡(luò)的前向傳播過程

640?wx_fmt=png
前向傳播過程如下所示:
CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED


可見我們要搭建的是一個(gè)典型的 CNN 過程,經(jīng)過兩次的卷積-relu激活-最大池化,然后展開接上一個(gè)全連接層。利用
Tensorflow 搭建上述傳播過程如下:

def forward_propagation(X, parameters):  
""" Implements the forward propagation for the model Arguments: X -- input dataset placeholder, of shape (input size, number of examples) parameters -- python dictionary containing your parameters "W1", "W2" the shapes are given in initialize_parameters Returns: Z3 -- the output of the last LINEAR unit """ # Retrieve the parameters from the dictionary "parameters" W1 = parameters['W1'] W2 = parameters['W2']
# CONV2D: stride of 1, padding 'SAME' Z1 = tf.nn.conv2d(X,W1, strides = [1,1,1,1], padding = 'SAME')
# RELU A1 = tf.nn.relu(Z1)
# MAXPOOL: window 8x8, sride 8, padding 'SAME' P1 = tf.nn.max_pool(A1, ksize = [1,8,8,1], strides = [1,8,8,1], padding = 'SAME')
# CONV2D: filters W2, stride 1, padding 'SAME' Z2 = tf.nn.conv2d(P1,W2, strides = [1,1,1,1], padding = 'SAME')
# RELU A2 = tf.nn.relu(Z2)
# MAXPOOL: window 4x4, stride 4, padding 'SAME' P2 = tf.nn.max_pool(A2, ksize = [1,4,4,1], strides = [1,4,4,1], padding = 'SAME')
# FLATTEN P2 = tf.contrib.layers.flatten(P2) Z3 = tf.contrib.layers.fully_connected(P2, 6, activation_fn = None)
return Z3
計(jì)算當(dāng)前損失

Tensorflow 中計(jì)算損失函數(shù)非常簡(jiǎn)單,一行代碼即可:

def compute_cost(Z3, Y):  
""" Computes the cost Arguments: Z3 -- output of forward propagation (output of the last LINEAR unit), of shape (6, number of examples) Y -- "true" labels vector placeholder, same shape as Z3 Returns: cost - Tensor of the cost function """ cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=Z3, labels=Y))
return cost

定義好上述過程之后,就可以封裝整體的訓(xùn)練過程模型??赡苣銜?huì)問為什么沒有反向傳播,這里需要注意的是 Tensorflow 幫助我們自動(dòng)封裝好了反向傳播過程,無需我們?cè)俅味x,在實(shí)際搭建過程中我們只需將前向傳播的網(wǎng)絡(luò)結(jié)構(gòu)定義清楚即可。

封裝模型
def model(X_train, Y_train, X_test, Y_test, learning_rate = 0.009,
     num_epochs = 100, minibatch_size = 64, print_cost = True):  
""" Implements a three-layer ConvNet in Tensorflow: CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED Arguments: X_train -- training set, of shape (None, 64, 64, 3) Y_train -- test set, of shape (None, n_y = 6) X_test -- training set, of shape (None, 64, 64, 3) Y_test -- test set, of shape (None, n_y = 6) learning_rate -- learning rate of the optimization num_epochs -- number of epochs of the optimization loop minibatch_size -- size of a minibatch print_cost -- True to print the cost every 100 epochs Returns: train_accuracy -- real number, accuracy on the train set (X_train) test_accuracy -- real number, testing accuracy on the test set (X_test) parameters -- parameters learnt by the model. They can then be used to predict. """ ops.reset_default_graph() tf.set_random_seed(1) seed = 3 (m, n_H0, n_W0, n_C0) = X_train.shape n_y = Y_train.shape[1] costs = [] # Create Placeholders of the correct shape X, Y = create_placeholders(n_H0, n_W0, n_C0, n_y)
# Initialize parameters parameters = initialize_parameters()
# Forward propagation Z3 = forward_propagation(X, parameters)
# Cost function cost = compute_cost(Z3, Y)
# Backpropagation optimizer = tf.train.AdamOptimizer(learning_rate = learning_rate).minimize(cost) # Initialize all the variables globally init = tf.global_variables_initializer()
# Start the session to compute the tensorflow graph with tf.Session() as sess:
# Run the initialization sess.run(init)
# Do the training loop for epoch in range(num_epochs): minibatch_cost = 0. num_minibatches = int(m / minibatch_size) seed = seed + 1 minibatches = random_mini_batches(X_train, Y_train, minibatch_size, seed)
for minibatch in minibatches:
# Select a minibatch (minibatch_X, minibatch_Y) = minibatch _ , temp_cost = sess.run([optimizer, cost], feed_dict={X: minibatch_X, Y: minibatch_Y}) minibatch_cost += temp_cost / num_minibatches
# Print the cost every epoch if print_cost == True and epoch % 5 == 0:
print ("Cost after epoch %i: %f" % (epoch, minibatch_cost))
if print_cost == True and epoch % 1 == 0: costs.append(minibatch_cost)
# plot the cost plt.plot(np.squeeze(costs)) plt.ylabel('cost') plt.xlabel('iterations (per tens)') plt.title("Learning rate =" + str(learning_rate)) plt.show() # Calculate the correct predictions predict_op = tf.argmax(Z3, 1) correct_prediction = tf.equal(predict_op, tf.argmax(Y, 1))
# Calculate accuracy on the test set accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(accuracy) train_accuracy = accuracy.eval({X: X_train, Y: Y_train}) test_accuracy = accuracy.eval({X: X_test, Y: Y_test}) print("Train Accuracy:", train_accuracy) print("Test Accuracy:", test_accuracy)

return train_accuracy, test_accuracy, parameters

對(duì)訓(xùn)練集執(zhí)行模型訓(xùn)練:

_,_,parameters=model(X_train,Y_train,X_test,Y_test)

訓(xùn)練迭代過程如下:

640?wx_fmt=png


我們?cè)谟?xùn)練集上取得了 0.67 的準(zhǔn)確率,在測(cè)試集上的預(yù)測(cè)準(zhǔn)確率為 0.58 ,雖然效果并不顯著,模型也有待深度調(diào)優(yōu),但我們已經(jīng)學(xué)會(huì)了如何用 Tensorflow 快速搭建起一個(gè)深度學(xué)習(xí)系統(tǒng)了。


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    如何在STM32和Arduino上實(shí)現(xiàn)卷積神經(jīng)網(wǎng)絡(luò)

    在大多數(shù)情況下,實(shí)用的機(jī)器學(xué)習(xí)算法需要大量計(jì)算資源(CPU 運(yùn)算周期和內(nèi)存占用)。不過,TensorFlow Lite 近期推出了一個(gè)實(shí)驗(yàn)版本,可在多款微控制器上運(yùn)行。倘若我們能構(gòu)建出適用于資源受限設(shè)備的模型,便能著手將嵌入式系統(tǒng)改造為小型機(jī)器
    的頭像 發(fā)表于 01-19 10:04 ?4029次閱讀
    如何在STM32和Arduino上<b class='flag-5'>實(shí)現(xiàn)</b><b class='flag-5'>卷積</b><b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>

    神經(jīng)網(wǎng)絡(luò)的初步認(rèn)識(shí)

    日常生活中的智能應(yīng)用都離不開深度學(xué)習(xí),而深度學(xué)習(xí)則依賴于神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)。什么是
    的頭像 發(fā)表于 12-17 15:05 ?323次閱讀
    <b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>的初步認(rèn)識(shí)

    自動(dòng)駕駛中常提的卷積神經(jīng)網(wǎng)絡(luò)是個(gè)啥?

    在自動(dòng)駕駛領(lǐng)域,經(jīng)常會(huì)聽到卷積神經(jīng)網(wǎng)絡(luò)技術(shù)。卷積神經(jīng)網(wǎng)絡(luò),簡(jiǎn)稱為CNN,是一種專門用來處理網(wǎng)格狀數(shù)據(jù)(比如圖像)的深度
    的頭像 發(fā)表于 11-19 18:15 ?2076次閱讀
    自動(dòng)駕駛中常提的<b class='flag-5'>卷積</b><b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>是個(gè)啥?

    CNN卷積神經(jīng)網(wǎng)絡(luò)設(shè)計(jì)原理及在MCU200T上仿真測(cè)試

    數(shù)的提出很大程度的解決了BP算法在優(yōu)化深層神經(jīng)網(wǎng)絡(luò)時(shí)的梯度耗散問題。當(dāng)x&gt;0 時(shí),梯度恒為1,無梯度耗散問題,收斂快;當(dāng)x&lt;0 時(shí),該層的輸出為0。 CNN
    發(fā)表于 10-29 07:49

    NMSIS神經(jīng)網(wǎng)絡(luò)庫使用介紹

    :   神經(jīng)網(wǎng)絡(luò)卷積函數(shù)   神經(jīng)網(wǎng)絡(luò)激活函數(shù)   全連接層函數(shù)   神經(jīng)網(wǎng)絡(luò)池化函數(shù)   Softmax 函數(shù)   神經(jīng)網(wǎng)絡(luò)支持功能
    發(fā)表于 10-29 06:08

    構(gòu)建CNN網(wǎng)絡(luò)模型并優(yōu)化的一般化建議

    整個(gè)模型非常巨大。所以要想實(shí)現(xiàn)輕量級(jí)的CNN神經(jīng)網(wǎng)絡(luò)模型,首先應(yīng)該避免嘗試單層神經(jīng)網(wǎng)絡(luò)。 2)減少卷積核的大?。篊NN神經(jīng)網(wǎng)絡(luò)是通過權(quán)值共
    發(fā)表于 10-28 08:02

    卷積運(yùn)算分析

    的數(shù)據(jù),故設(shè)計(jì)了ConvUnit模塊實(shí)現(xiàn)單個(gè)感受域規(guī)模的卷積運(yùn)算. 卷積運(yùn)算:不同于數(shù)學(xué)當(dāng)中提及到的卷積概念,CNN神經(jīng)網(wǎng)絡(luò)中的
    發(fā)表于 10-28 07:31

    在Ubuntu20.04系統(tǒng)中訓(xùn)練神經(jīng)網(wǎng)絡(luò)模型的一些經(jīng)驗(yàn)

    本帖欲分享在Ubuntu20.04系統(tǒng)中訓(xùn)練神經(jīng)網(wǎng)絡(luò)模型的一些經(jīng)驗(yàn)。我們采用jupyter notebook作為開發(fā)IDE,以TensorFlow2為訓(xùn)練框架,目標(biāo)是訓(xùn)練一個(gè)手寫數(shù)字識(shí)別的神經(jīng)網(wǎng)絡(luò)
    發(fā)表于 10-22 07:03

    CICC2033神經(jīng)網(wǎng)絡(luò)部署相關(guān)操作

    讀取。接下來需要使用擴(kuò)展指令,完成神經(jīng)網(wǎng)絡(luò)的部署,此處僅對(duì)第一層卷積+池化的部署進(jìn)行說明,其余層與之類似。 1.使用 Custom_Dtrans 指令,將權(quán)重?cái)?shù)據(jù)、輸入數(shù)據(jù)導(dǎo)入硬件加速器內(nèi)。對(duì)于權(quán)重
    發(fā)表于 10-20 08:00

    液態(tài)神經(jīng)網(wǎng)絡(luò)(LNN):時(shí)間連續(xù)性與動(dòng)態(tài)適應(yīng)性的神經(jīng)網(wǎng)絡(luò)

    1.算法簡(jiǎn)介液態(tài)神經(jīng)網(wǎng)絡(luò)(LiquidNeuralNetworks,LNN)是一種新型的神經(jīng)網(wǎng)絡(luò)架構(gòu),其設(shè)計(jì)理念借鑒自生物神經(jīng)系統(tǒng),特別是秀麗隱桿線蟲的神經(jīng)結(jié)構(gòu),盡管這種微生物的
    的頭像 發(fā)表于 09-28 10:03 ?1211次閱讀
    液態(tài)<b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>(LNN):時(shí)間連續(xù)性與動(dòng)態(tài)適應(yīng)性的<b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>

    如何在機(jī)器視覺中部署深度學(xué)習(xí)神經(jīng)網(wǎng)絡(luò)

    圖 1:基于深度學(xué)習(xí)的目標(biāo)檢測(cè)可定位已訓(xùn)練的目標(biāo)類別,并通過矩形框(邊界框)對(duì)其進(jìn)行標(biāo)識(shí)。 在討論人工智能(AI)或深度學(xué)習(xí)時(shí),經(jīng)常會(huì)出現(xiàn)“神經(jīng)網(wǎng)絡(luò)
    的頭像 發(fā)表于 09-10 17:38 ?900次閱讀
    如何在機(jī)器視覺中部署<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b><b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>

    卷積神經(jīng)網(wǎng)絡(luò)如何監(jiān)測(cè)皮帶堵料情況 #人工智能

    卷積神經(jīng)網(wǎng)絡(luò)
    jf_60804796
    發(fā)布于 :2025年07月01日 17:08:42

    無刷電機(jī)小波神經(jīng)網(wǎng)絡(luò)轉(zhuǎn)子位置檢測(cè)方法的研究

    摘要:論文通過對(duì)無刷電機(jī)數(shù)學(xué)模型的推導(dǎo),得出轉(zhuǎn)角:與三相相電壓之間存在映射關(guān)系,因此構(gòu)建了一個(gè)以三相相電壓為輸人,轉(zhuǎn)角為輸出的小波神經(jīng)網(wǎng)絡(luò)實(shí)現(xiàn)轉(zhuǎn)角預(yù)測(cè),并采用改進(jìn)遺傳算法來訓(xùn)練網(wǎng)絡(luò)結(jié)構(gòu)與參數(shù),借助
    發(fā)表于 06-25 13:06

    神經(jīng)網(wǎng)絡(luò)專家系統(tǒng)在電機(jī)故障診斷中的應(yīng)用

    摘要:針對(duì)傳統(tǒng)專家系統(tǒng)不能進(jìn)行自學(xué)習(xí)、自適應(yīng)的問題,本文提出了基于種經(jīng)網(wǎng)絡(luò)專家系統(tǒng)的并步電機(jī)故障診斷方法。本文將小波神經(jīng)網(wǎng)絡(luò)和專家系統(tǒng)相結(jié)合,充分發(fā)揮了二者故障診斷的優(yōu)點(diǎn),很大程度上降低了對(duì)電機(jī)
    發(fā)表于 06-16 22:09

    自動(dòng)駕駛感知系統(tǒng)中卷積神經(jīng)網(wǎng)絡(luò)原理的疑點(diǎn)分析

    背景 卷積神經(jīng)網(wǎng)絡(luò)(Convolutional Neural Networks, CNN)的核心技術(shù)主要包括以下幾個(gè)方面:局部連接、權(quán)值共享、多卷積核以及池化。這些技術(shù)共同作用,使得CNN在圖像
    的頭像 發(fā)表于 04-07 09:15 ?846次閱讀
    自動(dòng)駕駛感知系統(tǒng)中<b class='flag-5'>卷積</b><b class='flag-5'>神經(jīng)網(wǎng)絡(luò)</b>原理的疑點(diǎn)分析