LSTMのハイパパラメータをグリッドサーチによって調整したいです。
しかしcodeを実行すると以下のエラー文が出てしまいます。
python3
1def grid(df,length_of_sequences): 2 (X_train, y_train), (X_test, y_test) = train_test_split(df,test_size=0.2,n_prev=length_of_sequences) 3 out_neurons = 1 4 in_neurons=len(df.columns) 5 hidden_neurons = 300 6 model = Sequential() 7 model.add(LSTM(hidden_neurons, batch_input_shape=(None, length_of_sequences, in_neurons), kernel_initializer = glorot_uniform(seed=20201119),return_sequences=False)) 8 model.add(Dense(out_neurons)) 9 model.add(Activation("linear")) 10 model.compile(loss="mean_squared_error", optimizer="adam",) 11 #グリッドサーチ対象のハイパーパラメーターを準備 12 activation = ["linear","relu", "sigmoid"] 13 optimizer = ["adam", "adagrad"] 14 batch_size = [32, 64,128] 15 16 #グリッドサーチ対象のハイパーパラメーターを辞書型にまとめる 17 param_grid = dict(activation=activation, optimizer=optimizer, batch_size=batch_size) 18 19 #グリッドサーチの実行 20 grid = GridSearchCV(estimator=model, param_grid=param_grid) 21 scores = 'precision'#['precision', 'recall'] 22 grid_result = grid.fit(X_train, y_train,scoring="accuracy") 23 return grid_result
116 grid = GridSearchCV(estimator=model, param_grid=param_grid) 117 scores = 'precision'#['precision', 'recall'] --> 118 grid_result = grid.fit(X_train, y_train,scoring="accuracy") 119 return grid_result 120 /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/model_selection/_search.py in fit(self, X, y, groups, **fit_params) 593 594 scorers, self.multimetric_ = _check_multimetric_scoring( --> 595 self.estimator, scoring=self.scoring) 596 597 if self.multimetric_: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/metrics/scorer.py in _check_multimetric_scoring(estimator, scoring) 340 if callable(scoring) or scoring is None or isinstance(scoring, 341 six.string_types): --> 342 scorers = {"score": check_scoring(estimator, scoring=scoring)} 343 return scorers, False 344 else: /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/sklearn/metrics/scorer.py in check_scoring(estimator, scoring, allow_none) 297 "If no scoring is specified, the estimator passed should " 298 "have a 'score' method. The estimator %r does not." --> 299 % estimator) 300 else: 301 raise ValueError("scoring value should either be a callable, string or" TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator <keras.engine.sequential.Sequential object at 0x2225b1b00> does not.
評価方法は
grid_result = grid.fit(X_train, y_train,scoring="accuracy")
にてaccuracyを指定しているはずなのですがエラーが出てしまいます。
原因が調べても分からなかったためどなたか知恵をお貸しいただけると幸いです。
あなたの回答
tips
プレビュー