前提・実現したいこと
tensorflow,kerasでCNNを実装させたいと考えています
発生している問題・エラーメッセージ
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-26-4e988137566f> in <module> 39 model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=["accuracy"]) 40 history = model.fit(X_train, Y_train, batch_size=5, nb_epoch=10, verbose=1, ---> 41 validation_split=0.4) C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs) 1152 sample_weight=sample_weight, 1153 class_weight=class_weight, -> 1154 batch_size=batch_size) 1155 1156 # Prepare validation data. C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_array_lengths, batch_size) 577 feed_input_shapes, 578 check_batch_axis=False, # Don't enforce the batch size. --> 579 exception_prefix='input') 580 581 if y is not None: C:\ProgramData\Anaconda3\lib\site-packages\keras\engine\training_utils.py in standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 133 ': expected ' + names[i] + ' to have ' + 134 str(len(shape)) + ' dimensions, but got array ' --> 135 'with shape ' + str(data_shape)) 136 if not check_batch_axis: 137 data_shape = data_shape[1:] ValueError: Error when checking input: expected conv2d_41_input to have 4 dimensions, but got array with shape (45, 250, 250)
該当のソースコード
X_train,X_test,y_train,y_test = train_test_split(X,y,random_state=0) X_train = X_train.astype('float32') X_test = X_test.astype('float32') X_train /= 255.0 X_test /= 255.0 Y_train = np_utils.to_categorical(y_train, 3) Y_test = np_utils.to_categorical(y_test, 3) # モデルの定義 model = Sequential() model.add(Conv2D(16,(7,7),strides=(1,1),padding='valid',input_shape=(250,250,1))) model.add(Activation('relu')) model.add(MaxPool2D(pool_size=(2,2),strides=(2,2))) model.add(Conv2D(30,(3,3),strides=(1,1),padding='valid')) model.add(MaxPool2D(pool_size=(2,2),strides=(2,2))) model.add(Conv2D(16,(3,3),strides=(1,1),padding='same')) model.add(Activation('relu')) model.add(MaxPool2D(pool_size=(2,2),strides=(2,2))) model.add(Conv2D(10,(3,3),strides=(1,1),padding='same')) model.add(Activation('relu')) model.add(MaxPool2D(pool_size=(1,3),strides=(2,2))) model.add(Activation('relu')) model.add(Dense(10)) model.add(Activation('relu')) model.add(Dense(3, activation='softmax')) adam = Adam(lr=1e-8) model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=["accuracy"]) history = model.fit(X_train, Y_train, batch_size=5, nb_epoch=10, verbose=1, validation_split=0.4)
試したこと
入力データなど確認しましたが、特に異常はありませんでした
補足情報(FW/ツールのバージョンなど)
tensorflow-gpu 2.2.0
Keras 2.3.1
回答1件
あなたの回答
tips
プレビュー