前提・実現したいこと
<環境>
Anaconda, tensorflow-gpu, GPyOpt, python3.6, Windows10
CNNにおけるハイパーパラメータの最適化プログラムを以下のサイトを参考にCNNに組み込もうとしたのですが、
形状はランク1でなければなりませんが、入力形状が[1,15,1,1]、[]の 'batch_normalization_1 / cond / Reshape_4'(op: 'Reshape')ではランク0です。
というエラーが出てきてしまい正常に動きません。
レイヤー自体は以前実装したCNNをもとに作っていますが、そちらは正常に動作しますので、ほかに原因があるのでしょうか。
最適化したいパラメータ
Convolutionによる出力フィルタの数(conv1 ~ conv5)
全結合層に置ける出力ノードの数(l1_out, l2_out)
ドロップアウト層におけるドロップアウト率(l1_drop, l2_drop)
エポック数(epochs)
バッチサイズ(batch_size)
バリデーションデータセットの割合(validation_split)
学習率(learning rate)
ベイズ最適化のKeras DNNモデルへの適用
Using Bayesian Optimization to optimize hyper parameter in Keras-made neural network model.
発生している問題・エラーメッセージ
shape must be rank 1 but is rank 0 for 'batch_normalization_1/cond/Reshape_4' (op: 'Reshape') with input shapes: [1,15,1,1], [].
該当のソースコード
import GPy, GPyOpt import numpy as np import tensorflow as tf from keras.layers import Activation, Dropout, BatchNormalization, Dense, Conv2D, MaxPooling2D from keras.models import Sequential from keras.datasets import cifar10 from keras.utils import np_utils from keras.metrics import categorical_crossentropy from keras.utils import np_utils from keras.callbacks import EarlyStopping
class CNN () : def __init__(self, conv1_depth=96, conv2_depth=256, conv3_depth=384, conv4_depth=384, conv5_depth=384, l1_out=768, l2_out=768, l1_drop=0.5, l2_drop=0.5, epochs=10, batch_size=1000, validation_split=0.25, learning_rate=0.01): self.conv1_depth = conv1_depth self.conv2_depth = conv2_depth self.conv3_depth = conv3_depth self.conv4_depth = conv4_depth self.conv5_depth = conv5_depth self.l1_out = l1_out self.l2_out = l2_out self.l1_drop = l1_drop self.l2_drop = l2_drop self.epochs = epochs self.batch_size = batch_size self.validation_split = validation_split self.leraning_rate = learning_rate self.__x_train, self.__x_test, self.__y_train, self.__y_test = self.cifar10_data() self.__model = self.CNN_model() #load cifar10 def cifar10_data(self): (X_train, y_train), (X_test, y_test) = cifar10.load_data() #正規化 X_train = X_train / 255.0 X_test = X_test / 255.0 #データセットのラベルをone-hot表現に変換 Y_train = np_utils.to_categorical(y_train) Y_test = np_utils.to_categorical(y_test) return X_train, X_test, Y_train, Y_test #CNN model def CNN_model(self): model = Sequential() #1st convolutional layer model.add(Conv2D(self.conv1_depth, 4, strides=(2, 2), padding='valid', data_format='channels_last', activation='relu', bias_initializer='ones', input_shape=(32,32,3))) model.add(BatchNormalization(axis=1)) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid', data_format='channels_last')) #2nd convolutional layer model.add(Conv2D(self.conv2_depth, 4, strides=(2, 2), padding='valid', data_format='channels_last', activation='relu', bias_initializer='zeros')) model.add(BatchNormalization(axis=1)) model.add(MaxPooling2D(pool_size=(1, 1), strides=(1, 1), padding='valid', data_format='channels_last')) #3rd~5th convolutional layer model.add(Conv2D(self.conv3_depth, 2, strides=(1, 1), padding='valid', data_format='channels_last', activation='relu', bias_initializer='zeros')) model.add(Conv2D(self.conv4_depth, 2, strides=(1, 1), padding='valid', data_format='channels_last', activation='relu', bias_initializer='ones')) model.add(Conv2D(self.conv5_depth, 2, strides=(1, 1), padding='valid', data_format='channels_last', activation='relu', bias_initializer='ones')) model.add(MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding='valid', data_format='channels_last')) # 6th flatten and whole junction layer model.add(Flatten()) model.add(Dense(self.l1_out)) model.add(Activation('relu')) model.add(Dropout(self.l1_drop)) model.add(Dense(self.l2_out)) model.add(Activation('relu')) model.add(Dropout(self.l2_drop)) #7th Softmax layer model.add(Dense(10, activation='softmax')) #compile model.compile(optimizer=tf.keras.optimizers.SGD(lr=self.learning_rate), loss='categorical_crossentropy', metrics=['accuracy']) return model #CNN model fit def CNN_fit(self): early_stopping = EarlyStopping(patience=0, verbose=1) self.__model.fit(self.__x_train, self.__y_train, batch_size=self.batch_size, epochs=self.epochs, verbose=0, validation_split=self.validation_split, callbacks=[early_stopping]) #evaluate CNN model def CNN_evaluate(self): self.CNN_fit() evaluation = self.__model.evaluate(self.__x_test, self.__y_test, batch_size=self.batch_size, verbose=0) return evaluation
# function to run cnn net class def run_CNN(conv1_depth=96, conv2_depth=256, conv3_depth=384, conv4_depth=384, conv5_depth=384, l1_out=768, l2_out=768, l1_drop=0.5, l2_drop=0.5, epochs=10, batch_size=1000, validation_split=0.25, learning_rate=0.01): _cnn = CNN(conv1_depth=conv1_depth, conv2_depth=conv2_depth, conv3_depth=conv3_depth, conv4_depth=conv4_depth, conv5_depth=conv5_depth, l1_out=l1_out, l2_out=l2_out, l1_drop=l1_drop, l2_drop=l2_drop, epochs=epochs, batch_size=batch_size, validation_split=validation_split, learning_rate=learning_rate) cnn_evaluation = _cnn.CNN_evaluate() return cnn_evaluation
# bounds for hyper-parameters in cnn model # the bounds dict should be in order of continuous type and then discrete type bounds = [{'name': 'validation_split', 'type': 'discrete', 'domain': (0.0, 0.3)}, {'name': 'conv1_depth', 'type': 'discrete', 'domain': (32, 64, 128)}, {'name': 'conv2_depth', 'type': 'discrete', 'domain': (32, 64, 128, 256)}, {'name': 'conv3_depth', 'type': 'discrete', 'domain': (32, 64, 128, 256, 512)}, {'name': 'conv4_depth', 'type': 'discrete', 'domain': (32, 64, 128, 256, 512)}, {'name': 'conv5_depth', 'type': 'discrete', 'domain': (32, 64, 128, 256, 512)}, {'name': 'l1_drop', 'type': 'discrete', 'domain': (0.0, 0.3, 0,5)}, {'name': 'l2_drop', 'type': 'discrete', 'domain': (0.0, 0.3, 0.5)}, {'name': 'l1_out', 'type': 'discrete', 'domain': (256, 512, 1024)}, {'name': 'l2_out', 'type': 'discrete', 'domain': (256, 512, 1024)}, {'name': 'batch_size', 'type': 'discrete', 'domain': (10, 100, 500, 1000, 2000, 5000)}, {'name': 'epochs', 'type': 'discrete', 'domain': (5, 10, 20, 100)}, {'name': 'learning_rate', 'type': 'discrete', 'domain': (0.001, 0.01, 0.1, 1)}]
# function to optimize mnist model def f(x): print(x) evaluation = run_CNN( conv1_depth = float(x[:,1]), conv2_depth = float(x[:,2]), conv3_depth = float(x[:,3]), conv4_depth = float(x[:,4]), conv5_depth = float(x[:,5]), l1_drop = float(x[:,6]), l2_drop = float(x[:,7]), l1_out = float(x[:,8]), l2_out = float(x[:,9]), batch_size = int(x[:,10]), epochs = int(x[:,11]), learning_rate = int(x[:,12]), validation_split = float(x[:,0])) print("LOSS:\t{0} \t ACCURACY:\t{1}".format(evaluation[0], evaluation[1])) print(evaluation) return evaluation[0]
# optimizer opt_mnist = GPyOpt.methods.BayesianOptimization(f=f, domain=bounds)
ValueError: Shape must be rank 1 but is rank 0 for 'batch_normalization_1/cond/Reshape_4' (op: 'Reshape') with input shapes: [1,15,1,1], [].
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。