這篇文章主要介紹導(dǎo)出python中模型參數(shù)的方法,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!

在大冶等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產(chǎn)品創(chuàng)新能力,以專注、極致的服務(wù)理念,為客戶提供成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、外貿(mào)網(wǎng)站建設(shè) 網(wǎng)站設(shè)計制作定制開發(fā),公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),品牌網(wǎng)站制作,網(wǎng)絡(luò)營銷推廣,外貿(mào)網(wǎng)站制作,大冶網(wǎng)站建設(shè)費用合理。
模型的保存和讀取
1.tensorflow保存和讀取模型:tf.train.Saver() .save()
#保存模型需要用到save函數(shù) save( sess, save_path, global_step=None, latest_filename=None, meta_graph_suffix='meta', write_meta_graph=True, write_state=True ) ''' sess: 保存模型要求必須有一個加載了計算圖的會話,而且所有變量必須已被初始化。 save_path: 模型保存路徑及保存名稱 global_step: 如果提供的話,這個數(shù)字會添加到save_path后面,用于區(qū)分不同訓(xùn)練階段的結(jié)果 '''
示例:
#例子
import tensorflow as tf
import numpy as np
import os
#用numpy產(chǎn)生數(shù)據(jù)
x_data = np.linspace(-1,1,300)[:, np.newaxis] #轉(zhuǎn)置
noise = np.random.normal(0,0.05, x_data.shape)
y_data = np.square(x_data)-0.5+noise
#輸入層
x_ph = tf.placeholder(tf.float32, [None, 1])
y_ph = tf.placeholder(tf.float32, [None, 1])
#隱藏層
w1 = tf.Variable(tf.random_normal([1,10]))
b1 = tf.Variable(tf.zeros([1,10])+0.1)
wx_plus_b1 = tf.matmul(x_ph, w1) + b1
hidden = tf.nn.relu(wx_plus_b1)
#輸出層
w2 = tf.Variable(tf.random_normal([10,1]))
b2 = tf.Variable(tf.zeros([1,1])+0.1)
wx_plus_b2 = tf.matmul(hidden, w2) + b2
y = wx_plus_b2
#損失
loss = tf.reduce_mean(tf.reduce_sum(tf.square(y_ph-y),reduction_indices=[1]))
train_op = tf.train.GradientDescentOptimizer(0.1).minimize(loss)
#保存模型對象saver
saver = tf.train.Saver()
#判斷模型保存路徑是否存在,不存在就創(chuàng)建
if not os.path.exists('tmp/'):
os.mkdir('tmp/')
#初始化
with tf.Session() as sess:
if os.path.exists('tmp/checkpoint'): #判斷模型是否存在
saver.restore(sess, 'tmp/model.ckpt') #存在就從模型中恢復(fù)變量
else:
init = tf.global_variables_initializer() #不存在就初始化變量
sess.run(init)
for i in range(1000):
_,loss_value = sess.run([train_op,loss], feed_dict={x_ph:x_data, y_ph:y_data})
if(i%50==0):
save_path = saver.save(sess, 'tmp/model.ckpt')
print("迭代次數(shù):%d , 訓(xùn)練損失:%s"%(i, loss_value))每調(diào)用一次保存操作會創(chuàng)建后3個數(shù)據(jù)文件并創(chuàng)建一個檢查點(checkpoint)文件,簡單理解就是權(quán)重等參數(shù)被保存到 .chkp.data 文件中,以字典的形式;圖和元數(shù)據(jù)被保存到 .chkp.meta 文件中,可以被 tf.train.import_meta_graph 加載到當(dāng)前默認的圖。
2.keras保存和讀取模型
model.save(filepath),同時保存model和權(quán)重的
import numpy as np
from keras.datasets import mnist
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
# 載入數(shù)據(jù)
(x_train,y_train),(x_test,y_test) = mnist.load_data()
# (60000,28,28)
print('x_shape:',x_train.shape)
# (60000)
print('y_shape:',y_train.shape)
# (60000,28,28)->(60000,784)
x_train = x_train.reshape(x_train.shape[0],-1)/255.0
x_test = x_test.reshape(x_test.shape[0],-1)/255.0
# 換one hot格式
y_train = np_utils.to_categorical(y_train,num_classes=10)
y_test = np_utils.to_categorical(y_test,num_classes=10)
# 創(chuàng)建模型,輸入784個神經(jīng)元,輸出10個神經(jīng)元
model = Sequential([
Dense(units=10,input_dim=784,bias_initializer='one',activation='softmax')
])
# 定義優(yōu)化器
sgd = SGD(lr=0.2)
# 定義優(yōu)化器,loss function,訓(xùn)練過程中計算準(zhǔn)確率
model.compile(
optimizer = sgd,
loss = 'mse',
metrics=['accuracy'],
)
# 訓(xùn)練模型
model.fit(x_train,y_train,batch_size=64,epochs=5)
# 評估模型
loss,accuracy = model.evaluate(x_test,y_test)
print('\ntest loss',loss)
print('accuracy',accuracy)
# 保存模型
model.save('model.h6')以上是導(dǎo)出python中模型參數(shù)的方法的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
網(wǎng)頁題目:導(dǎo)出python中模型參數(shù)的方法
網(wǎng)址分享:http://www.chinadenli.net/article36/gccspg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站內(nèi)鏈、關(guān)鍵詞優(yōu)化、用戶體驗、品牌網(wǎng)站設(shè)計、動態(tài)網(wǎng)站、網(wǎng)站營銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)