日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tensorflow详解保存模型(进阶版一):如何有选择的保存变量

發布時間:2024/4/18 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tensorflow详解保存模型(进阶版一):如何有选择的保存变量 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當然掌握了基礎版還不夠,我們來看一下進階版一:如何有選擇的保存變量:

這里還要另外涉及兩個函數

tf.variable_scope("xxx") 和 tf.get_variable(name,shape,initializer = initializer)

簡單介紹,詳細介紹還請大家在查閱其他資料

tf.varibale_scope("xxx"),是定義變量的作用范圍的函數,相當于把變量做一個集合方便管理。

tf.get_variable(),是生成一個變量。如果該變量生成過,就繼承;如果沒有聲明過,則重新生成。(注意,必須在會話(sess)外調用,否則會報錯類似:使用了未初始化(uninitialized)變量)

我們以C3D的做法舉例:

def _variable_on_cpu(name, shape, initializer):#with tf.device('/cpu:%d' % cpu_id):with tf.device('/cpu:0'):var = tf.get_variable(name, shape, initializer=initializer)return vardef _variable_with_weight_decay(name, shape, stddev, wd):var = _variable_on_cpu(name, shape, tf.truncated_normal_initializer(stddev=stddev))if wd is not None:weight_decay = tf.nn.l2_loss(var) * wdtf.add_to_collection('losses', weight_decay)return varwith tf.Graph().as_default():with tf.variable_scope('var_name') as var_scope:weights = {'wc1': _variable_with_weight_decay('wc1', [3, 3, 3, 3, 64], 0.04, 0.00),'wc2': _variable_with_weight_decay('wc2', [3, 3, 3, 64, 128], 0.04, 0.00),'wc3a': _variable_with_weight_decay('wc3a', [3, 3, 3, 128, 256], 0.04, 0.00),'wc3b': _variable_with_weight_decay('wc3b', [3, 3, 3, 256, 256], 0.04, 0.00),'wc4a': _variable_with_weight_decay('wc4a', [3, 3, 3, 256, 512], 0.04, 0.00),'wc4b': _variable_with_weight_decay('wc4b', [3, 3, 3, 512, 512], 0.04, 0.00),'wc5a': _variable_with_weight_decay('wc5a', [3, 3, 3, 512, 512], 0.04, 0.00),'wc5b': _variable_with_weight_decay('wc5b', [3, 3, 3, 512, 512], 0.04, 0.00),'cam':_variable_with_weight_decay('cam', [1,1,512,c3d_model.NUM_CLASSES], 0.04,0.00),}biases = {'bc1': _variable_with_weight_decay('bc1', [64], 0.04, 0.0),'bc2': _variable_with_weight_decay('bc2', [128], 0.04, 0.0),'bc3a': _variable_with_weight_decay('bc3a', [256], 0.04, 0.0),'bc3b': _variable_with_weight_decay('bc3b', [256], 0.04, 0.0),'bc4a': _variable_with_weight_decay('bc4a', [512], 0.04, 0.0),'bc4b': _variable_with_weight_decay('bc4b', [512], 0.04, 0.0),'bc5a': _variable_with_weight_decay('bc5a', [512], 0.04, 0.0),'bc5b': _variable_with_weight_decay('bc5b', [512], 0.04, 0.0),}

仔細看,這里weights 和 bias 都是字典。且都被劃分在了“var_name”這個集合之下:

在“var_name”這個集合下, _variable_with_weight_decay()函數調用了tf.get_variable()函數,這就相當于將tf.get_variable() 產生得變量加入到這個集合中。即在 tf.variable_scope('xxx') 下調用 tf.get_variable() 生成的變量都會被包括在該集合(xxx)中。

這會造成一個什么效果呢?就是在保存變量到模型中的時候Tensorname 會變成 xxx/wc1、xxx/wc2 等。

接下來我們來看如何有選擇的保存變量:

import tensorflow as tfwith tf.Graph() as graph:model_name = 'xxx/xxx/name'with tf.Session() as sess:init = tf.global_variables_initializer()sess.run(init)variables_to_save = ... # 自己定義saver = tf.train.Saver(vraiables_to_save)for epoch in range(max_epochs)training() #saver.save(sess,model_name)

我們來看代碼里的自己定義的部分:這也就是tf.train.Saver函數的輸入變量,官方給的文檔說,要求要是‘dict’型或者是‘list’型,這里大家獲取能明白為什么用字典來聲明了。

  • A?dict?of names to variables: The keys are the names that will be used to save or restore the variables in the checkpoint files.
  • A list of variables: The variables will be keyed with their op name in the checkpoint files.

簡單來說:

dict型:{變量名:變量值}
list型:[變量值,]

?

舉個例子,我們只想保存wc,wc2,bc1,bc2。針對上面的 C3D,我們可以這樣操作。

#1.dict型variables_to_save = {'wc1':weights['wc1'],'wc2':weights['wc2'],'bc1':weights['bc1'],'bc2':weigghts['bc2']}#2.list型variables_to_save = [weight['wc1'],weight['wc2'],weight['bc1'],weights['bc2']]

?到這里我們就介紹結束了,可能初次學習會有困難,歡迎留言交流。

??

總結

以上是生活随笔為你收集整理的Tensorflow详解保存模型(进阶版一):如何有选择的保存变量的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。