python
1import tensorflow.keras as keras 2from tensorflow.keras.utils import to_categorical 3from tensorflow.python.keras.models import Sequential, Model 4from tensorflow.python.keras.layers.convolutional import Conv2D, MaxPooling2D 5from tensorflow.python.keras.layers.core import Dense, Dropout, Activation, Flatten 6import numpy as np 7from tensorflow.keras.optimizers import SGD, Adam 8from sklearn.model_selection import train_test_split 9from PIL import Image 10import glob 11from tensorflow.python.keras.utils import np_utils 12from tensorflow.keras.applications import VGG16 13 14# モデルの定義 15 16def build_model(activation,optimizer): 17 18 model = VGG16(weights='imagenet', include_top=False, input_shape=(img_size,img_size,3)) 19 20 top_model = Sequential() 21 top_model.add(Flatten(input_shape=model.output_shape[1:])) 22 top_model.add(Dense(256,activation=activation)) 23 top_model.add(Dropout(0.5)) 24 top_model.add(Dense(num_classes,activation='softmax')) 25 26 model = Model(inputs=model.input, outputs=top_model(model.output)) 27 model.summary() 28 29 for layer in model.layers[:15]: 30 layer.trainable = False 31 32 33 model.compile(loss='categorical_crossentropy', optimizer=optimizer,metrics=['accuracy']) 34import numpy as np 35import pandas as pd 36from pandas import Series, DataFrame 37import sklearn 38import pathlib 39from sklearn import datasets, preprocessing 40from sklearn.model_selection import GridSearchCV 41from tensorflow.keras.wrappers.scikit_learn import KerasClassifier 42 43#グリッドサーチ対象のハイパーパラメーターを準備 44activation = ["relu", "sigmoid"] 45optimizer = ["adam",'adagrad','sgd'] 46nb_epoch = [30 ,50] 47batch_size = [10, 16] 48 49#グリッドサーチ対象のハイパーパラメーターを辞書型にまとめる 50param_grid = dict(activation=activation, optimizer=optimizer,nb_epoch=nb_epoch, batch_size=batch_size) 51 52#モデルを作成 53model = KerasClassifier(build_fn = build_model, verbose=0) 54 55#グリッドサーチの実行 56grid = GridSearchCV(estimator=model, param_grid=param_grid) 57 58grid_result = grid.fit(X_train, Y_train)
AttributeError Traceback (most recent call last)
<ipython-input-13-d4c36aa50258> in <module>
5 grid = GridSearchCV(estimator=model, param_grid=param_grid)
6
----> 7 grid_result = grid.fit(X_train, Y_train)
~/anaconda3/lib/python3.7/site-packages/sklearn/model_selection/search.py in fit(self, X, y, groups, **fit_params)
737 refit_start_time = time.time()
738 if y is not None:
--> 739 self.best_estimator.fit(X, y, **fit_params)
740 else:
741 self.best_estimator_.fit(X, **fit_params)
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
221 raise ValueError('Invalid shape for y: ' + str(y.shape))
222 self.n_classes_ = len(self.classes_)
--> 223 return super(KerasClassifier, self).fit(x, y, **kwargs)
224
225 def predict(self, x, **kwargs):
~/anaconda3/lib/python3.7/site-packages/tensorflow/python/keras/wrappers/scikit_learn.py in fit(self, x, y, **kwargs)
157 self.model = self.build_fn(**self.filter_sk_params(self.build_fn))
158
--> 159 if (losses.is_categorical_crossentropy(self.model.loss) and
160 len(y.shape) != 2):
161 y = to_categorical(y)
AttributeError: 'NoneType' object has no attribute 'loss'
このエラーの原因がわからず困っております。
原因がわかるかたがいたらご教示いただきたいです。よろしくお願い致します。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。