3

這個問題對Python庫scikit-learn非常具體。請讓我知道是否將它發佈到其他地方是一個更好的主意。謝謝!在scikit-learn中訓練神經網絡時儘早停止

現在的問題......

我ffnn基於BaseEstimator我與SGD訓練前饋神經網絡類。它運行良好,我也可以使用GridSearchCV()並行訓練它。

現在我想實現在函數ffnn.fit()中儘早停止,但爲此我還需要訪問fold的驗證數據。這樣做的一個辦法是改變sklearn.grid_search.fit_grid_point(),它說

clf.fit(X_train, y_train, **fit_params) 

成類似

clf.fit(X_train, y_train, X_test, y_test, **fit_params) 

行,改變ffnn.fit()把這些參數。這也會影響sklearn中的其他分類器,這是一個問題。我可以通過檢查fit_grid_point()中的某種標誌來避免這種情況,該標誌告訴我何時以上述兩種方式調用clf.fit()。

有人可以建議一個不同的方式來做到這一點,我不必編輯sklearn庫中的任何代碼?

或者,將X_train和y_train隨機分爲火車/驗證集合並檢查一個好的停靠點,然後在所有X_train上重新訓練模型是否正確?

謝謝!

回答

7

通過使用train_test_split函數,您可以讓神經網絡模型在內部從已通過的X_trainy_train中提取驗證集。

編輯:

或者,會是正確的進一步分裂X_train和y_train到火車/驗證隨機檢查設置了一個良好的停車點,然後重新訓練所有X_train的模式?

是的,但那樣會很貴。你可以找到停靠點,然後只需要對你用來查找停靠點的驗證數據進行一次額外的傳遞。

+0

謝謝! @ogrisel:驗證數據是否足夠通過?我怎樣才能檢查它是否可以通過多次傳球獲得更好的效果? – user1953384

+0

您可以將最終測試分數與原始但成本較高的測試分數進行比較。 – ogrisel

+0

謝謝!對於這個微不足道的問題感到抱歉。這當然是要做的事:)。 – user1953384

0

有兩種方式:

第一:

雖然採取了x_train和x_test分裂。你可以拿一個0。從x_train 1分,並保持它的有效性x_dev:

x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size=0.25) 

x_train, x_dev, y_train, y_dev = train_test_split(x_train, y_train, test_size=0.1) 

clf = GridSearchCV(YourEstimator(), param_grid=param_grid,) 
clf.fit(x_train, y_train, x_dev, y_dev) 

而且你估計會像下面和實施早期停止與x_dev,y_dev

class YourEstimator(BaseEstimator, ClassifierMixin): 
    def __init__(self, param1, param2): 
     # perform initialization 
     # 

    def fit(self, x, y, x_dev=None, y_dev=None): 
     # perform training with early stopping 
     # 

你不會在x_train上執行第二次分割,但會取出估計器的擬合方法中的開發集。

x_train, x_test, y_train, y_test = train_test_split(data_x, data_y, test_size=0.25) 

clf = GridSearchCV(YourEstimator(), param_grid=param_grid) 
clf.fit(x_train, y_train) 

而且你估計將類似於以下內容:

class YourEstimator(BaseEstimator, ClassifierMixin): 
    def __init__(self, param1, param2): 
     # perform initialization 
     # 

    def fit(self, x, y): 
     # perform training with early stopping 
     x_train, x_dev, y_train, y_dev = train_test_split(x, y, 
                 test_size=0.1)