import keras from keras.datasets import mnist (x_train, y_train), (x_test, y_test) = mnist.load_data() num_classes = 10 x_train = x_train.reshape(60000, 784) x_test = x_test.reshape(10000, 784) x_train = x_train.astype('float32') x_test = x_test.astype('float32') x_train /= 255 x_test /= 255 y_train = y_train.astype('int32') y_test = y_test.astype('int32') y_train = keras.utils.np_utils.to_categorical(y_train, num_classes) y_test = keras.utils.np_utils.to_categorical(y_test, num_classes) print(x_train.shape[0], 'train samples') print(x_test.shape[0], 'test samples') from keras.models import Sequential from keras.layers import Dense, Dropout, Activation from keras.optimizers import RMSprop model = Sequential() model.add(Dense(512, activation = "relu", input_shape=(784, ))) model.add(Dropout(0.2)) model.add(Dense(512, activation = "relu")) model.add(Dropout(0.2)) model.add(Dense(10, activation = "softmax")) model.compile(loss = "categorical_crossentropy", optimizer=RMSprop(), metrics=['accuracy']) batch_size = 128 epochs = 20 history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
ここまでを、jupyter-notebookで書いたのですが、最後の
batch_size = 128 epochs = 20 history = model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, verbose=1, validation_data=(x_test, y_test))
を実行したところ、
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-24-3d17f9e6f4f4> in <module>() 3 history = model.fit(x_train, y_train, 4 batch_size=batch_size, epochs=epochs, ----> 5 verbose=1, validation_data=(x_test, y_test)) ~/anaconda3/lib/python3.6/site-packages/keras/models.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, **kwargs) 958 initial_epoch=initial_epoch, 959 steps_per_epoch=steps_per_epoch, --> 960 validation_steps=validation_steps) 961 962 def evaluate(self, x, y, batch_size=32, verbose=1, ~/anaconda3/lib/python3.6/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, **kwargs) 1579 class_weight=class_weight, 1580 check_batch_axis=False, -> 1581 batch_size=batch_size) 1582 # Prepare validation data. 1583 do_validation = False ~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in _standardize_user_data(self, x, y, sample_weight, class_weight, check_batch_axis, batch_size) 1416 output_shapes, 1417 check_batch_axis=False, -> 1418 exception_prefix='target') 1419 sample_weights = _standardize_sample_weights(sample_weight, 1420 self._feed_output_names) ~/anaconda3/lib/python3.6/site-packages/keras/engine/training.py in _standardize_input_data(data, names, shapes, check_batch_axis, exception_prefix) 139 ' to have ' + str(len(shapes[i])) + 140 ' dimensions, but got array with shape ' + --> 141 str(array.shape)) 142 for j, (dim, ref_dim) in enumerate(zip(array.shape, shapes[i])): 143 if not j and not check_batch_axis: ValueError: Error when checking target: expected dense_6 to have 2 dimensions, but got array with shape (60000, 10, 10, 10, 10)
と、なりました。
解決方法をお教えください。
回答お待ちしております。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。