我想在火車組(is_training=True
)和驗證集(is_training=False
)上運行給定模型,具體說明如何應用dropout
。現在,prebuilt models公開了一個參數is_training
,在構建網絡時,該參數傳遞給dropout
層。問題是,如果我使用不同的值is_training
兩次調用方法,我會得到兩個不分享權重的網絡(我認爲?)。我怎樣才能讓兩個網絡共享相同的權重,以便我可以運行我在驗證集上訓練過的網絡?帶有is_training True和False的Tensorflow(tf-slim)模型
1
A
回答
1
我寫了一個解決方案,您的評論在列車和測試模式中使用Overfeat。 (我無法測試,所以你可以檢查是否正常工作?)
一是部分進口及參數:
import tensorflow as tf
slim = tf.contrib.slim
overfeat = tf.contrib.slim.nets.overfeat
batch_size = 32
inputs = tf.placeholder(tf.float32, [batch_size, 231, 231, 3])
dropout_keep_prob = 0.5
num_classes = 1000
在訓練模式,我們通過正常範圍到功能overfeat
:
scope = 'overfeat'
is_training = True
output = overfeat.overfeat(inputs, num_classes, is_training,
dropout_keep_prob, scope=scope)
然後在測試模式下,我們創建了與reuse=True
相同的範圍。
scope = tf.VariableScope(reuse=True, name='overfeat')
is_training = False
output = overfeat.overfeat(inputs, num_classes, is_training,
dropout_keep_prob, scope=scope)
0
你可以只使用一個佔位符is_training:
isTraining = tf.placeholder(tf.bool)
# create nn
net = ...
net = slim.dropout(net,
keep_prob=0.5,
is_training=isTraining)
net = ...
# training
sess.run([net], feed_dict={isTraining: True})
# testing
sess.run([net], feed_dict={isTraining: False})
+1
我試過這個,並且遇到了問題,因爲變量沒有被重用。我也遇到了我無法解釋的內存限制。 –
0
這要看情況下,解決方案是不同的。
我的第一個選擇是使用不同的流程來進行評估。你只需要檢查是否有新的關卡和加載權納入評價網絡(與is_training=False
):
checkpoint = tf.train.latest_checkpoint(self.checkpoints_path)
# wait until a new check point is available
while self.lastest_checkpoint == checkpoint:
time.sleep(30) # sleep 30 seconds waiting for a new checkpoint
checkpoint = tf.train.latest_checkpoint(self.checkpoints_path)
logging.info('Restoring model from {}'.format(checkpoint))
self.saver.restore(session, checkpoint)
self.lastest_checkpoint = checkpoint
第二個選項是每一個時代後您卸載圖形,並創建一個新的評價用圖。這個解決方案浪費了很多時間加載和卸載圖形。
第三個選項是分享權重。但是給這些網絡添加隊列或數據集可能會導致問題,所以您必須非常小心。我只用於連體網絡。
with tf.variable_scope('the_scope') as scope:
your_model(is_training=True)
scope.reuse_variables()
your_model(is_training=False)
相關問題
- 1. 設置is_training爲false時,MobileNet不可用
- 2. FALSE和TRUE VS NULL和TRUE
- 3. objective-c中的YES/NO,TRUE/FALSE和true/false是否有區別?
- 4. String和True/False
- 5. jQuery true和false
- 6. 布爾當選擇true時帶回true和false值
- 7. fs.exists()是true和false
- 8. jQuery值true和false
- 9. false或true!= true或false!= true ||假
- 10. Tensorflow苗條列車和驗證啓動模型
- 11. 帶編輯模式的Telerik mvc網格true或false
- 12. TRUE和FALSE沒有在SUM()工作
- 13. jQuery UI模式沒有返回true和false
- 14. VB.Net true和false運算符
- 15. 使用TRUE,FALSE和NULL
- 16. OCR的Tensorflow模型
- 17. 從我的模型字段更改unique = True到unique = False
- 18. 如何獲得MVC3模型驗證的True或False輸出
- 19. Javascript布爾值:false && true true results in true
- 20. C#How false false == true?見圖
- 21. EnableViewstate = true/false
- 22. AutoEventWireup True Vs False
- 23. WCF中帶有DataContractSerializer和IsReference = true的StackOverflowException
- 24. PHP boolean TRUE/FALSE?
- 25. not(not false)= True?
- 26. 帶有枚舉模型的MVC模型
- 27. 與模型數據一起返回true/false
- 28. 壓縮TensorFlow模型
- 29. tensorflow模型彙編
- 30. 從模型到控制器傳遞true或false
我覺得默認的行爲是共享兩種情況之間的權重,所以你不要有什麼關係。 'tf-slim'使用'tf.get_variable()'來重用調用之間的變量。 –
好的,我認爲這主要是有效的。你需要確保'scope'被設置,然後爲了安全,最好也設置'reuse = True'。 –