python
  • machine-learning
  • keras
  • 2017-07-03 66 views 3 likes 
    3

    我正在關注this blog,並且因爲它在鏈接的博客中使用而難以實施保存檢查點。在第23行使用:Keras保存檢查點

    filepath="weights-improvement-{epoch:02d}-{val_acc:.2f}.hdf5"

    於是,我就調整代碼一點點更有活力:

    filepath = '{0}/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'.format(directory)

    ,我想給定架構的所有檢查站存放在1個目錄,例如:./architecture1/checkpoints/

    ,但我得到了以下錯誤:KeyError: 'epoch'。我在這裏做錯了什麼?

    P.S. .: filepath = "./checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5"的作品,但它保存了我不想要的1個目錄中的所有檢查點。

    +0

    你嘗試導入操作系統; filepath = os.path.join(目錄,'checkpoints','checkpoint- {epoch:02d} - {val_loss:.2f} .hdf5')? –

    +0

    如何將一個目錄分配給模型? –

    +0

    其實我只是發現這個問題可以用簡單的字符串連接來解決。 – narn

    回答

    1

    如果你想使用format,正確的方式是逃避括號這樣的:

    filepath = '{0}/checkpoints/checkpoint-{{epoch:02d}}-{{val_loss:.2f}}.hdf5'.format(directory) 
    

    所以,如果directory = 'weights'filepath'weights/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'

    (小心如果directory包含{}

    +0

    這很好。非常感謝。 – narn

    +0

    @narn我發佈,作爲我的第二個解決方案,你說它沒有工作:/ – bluesummers

    +0

    @bluesummers不正確。你的第二個解決方案是這樣的:'filepath ='{dir}/checkpoints/checkpoint - {{epoch:02d}} - {{val_loss:.2f}}。hdf5'.format(dir ='directory')'which沒有工作。而弗朗西斯科的解決方案是逃避括號。 – narn

    -1

    我發現正常字符串連接起作用。含義這個工程:

    filepath = directory + '/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5'

    我不知道爲什麼.format()不工作,如果有人能闡述我會很高興聽到的原因。

    1

    的問題是,你在一個format符合條件的字符串,但只供應的關鍵之一使用format - 這將導致錯誤。

    你在做什麼是

    "{0} some text here {epoch:02d}".format("text") 
    

    並導致一個錯誤,因爲它看起來第二鍵並不能找到它。

    如果你想你的代碼是動態的,我會做的是:

    "{0}".format(directory) + "/checkpoints/checkpoint-{epoch:02d}-{val_loss:.2f}.hdf5" 
    
    +0

    其實第二個例子不起作用,它仍然給'KeyError:'epoch''錯誤。第一個例子雖然工作。 – narn

    +0

    真的嗎?令人驚訝的是,謝謝你的反饋,我會編輯它 – bluesummers

    相關問題