在线观看www成人影院-在线观看www日本免费网站-在线观看www视频-在线观看操-欧美18在线-欧美1级

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

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

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

如何使用tensorflow快速搭建起一個深度學(xué)習(xí)項目

lviY_AI_shequ ? 來源:未知 ? 作者:李倩 ? 2018-10-25 08:57 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

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

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

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

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).Tprint ("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))

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

創(chuàng)建placeholder

首先需要為訓(xùn)練集預(yù)測變量和目標(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ù)初始化

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

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ò)的前向傳播過程

前向傳播過程如下所示:CONV2D -> RELU -> MAXPOOL -> CONV2D -> RELU -> MAXPOOL -> FLATTEN -> FULLYCONNECTED

可見我們要搭建的是一個典型的 CNN 過程,經(jīng)過兩次的卷積-relu激活-最大池化,然后展開接上一個全連接層。利用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

計算當(dāng)前損失

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

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)練過程模型。可能你會問為什么沒有反向傳播,這里需要注意的是Tensorflow幫助我們自動封裝好了反向傳播過程,無需我們再次定義,在實際搭建過程中我們只需將前向傳播的網(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

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

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

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

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

注:本深度學(xué)習(xí)筆記系作者學(xué)習(xí) Andrew NG 的 deeplearningai 五門課程所記筆記,其中代碼為每門課的課后assignments作業(yè)整理而成。

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

原文標(biāo)題:深度學(xué)習(xí)筆記12:卷積神經(jīng)網(wǎng)絡(luò)的Tensorflow實現(xiàn)

文章出處:【微信號:AI_shequ,微信公眾號:人工智能愛好者社區(qū)】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評論

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

    用樹莓派搞深度學(xué)習(xí)TensorFlow啟動!

    介紹本頁面將指導(dǎo)您在搭載64位Bullseye操作系統(tǒng)的RaspberryPi4上安裝TensorFlowTensorFlow專為深度
    的頭像 發(fā)表于 03-25 09:33 ?387次閱讀
    用樹莓派搞<b class='flag-5'>深度</b><b class='flag-5'>學(xué)習(xí)</b>?<b class='flag-5'>TensorFlow</b>啟動!

    GPU深度學(xué)習(xí)應(yīng)用案例

    能力,可以顯著提高圖像識別模型的訓(xùn)練速度和準確性。例如,在人臉識別、自動駕駛等領(lǐng)域,GPU被廣泛應(yīng)用于加速深度學(xué)習(xí)模型的訓(xùn)練和推理過程。 二、自然語言處理 自然語言處理(NLP)是深度學(xué)習(xí)
    的頭像 發(fā)表于 10-27 11:13 ?1258次閱讀

    FPGA加速深度學(xué)習(xí)模型的案例

    FPGA(現(xiàn)場可編程門陣列)加速深度學(xué)習(xí)模型是當(dāng)前硬件加速領(lǐng)域的熱門研究方向。以下是些FPGA加速
    的頭像 發(fā)表于 10-25 09:22 ?1153次閱讀

    PyTorch深度學(xué)習(xí)開發(fā)環(huán)境搭建指南

    PyTorch作為種流行的深度學(xué)習(xí)框架,其開發(fā)環(huán)境的搭建對于深度學(xué)習(xí)研究者和開發(fā)者來說至關(guān)重要
    的頭像 發(fā)表于 07-16 18:29 ?2406次閱讀

    如何在Tensorflow中實現(xiàn)反卷積

    TensorFlow中實現(xiàn)反卷積(也稱為轉(zhuǎn)置卷積或分數(shù)步長卷積)是涉及多個概念和步驟的過程。反卷積在深度學(xué)習(xí)領(lǐng)域,特別是在圖像分割、圖
    的頭像 發(fā)表于 07-14 10:46 ?1110次閱讀

    TensorFlow是什么?TensorFlow怎么用?

    TensorFlow是由Google開發(fā)的開源深度學(xué)習(xí)框架,它允許開發(fā)者方便地構(gòu)建、訓(xùn)練和部署各種復(fù)雜的機器
    的頭像 發(fā)表于 07-12 16:38 ?1287次閱讀

    tensorflow和pytorch哪個更簡單?

    PyTorch更簡單。選擇TensorFlow還是PyTorch取決于您的具體需求和偏好。如果您需要易于使用、靈活且具有強大社區(qū)支持的框架,PyTorch可能是
    的頭像 發(fā)表于 07-05 09:45 ?1413次閱讀

    tensorflow和pytorch哪個好

    :2015年由Google Brain團隊發(fā)布。 語言支持 :主要使用Python,也支持C++、Java等。 設(shè)計哲學(xué) :TensorFlow端到端的機器學(xué)習(xí)平臺,支持從研究
    的頭像 發(fā)表于 07-05 09:42 ?1176次閱讀

    tensorflow簡單的模型訓(xùn)練

    在本文中,我們將詳細介紹如何使用TensorFlow進行簡單的模型訓(xùn)練。TensorFlow開源的機器學(xué)習(xí)庫,廣泛用于各種機器
    的頭像 發(fā)表于 07-05 09:38 ?1271次閱讀

    keras模型轉(zhuǎn)tensorflow session

    和訓(xùn)練深度學(xué)習(xí)模型。Keras是基于TensorFlow、Theano或CNTK等底層計算框架構(gòu)建的。TensorFlow
    的頭像 發(fā)表于 07-05 09:36 ?841次閱讀

    keras的模塊結(jié)構(gòu)介紹

    Keras是高級深度學(xué)習(xí)庫,它提供了易于使用的接口來構(gòu)建和訓(xùn)練
    的頭像 發(fā)表于 07-05 09:35 ?680次閱讀

    基于深度學(xué)習(xí)的小目標(biāo)檢測

    在計算機視覺領(lǐng)域,目標(biāo)檢測直是研究的熱點和難點之。特別是在小目標(biāo)檢測方面,由于小目標(biāo)在圖像中所占比例小、特征不明顯,使得檢測難度顯著增加。隨著深度學(xué)習(xí)技術(shù)的
    的頭像 發(fā)表于 07-04 17:25 ?1991次閱讀

    如何使用Tensorflow保存或加載模型

    TensorFlow廣泛使用的開源機器學(xué)習(xí)庫,它提供了豐富的API來構(gòu)建和訓(xùn)練各種深度學(xué)習(xí)
    的頭像 發(fā)表于 07-04 13:07 ?2623次閱讀

    TensorFlow的定義和使用方法

    TensorFlow由谷歌人工智能團隊谷歌大腦(Google Brain)開發(fā)和維護的開源機器學(xué)習(xí)庫。它基于數(shù)據(jù)流編程(dataflow programming)的概念,將復(fù)雜的
    的頭像 發(fā)表于 07-02 14:14 ?1408次閱讀

    TensorFlow與PyTorch深度學(xué)習(xí)框架的比較與選擇

    深度學(xué)習(xí)作為人工智能領(lǐng)域的重要分支,在過去十年中取得了顯著的進展。在構(gòu)建和訓(xùn)練深度學(xué)習(xí)模型的
    的頭像 發(fā)表于 07-02 14:04 ?1621次閱讀
    主站蜘蛛池模板: 噜噜色小说| 色视频网站大全免费 | 一区二区中文字幕亚洲精品 | 狠狠操夜夜爽 | 狠狠色丁香婷婷久久综合不卡 | 青草青青产国视频在线 | 在线种子搜索 | 亚洲羞羞裸色私人影院 | 天天躁夜夜躁狠狠躁2024 | 天天摸天天操天天射 | 久久精品国产亚洲综合色 | 1024手机最新手机在线 | 欧美伦理影院 | 青草国内精品视频在线观看 | 国产欧美乱码在线看 | 欧美特黄三级在线观看 | 高清一区二区三区四区五区 | 中文4480yy私人免费影院 | 欧美一级片免费观看 | 人人乳乳香蕉大免费 | 欧美大片一区二区 | 亚欧美视频 | 色多多在线观看播放 | 欧美高清在线观看视频 | 天天天天添天天拍天天谢 | 黄色小视频免费 | 欧美一区二区三区视频在线观看 | 欧美亚洲综合另类成人 | 国产精品夜夜春夜夜爽久久 | 色噜噜亚洲 | 天天翘夜夜洗澡天天做 | bt天堂网在线www资源 | 国产吧在线视频 | 美女被网站免费看九色视频 | 精品女同同性视频很黄很色 | 特级黄色免费片 | 全部在线播放免费毛片 | 欧美一区二区影院 | 色婷婷亚洲综合五月 | 嗯好舒服好爽好快好大 | 4虎影视国产在线观看精品 4虎影院永久地址www |