Python高級項目在Python中使用TensorFlow實現(xiàn)CIFAR10_第1頁
Python高級項目在Python中使用TensorFlow實現(xiàn)CIFAR10_第2頁
Python高級項目在Python中使用TensorFlow實現(xiàn)CIFAR10_第3頁
已閱讀5頁,還剩8頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、Python高級項目Python 有著廣泛的應(yīng)用從“HelloWorld”一路走到實現(xiàn)人工智能。實際上,您可以使用Python 進(jìn)行無限多的項目,但如果您想深入了解Python 的核心,可以考慮以下幾個主要的項目。使用 PyTorch 、 TensorF low 、 Keras 和您喜歡的任何機(jī)器學(xué)習(xí)庫進(jìn)行機(jī)器學(xué)習(xí)。使用 Open CV和 PIL 研究計算機(jī)視覺。使用測試和文檔,創(chuàng)建和發(fā)布自己的pip 模塊。在這些里面,我最喜歡的就是機(jī)器學(xué)習(xí)和深度學(xué)習(xí)。讓我們看一個非常好的用例以便深入學(xué)習(xí)Python 。在 Python 中使用 TensorFlo w實現(xiàn) CIFA R10讓我們訓(xùn)練一個網(wǎng)絡(luò),

2、對CIFA R10數(shù)據(jù)集中的圖像進(jìn)行分類。可以使用TensorF low 內(nèi)置的卷積神經(jīng)網(wǎng)絡(luò)。為理解用例的工作原理,我們考慮以下流程圖:我們把這個流程圖分解成簡單的組分:首先將圖像加載到程序中這些圖像存儲在程序可以訪問的位置將數(shù)據(jù)規(guī)范化,因為我們需要Python 來理解當(dāng)前的信息。定義神經(jīng)網(wǎng)絡(luò)的基礎(chǔ)。定義損失函數(shù)以確保我們在數(shù)據(jù)集上獲得最大精度訓(xùn)練實際模型,了解一些它所一直看到的數(shù)據(jù)對模型進(jìn)行測試,以分析其準(zhǔn)確性,并迭代整個訓(xùn)練過程,以獲得更好的精度。這個用例分為兩個程序。一個是訓(xùn)練網(wǎng)絡(luò),另一個是測試網(wǎng)絡(luò)。我們先訓(xùn)練一下這個網(wǎng)絡(luò)。訓(xùn)練網(wǎng)絡(luò)importnumpyasnpimporttensorf

3、lowas tffromtime importtimeimportmathfrominclude.dataimportget_data_setfrominclude.modelimportmodel, lrtrain_x, train_y = get_data_set("train")test_x,test_y=get_data_set("test")tf.set_random_seed(21)x, y, output, y_pred_cls, global_step, learning_rate = model()global_accuracy = 0

4、epoch_start=0# PARAMS=_BATCH_SIZE128_EPOCH =60_SAVE_PATH ="./tensorboard/cifar-10-v1.0.0/"# LOSS AND OPTIMIZERloss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=output, optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate, beta1=0.9,labels=y)beta2=0.999,epsil

5、on=1e-08).minimize(loss,global_step=global_step)# PREDICTION AND ACCURACYCALCULATIONcorrect_prediction = tf.equal(y_pred_cls, tf.argmax(y, axis=1)accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)# SAVERmerged=tf.summary.merge_all()saver=tf.train.Saver()sess=tf.Session()train_writer=t

6、f.summary.FileWriter(_SAVE_PATH,sess.graph)try:print("nTryingtorestorelastcheckpoint.")last_chk_path=tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)saver.restore(sess,save_path=last_chk_path)print("Restoredcheckpointfrom:",last_chk_path)exceptValueError:print("nFailedt

7、orestorecheckpoint.Initializingvariablesinstead.")sess.run(tf.global_variables_initializer()deftrain(epoch):globalepoch_startepoch_start=time()batch_size=int(math.ceil(len(train_x)/ _BATCH_SIZE)0i_global=forsinbatch_xs=batch_ys=start_timei_global,global_step,feed_dict=x:range(batch_size):train_

8、xs*_BATCH_SIZE:(s+1)*_BATCH_SIZEtrain_ys*_BATCH_SIZE:(s+1)*_BATCH_SIZE=time()_,batch_loss,batch_acc= sess.run(optimizer,loss,accuracy,batch_xs,y:batch_ys,learning_rate:lr(epoch)duration=time()-start_timeifs% 10= 0:percentage=int(round(s/batch_size)*100)bar_len=29filled_len=int(bar_len*int(percentage

9、)/100)bar='='*filled_len+ '>'+'-'*(bar_len- filled_len)msg="Globalstep:>5-:>3%- acc:.4f- loss:.4f-:.1fsample/sec"print(msg.format(i_global,bar,percentage,batch_acc,batch_loss,_BATCH_SIZE /duration)test_and_save(i_global,epoch)deftest_and_save(_global_step,e

10、poch):globalglobal_accuracyglobalepoch_starti=0predicted_class=np.zeros(shape=len(test_x),dtype=)whilei <len(test_x):j=min(i+ _BATCH_SIZE,len(test_x)batch_xs= test_xi:j,:batch_ys = test_yi:j,: predicted_classprint(mes.format(epoch+1),acc,correct_numbers,len(test_x),int(hours),int(minutes),s

11、econds)ifglobal_accuracy!=0andglobal_accuracy< acc:summary= tf.Summary(value=tf.Summary.Value(tag="Accuracy/test",simple_value=acc),print(mes.format(acc,global_accuracy)global_accuracy=accelifglobal_accuracy=0:global_accuracy=accprint("#")def main():train_start=time()foriinran

12、ge(_EPOCH):print("nEpoch:/n".format(i+1),_EPOCH)train(i)hours,rem=divmod(time()-train_start,3600)minutes, seconds = divmod(rem, 60)mes="Bestaccuracypresession:.2f,time:0>2:0>2:05.2f"print(mes.format(global_accuracy,int(hours),int(minutes),seconds)if_name_="_main_"

13、:main()sess.close()輸出: Epoch: 60/60Globalstep:23070->-0%-acc:0.9531-loss:1.5081-7045.4sample/secGlobalstep:23080->-3%-acc:0.9453-loss:1.5159-7147.6sample/secGlobalstep:23090-=>-5%-acc:0.9844-loss:1.4764-7154.6sample/secGlobalstep:23100-=>-8%-acc:0.9297-loss:1.5307-7104.4sample/secGlobals

14、tep:23110-=>-10%-acc:0.9141-loss:1.5462-7091.4sample/secGlobalstep:23120-=>-13%-acc:0.9297-loss:1.5314-7162.9sample/secGlobalstep:23130-=>-15%-acc:0.9297-loss:1.5307-7174.8sample/secGlobalstep:23140-=>-18%-acc:0.9375-loss:1.5231-7140.0sample/secGlobalstep:23150-=>-20%-acc:0.9297-loss:

15、1.5301-7152.8sample/secGlobalstep:23160-=>-23%-acc:0.9531-loss:1.5080-7112.3sample/secGlobalstep:23170-=>-26%-acc:0.9609-loss:1.5000-7154.0sample/secGlobalstep:23180-=>-28%-acc:0.9531-loss:1.5074-6862.2sample/secGlobalstep:23190-=>-31%-acc:0.9609-loss:1.4993-7134.5sample/secGlobalstep:23

16、200-=>-33%-acc:0.9609-loss:1.4995-7166.0sample/secGlobalstep:23210-=>-36%-acc:0.9375-loss:1.5231-7116.7sample/secGlobalstep:23220-=>-38%-acc:0.9453-loss:1.5153-7134.1sample/secGlobalstep:23230-=>-41%-acc:0.9375-loss:1.5233-7074.5sample/secGlobalstep:23240-=>-43%-acc:0.9219-loss:1.5387

17、-7176.9sample/secGlobalstep:23250-=>-46%-acc:0.8828-loss:1.5769-7144.1sample/secGlobalstep:23260-=>-49%-acc:0.9219-loss:1.5383-7059.7sample/secGlobalstep:23270-=>-51%-acc:0.8984-loss:1.5618-6638.6sample/secGlobalstep:23280-=>-54%-acc:0.9453-loss:1.5151-7035.7sample/secGlobalstep:23290-=&

18、gt;-56%-acc:0.9609-loss:1.4996-7129.0sample/secGlobalstep:23300-=>-59%-acc:0.9609-loss:1.4997-7075.4sample/secGlobalstep:23310-=>-61%-acc:0.8750-loss:1.5842-7117.8sample/secGlobal step: 23320 -=>-64%- acc: 0.9141 -loss: 1.5463 - 7157.2 sample/secGlobal step: 23330 -=>-66%- acc: 0.9062 -l

19、oss: 1.5549 - 7169.3 sample/secGlobalstep:23340-=>-69%-acc:0.9219-loss:1.5389-7164.4sample/secGlobalstep:23350-=>-72%-acc:0.9609-loss:1.5002-7135.4sample/secGlobalstep:23360-=>-74%-acc:0.9766-loss:1.4842-7124.2sample/secGlobal step: 23370 -=>-77%- acc: 0.9375 -loss: 1.5231 - 7168.5 sampl

20、e/secGlobal step: 23380-=>-79%- acc: 0.8906-loss: 1.5695 - 7175.2sample/secGlobal step: 23390-=>-82%- acc: 0.9375-loss: 1.5225 - 7132.1sample/secGlobal step: 23400-=>-84%- acc: 0.9844-loss: 1.4768 - 7100.1sample/secGlobalstep:23410-=>-87%-acc:0.9766-loss:1.4840-7172.0sample/secGlobal ste

21、p: 23420-=>-90%- acc: 0.9062-loss: 1.5542 - 7122.1sample/secGlobalstep:23430-=>-92%-acc:0.9297-loss:1.5313-7145.3sample/secGlobalstep:23440-=>-95%-acc:0.9297-loss:1.5301-7133.3sample/secGlobalstep:23450-=>-97%-acc:0.9375-loss:1.5231-7135.7sample/secGlobalstep:23460-=>100%-acc:0.9250-l

22、oss:1.5362-10297.5sample/secEpoch60-accuracy:78.81%(7881/10000)Thisepochreceivebetteraccuracy:78.81>78.78.Savingsession.#在測試數(shù)據(jù)集上運行網(wǎng)絡(luò)importnumpyasnpimporttensorflowas tffrominclude.dataimport get_data_setfrominclude.modelimportmodeltest_x,test_y=get_data_set("test")x, y,output,y_pred_cls

23、,global_step,learning_rate= model()_BATCH_SIZE=128_CLASS_SIZE=10_SAVE_PATH ="./tensorboard/cifar-10-v1.0.0/"saver = tf.train.Saver()sess=tf.Session()try:print("nTryingtorestorelastcheckpoint.")last_chk_path=tf.train.latest_checkpoint(checkpoint_dir=_SAVE_PATH)saver.restore(sess,save_path=last_chk_path)print("Restoredcheckpointfrom:",last_chk_path)exceptValueError:print("nFailedtorestorecheck

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論