tensorflow如何繼續(xù)訓(xùn)練之前保存的模型實(shí)例
一:需重定義神經(jīng)網(wǎng)絡(luò)繼續(xù)訓(xùn)練的方法
1.訓(xùn)練代碼
import numpy as np import tensorflow as tf x_data=np.random.rand(100).astype(np.float32) y_data=x_data*0.1+0.3 weight=tf.Variable(tf.random_uniform([1],-1.0,1.0),name="w") biases=tf.Variable(tf.zeros([1]),name="b") y=weight*x_data+biases loss=tf.reduce_mean(tf.square(y-y_data)) #loss optimizer=tf.train.GradientDescentOptimizer(0.5) train=optimizer.minimize(loss) init=tf.global_variables_initializer() sess=tf.Session() sess.run(init) saver=tf.train.Saver(max_to_keep=0) for step in range(10): sess.run(train) saver.save(sess,"./save_mode",global_step=step) #保存 print("當(dāng)前進(jìn)行:",step)
第一次訓(xùn)練截圖:
2.恢復(fù)上一次的訓(xùn)練
import numpy as np import tensorflow as tf sess=tf.Session() saver=tf.train.import_meta_graph(r'save_mode-9.meta') saver.restore(sess,tf.train.latest_checkpoint(r'./')) print(sess.run("w:0"),sess.run("b:0")) graph=tf.get_default_graph() weight=graph.get_tensor_by_name("w:0") biases=graph.get_tensor_by_name("b:0") x_data=np.random.rand(100).astype(np.float32) y_data=x_data*0.1+0.3 y=weight*x_data+biases loss=tf.reduce_mean(tf.square(y-y_data)) optimizer=tf.train.GradientDescentOptimizer(0.5) train=optimizer.minimize(loss) saver=tf.train.Saver(max_to_keep=0) for step in range(10): sess.run(train) saver.save(sess,r"./save_new_mode",global_step=step) print("當(dāng)前進(jìn)行:",step," ",sess.run(weight),sess.run(biases))
使用上次保存下的數(shù)據(jù)進(jìn)行繼續(xù)訓(xùn)練和保存:
#最后要提一下的是:
checkpoint文件
meta保存了TensorFlow計(jì)算圖的結(jié)構(gòu)信息
datat保存每個(gè)變量的取值
index保存了 表
加載restore時(shí)的文件路徑名是以checkpoint文件中的“model_checkpoint_path”值決定的
這個(gè)方法需要重新定義神經(jīng)網(wǎng)絡(luò)
二:不需要重新定義神經(jīng)網(wǎng)絡(luò)的方法:
在上面訓(xùn)練的代碼中加入:tf.add_to_collection("name",參數(shù))
import numpy as np import tensorflow as tf x_data=np.random.rand(100).astype(np.float32) y_data=x_data*0.1+0.3 weight=tf.Variable(tf.random_uniform([1],-1.0,1.0),name="w") biases=tf.Variable(tf.zeros([1]),name="b") y=weight*x_data+biases loss=tf.reduce_mean(tf.square(y-y_data)) optimizer=tf.train.GradientDescentOptimizer(0.5) train=optimizer.minimize(loss) tf.add_to_collection("new_way",train) init=tf.global_variables_initializer() sess=tf.Session() sess.run(init) saver=tf.train.Saver(max_to_keep=0) for step in range(10): sess.run(train) saver.save(sess,"./save_mode",global_step=step) print("當(dāng)前進(jìn)行:",step)
在下面的載入代碼中加入:tf.get_collection("name"),就可以直接使用了
import numpy as np import tensorflow as tf sess=tf.Session() saver=tf.train.import_meta_graph(r'save_mode-9.meta') saver.restore(sess,tf.train.latest_checkpoint(r'./')) print(sess.run("w:0"),sess.run("b:0")) graph=tf.get_default_graph() weight=graph.get_tensor_by_name("w:0") biases=graph.get_tensor_by_name("b:0") y=tf.get_collection("new_way")[0] saver=tf.train.Saver(max_to_keep=0) for step in range(10): sess.run(y) saver.save(sess,r"./save_new_mode",global_step=step) print("當(dāng)前進(jìn)行:",step," ",sess.run(weight),sess.run(biases))
總的來說,下面這種方法好像是要便利一些
以上這篇tensorflow如何繼續(xù)訓(xùn)練之前保存的模型實(shí)例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Tensorflow訓(xùn)練MNIST手寫數(shù)字識別模型
- tensorflow實(shí)現(xiàn)訓(xùn)練變量checkpoint的保存與讀取
- Tensorflow訓(xùn)練模型越來越慢的2種解決方案
- TensorFlow實(shí)現(xiàn)保存訓(xùn)練模型為pd文件并恢復(fù)
- 解決TensorFlow訓(xùn)練內(nèi)存不斷增長,進(jìn)程被殺死問題
- tensorflow獲取預(yù)訓(xùn)練模型某層參數(shù)并賦值到當(dāng)前網(wǎng)絡(luò)指定層方式
- tensorflow模型繼續(xù)訓(xùn)練 fineturn實(shí)例
- Tensorflow實(shí)現(xiàn)在訓(xùn)練好的模型上進(jìn)行測試
- tensorflow保持每次訓(xùn)練結(jié)果一致的簡單實(shí)現(xiàn)
相關(guān)文章
詳解字典樹Trie結(jié)構(gòu)及其Python代碼實(shí)現(xiàn)
Trie多被用來查找和統(tǒng)計(jì)字符串,利用公共前綴來減少搜索時(shí)間,下面我們就來詳解字典樹Trie結(jié)構(gòu)及其Python代碼實(shí)現(xiàn)2016-06-06使用Python3 poplib模塊刪除服務(wù)器多天前的郵件實(shí)現(xiàn)代碼
這篇文章主要介紹了使用Python3 poplib模塊刪除多天前的郵件的實(shí)現(xiàn)代碼,代碼簡單易懂,非常不錯(cuò),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04Python實(shí)戰(zhàn)爬蟲之女友欲買文胸不知何色更美
實(shí)踐來源于理論,做爬蟲前肯定要先了解相關(guān)的規(guī)則和原理,網(wǎng)絡(luò)爬蟲又稱為網(wǎng)頁蜘蛛,網(wǎng)絡(luò)機(jī)器人,更經(jīng)常的稱為網(wǎng)頁追逐者,是一種按照一定的規(guī)則,自動地抓取萬維網(wǎng)信息的程序或者腳本。一句話概括就是網(wǎng)上信息搬運(yùn)工。本篇文章帶你深入了解,需要的朋友可以參考下2021-09-09在Python的Django框架的視圖中使用Session的方法
這篇文章主要介紹了在Python的Django框架的視圖中使用Session的方法,包括相關(guān)的設(shè)置測試Cookies的方法,需要的朋友可以參考下2015-07-07Python實(shí)現(xiàn)方便使用的級聯(lián)進(jìn)度信息實(shí)例
這篇文章主要介紹了Python實(shí)現(xiàn)方便使用的級聯(lián)進(jìn)度信息,實(shí)例分析了Python顯示級聯(lián)進(jìn)度信息的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-05-05在Django的視圖中使用數(shù)據(jù)庫查詢的方法
這篇文章主要介紹了在Django的視圖中使用數(shù)據(jù)庫查詢的方法,是Python的Django框架使用的基礎(chǔ)操作,需要的朋友可以參考下2015-07-07python 如何用map()函數(shù)創(chuàng)建多線程任務(wù)
這篇文章主要介紹了python 使用map()函數(shù)創(chuàng)建多線程任務(wù)的操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04