1

を決定し、私はそれは私がkerasシーケンシャルモデルを作りたい画像サイズ28 * 28はKerasのMnist入力形状

と60000個のチャネルを意味

(60000, 28, 28) 

としてxtrain.shapeてきました。

input_shapeがどのように見えるべきモデル形状

model = Sequential() 
model.add(Convolution2D(32,3,activation='relu',input_shape=(????))) 
model.add(Dense(10, activation='relu')) 
model.summary() 

を指定しますか?私はこれを入れたときに、これは4次元を必要とする理由

model = Sequential() 
model.add(Dense(64,input_shape=(1,28,28))) 

は、私は次のエラー

Error when checking input: expected dense_31_input to have 4 dimensions, but got array with shape (60000, 28, 28) 

を得ましたか。コードをフォームで修正する方法は?

答えて

0

データを(60000,28,28,1)または(60000,1,28,28)に変更しようとします。

0

まず1、

model = Sequential() 
model.add(Convolution2D(32,3,activation='relu',input_shape=(60000,28,28))) 
model.add(Dense(10, activation='relu')) 
model.summary() 

2つ目、

model = Sequential() 
model.add(Dense(64,input_shape=(None,60000,28,28))) 
1

私はそれは画像サイズ28 * 28

と60000個のチャネルを意味

(60000, 28, 28) 

としてxtrain.shapeてきました

まあ、それは確かにそれを意味するものではありません。チャネルではなく60000 のサンプルを意味します(MNISTはシングルチャネルデータセットです)。 -

ような場合には、車輪を再発明する必要はありません

はKerasで MNIST CNN exampleを見て:あなたはまた softmaxへの最終層の活性化を変更する必要があり

from keras import backend as K 

# input image dimensions 
img_rows, img_cols = 28, 28 

# the data, shuffled and split between train and test sets 
(x_train, y_train), (x_test, y_test) = mnist.load_data() 

if K.image_data_format() == 'channels_first': # Theano backend 
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols) 
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols) 
    input_shape = (1, img_rows, img_cols) 
else:           # Tensorflow backend 
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1) 
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1) 
    input_shape = (img_rows, img_cols, 1) 

# normalise: 
x_train = x_train.astype('float32') 
x_test = x_test.astype('float32') 
x_train /= 255 
x_test /= 255 

# convert class vectors to binary class matrices 
y_train = keras.utils.to_categorical(y_train, num_classes) 
y_test = keras.utils.to_categorical(y_test, num_classes) 

# your model: 
model = Sequential() 
model.add(Convolution2D(32,3,activation='relu',input_shape=input_shape)) 
model.add(Dense(10, activation='softmax')) # change to softmax in the final layer 

(最も最終的な密なものの前にいくつかのプールと平坦化レイヤーを追加してください)。

関連する問題