Tensorflow 坑(和一些說明)

工作中遇到的一些問題,隨手記下
來自我的cmd https://www.zybuluo.com/jiehanwang/note/770502

    • densenet 耗費顯存的解釋:

    不少人跟我們反映過 DenseNet 在訓練時對內(nèi)存消耗非常厲害。這個問題其實是算法實現(xiàn)不優(yōu)帶來的。當前的深度學習框架對 DenseNet 的密集連接沒有很好的支持,我們只能借助于反復的拼接(Concatenation)操作,將之前層的輸出與當前層的輸出拼接在一起,然后傳給下一層。對于大多數(shù)框架(如 Torch 和 TensorFlow),每次拼接操作都會開辟新的內(nèi)存來保存拼接后的特征。這樣就導致一個 L 層的網(wǎng)絡,要消耗相當于 L(L+1)/2 層網(wǎng)絡的內(nèi)存(第 l 層的輸出在內(nèi)存里被存了 (L-l+1) 份)。來自這里

    • tfrecord數(shù)據(jù)過大,導致訓練遲遲不能運行(減少訓練數(shù)據(jù)確實可以運行)。tfrecord為何需要如此巨大的cpu內(nèi)存???

    • densenet 隨著計算進行,CPU內(nèi)存也逐漸增大,如下圖:


      image_1br3j3cm319i61qq5qei97mgmhg.png-59.8kB
    • 在densenet_small代碼中,用Momentum可以達到90%以上,而GD只能到80%左右。

    opt = tf.train.MomentumOptimizer(lr, 0.9, use_nesterov=True)
    # opt = tf.train.GradientDescentOptimizer(lr)
    
  1. 七個小貼士,順利提升TensorFlow模型訓練表現(xiàn)。鏈接

  2. tfrecord的數(shù)據(jù)必須保持完整,不然訓練的時候會發(fā)生截斷錯誤,導致退出

  3. retrain 報錯:模型大小不能超過67M,新版的tf據(jù)說已經(jīng)解決了這個問題,mac上運行也沒有問題。但是yard估計沒有更新。加入以下語句即可解決。export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

  4. A placeholder generates an error if it is executed without a feed, so you won't forget to feed it. 如果用feeding輸入數(shù)據(jù)的話,最好用placeholder,這樣,如果沒有輸入,在編譯的時候就會報錯。

  5. sv = tf.train.Supervisor(logdir=log_path, init_op=init) 會判斷模型是否存在.如果存在,會自動讀取模型.不用顯式地調(diào)用restore。如果訓練程序shut down或者crash了,那么它最近的checkpoint以及事件文件就被留到了logdir里。當你重啟程序的時候,managed_session會從最近的checkpoint中加載圖,并且從中止的地方恢復訓練。
    再youtube-8m的train代碼中就是這種方式。

  6. 如果模型包含一個標量整型變量名字為global_step,則該變量的值會被添加到checkpoint文件名中。例如,在global step 1234,checkpoint 文件名就是 “model.ckpt-1234”?!緔outube-8m中的模型保存方式】

  7. 深度學習theano/tensorflow多顯卡多人使用問題集: https://zhuanlan.zhihu.com/p/23250782 里面提到,多人用同一塊卡的時候容易出現(xiàn)一方的內(nèi)存不足。TF默認會占用所有的顯存。以及,如何設置TF占用顯存的百分比。

  8. 當保存的模型帶有設備信息時,可以用以下方法去除,這樣在gpu上訓練的模型就可以在只有cpu的機器上運行。

When importing such models, it's useful to be able to clear the device settings in the graph so that we can run it on locally available devices. This can be achieved by calling import_meta_graph with the clear_devices option set to True.

  1. 解決“device specification '/device:GPU:3' because no supported kernel for GPU devices is available.”問題的方法(這是一個坑)有些操作只能在cpu進行,不按照以下方式進行,可能會出錯

I just follow mrry's suggestion here, adding "allow_soft_placement=True" as follows: config = tf.ConfigProto(allow_soft_placement = True) sess = tf.Session(config = config) Then it works. I reviewed the Using GPUs in tutorial. It mentions adding "allow_soft_placement" under the error "Could not satisfy explicit device specification '/gpu:X' ". But it not mentions it could also solve the error "no supported kernel for GPU devices is available". Maybe it's better to add this in tutorial text in order to avoid confusing future users.

  1. densenet并行化中,出現(xiàn)過的問題匯總:

1). ValueError: No variables to save。這是因為沒有用同一個graph的命名空間。
2). ValueError: Variable BatchNorm/beta does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?
3). ValueError: Variable block_1/weight_conv_1 already exists, disallowed. Did you mean to set reuse=True in VarScope?
4). Variable conv2d/weight_conv_0/ExponentialMovingAverage/ does not exist, or was not created with tf.get_variable(). Did you mean to set reuse=None in VarScope?

  1. 下面的這篇并行方式值得參考:http://geek.csdn.net/news/detail/191567

  2. 如果想要達到重復利用變量的效果, 我們就要使用 tf.variable_scope(), 并搭配 tf.get_variable() 這種方式產(chǎn)生和提取變量. 不像 tf.Variable() 每次都會產(chǎn)生新的變量, tf.get_variable() 如果遇到了同樣名字的變量時, 它會單純的提取這個同樣名字的變量(避免產(chǎn)生新變量). 而在重復使用的時候, 一定要在代碼中強調(diào) scope.reuse_variables(), 否則系統(tǒng)將會報錯, 以為你只是單純的不小心重復使用到了一個變量.

  3. Tensorflow cifar10_multi_gpu問題:"Variable conv1/weights/ExponentialMovingAverage/ does not exist". you can find the answer to your problem here: Issue 6220

You need to put: with tf.variable_scope(tf.get_variable_scope()) in front of the loop which runs over your devices ... so, do that:

with tf.variable_scope(tf.get_variable_scope()):
    for i in xrange(FLAGS.num_gpus):
        with tf.device('/gpu:%d' % i): 

The explanation is given in the link... Here the quote: When you do tf.get_variable_scope().reuse_variables() you set the current scope to reuse variables. If you call the optimizer in such scope, it's trying to reuse slot variables, which it cannot find, so it throws an error. If you put a scope around, the tf.get_variable_scope().reuse_variables() only affects that scope, so when you exit it, you're back in the non-reusing mode, the one you want.Hope that helps, let me know if I should clarify more.

  1. 如果要在tfrecord 上加上limit epoch數(shù)值如下:

    filename_queue = tf.train.string_input_producer(file_, num_epochs = FLAGS.num_epoch)
    

    需要初始化如下,否則會報錯

    session.run(tf.global_variables_initializer())
    session.run(tf.local_variables_initializer())
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=session, coord=coord)
    
  2. train 和 test的數(shù)據(jù)流切換實現(xiàn)training的時候eval。參考這下面的鏈接。主要用到了tf.cond() 和 tf.QueueBase.from_list()函數(shù)【我沒有搞定】

https://stackoverflow.com/questions/41162955/tensorflow-queues-switching-between-train-and-validation-data

  1. tfrecord 清點里面的數(shù)據(jù)數(shù)量

    c = 0
    for fn in tf_records_filenames:
      for record in tf.python_io.tf_record_iterator(fn):
         c += 1
    
  2. Densenet 采用內(nèi)存優(yōu)化的代碼,其實就是給每個block加了一層1*1的卷積?這個理解有誤,需要進一步查看文獻

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 11,018評論 0 23
  • 終于,你也走了。你們在那邊一定好好的。帶給我們的歡樂永生難忘。乖,小虎。 心痛到極致時會忘記一切。
    執(zhí)念馥郁閱讀 146評論 0 0
  • 我們躲在虛掩的世界里 分頭去瘋 分頭去疼痛 重逢 遙遙無期 為你寫一百首詩 每一個字像淚一樣流淌 像雪一樣盛放 你...
    月宛央閱讀 172評論 0 0
  • 讀書的時候,特想有幅眼鏡。那時候還不時興文藝范兒,也不流行復古風,就覺得戴幅眼鏡很有味道。自從有了這個夢想之后,...
    薇禾閱讀 450評論 0 0

友情鏈接更多精彩內(nèi)容