淺談keras 模型用于預測時的注意事項
為什么訓練誤差比測試誤差高很多?
一個Keras的模型有兩個模式:訓練模式和測試模式。一些正則機制,如Dropout,L1/L2正則項在測試模式下將不被啟用。
另外,訓練誤差是訓練數(shù)據(jù)每個batch的誤差的平均。在訓練過程中,每個epoch起始時的batch的誤差要大一些,而后面的batch的誤差要小一些。另一方面,每個epoch結束時計算的測試誤差是由模型在epoch結束時的狀態(tài)決定的,這時候的網(wǎng)絡將產(chǎn)生較小的誤差。
【Tips】可以通過定義回調函數(shù)將每個epoch的訓練誤差和測試誤差并作圖,如果訓練誤差曲線和測試誤差曲線之間有很大的空隙,說明你的模型可能有過擬合的問題。當然,這個問題與Keras無關。
在keras中文文檔中指出了這一誤區(qū),筆者認為產(chǎn)生這一問題的原因在于網(wǎng)絡實現(xiàn)的機制。即dropout層有前向實現(xiàn)和反向實現(xiàn)兩種方式,這就決定了概率p是在訓練時候設置還是測試的時候進行設置
利用預訓練的權值進行Fine tune時的注意事項:
不能把自己添加的層進行將隨機初始化后直接連接到前面預訓練后的網(wǎng)絡層
in order to perform fine-tuning, all layers should start with properly trained weights: for instance you should not slap a randomly initialized fully-connected network on top of a pre-trained convolutional base. This is because the large gradient updates triggered by the randomly initialized weights would wreck the learned weights in the convolutional base. In our case this is why we first train the top-level classifier, and only then start fine-tuning convolutional weights alongside it.
we choose to only fine-tune the last convolutional block rather than the entire network in order to prevent overfitting, since the entire network would have a very large entropic capacity and thus a strong tendency to overfit. The features learned by low-level convolutional blocks are more general, less abstract than those found higher-up, so it is sensible to keep the first few blocks fixed (more general features) and only fine-tune the last one (more specialized features).
fine-tuning should be done with a very slow learning rate, and typically with the SGD optimizer rather than an adaptative learning rate optimizer such as RMSProp. This is to make sure that the magnitude of the updates stays very small, so as not to wreck the previously learned features.
補充知識:keras框架中用keras.models.Model做的時候預測數(shù)據(jù)不是標簽的問題
我們發(fā)現(xiàn),在用Sequential去搭建網(wǎng)絡的時候,其中有predict和predict_classes兩個預測函數(shù),前一個是返回的精度,后面的是返回的具體標簽。但是,在使用keras.models.Model去做的時候,就會發(fā)現(xiàn),它只有一個predict函數(shù),沒有返回標簽的predict_classes函數(shù),所以,針對這個問題,我們將其改寫。改寫如下:
def my_predict_classes(predict_data): if predict_data.shape[-1] > 1: return predict_data.argmax(axis=-1) else: return (predict_data > 0.5).astype('int32') # 這里省略網(wǎng)絡搭建部分。。。。 model = Model(data_input, label_output) model.compile(loss='categorical_crossentropy', optimizer=keras.optimizers.Nadam(lr=0.002), metrics=['accuracy']) model.summary() y_predict = model.predict(X_test) y_pre = my_predict_classes(y_predict)
這樣,y_pre就是具體的標簽了。
以上這篇淺談keras 模型用于預測時的注意事項就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Python中SOAP項目的介紹及其在web開發(fā)中的應用
這篇文章主要介紹了Python中的SOAP項目及其在web開發(fā)中的應用,本文來自于IBM官方網(wǎng)站技術文檔,需要的朋友可以參考下2015-04-04探究數(shù)組排序提升Python程序的循環(huán)的運行效率的原因
這篇文章主要介紹了探究數(shù)組排序提升Python程序的循環(huán)的運行效率的原因,作者用代碼實踐了多個小片段來進行對比解釋,需要的朋友可以參考下2015-04-04anaconda安裝pytorch1.7.1和torchvision0.8.2的方法(親測可用)
這篇文章主要介紹了anaconda安裝pytorch1.7.1和torchvision0.8.2的方法(親測可用),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2021-02-02python實現(xiàn)郵件循環(huán)自動發(fā)件功能
這篇文章主要介紹了python實現(xiàn)郵件循環(huán)自動發(fā)件功能,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-09-09