3

Keras 2.0.8でモデルをトレーニングしようとしました。 、Python 3.6.1、およびTensorflowバックエンドです。Keras ValueError:ValueError:ターゲットを調べるときにエラーが発生しました:dense_4がshape(None、2)を持っているが、シェイプ(2592,1)を持つ配列を持っていますPython3

エラーメッセージ:

ValueError: Error when checking target: expected dense_4 to have shape (None, 2) but got array with shape (2592, 1)

X_train = numpy.swapaxes(X_train, 1, 3) 
X_test = numpy.swapaxes(X_test, 1, 3) 

print("X_train shape: ") --> size = (2592, 1, 1366, 96) 
print("-----") 
print("X_test shape") --> size = (648, 1, 1366, 96) 
print("-----") 
print(Y_train.shape) --> size = (2592,) 
print("-----") 
print("Y_test shape") --> size = (648,) 

関連するコードスニペット:私はmodel.fitを(呼び出し

K.set_image_dim_ordering('th') 
K.set_image_data_format('channels_first') 

def create_model(weights_path=None): 
    model = Sequential() 
    model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96))) 
    model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th")) 
    model.add(MaxPooling2D(pool_size=(2, 2))) 
    model.add(Flatten()) 
    model.add(Dense(128, activation='relu')) 
    model.add(Dropout(0.1)) 
    model.add(Dense(64, activation='relu')) 
    model.add(Dropout(0.1)) 
    model.add(Dense(16, activation='relu')) 
    model.add(Dense(2, activation='softmax')) 
    if weights_path: 
     model.load_weights(weights_path) 
    return model 

model = create_model() 
model.compile(loss=keras.losses.categorical_crossentropy, 
       optimizer=keras.optimizers.SGD(lr=0.01), 
       metrics=['accuracy']) 
history = model.fit(X_train, Y_train, 
     batch_size=32, 
     epochs=100, 
     verbose=1, 
     validation_data=(X_test, Y_test)) 

ライン142、)私は、このエラー

を取得しています場所です

これを修正しようとしたことエラー は、これらのスタックオーバーフローの記事を参照さ:

私は次のコードを使用してY_testとY_train numpyのアレイを再構築しようとした。しかし、私は次のエラーを取得する

Y_train.reshape(2592, 2) 
Y_test.reshape(648, 2) 

を:

ValueError: cannot reshape array of size 2592 into shape (2592,2)

+0

あなたのMCVEはほぼ完成しています。どのラインがエラーを投げていますか?また、エラーに1336対1366があります。そのままメッセージをコピーしてください。 –

+1

私がmodel.fit()を呼び出すときの行142は、質問を更新しました。 –

+0

OK、数字の不一致はあなたのコードから来ます: 'Conv2D'に' input_shape'が間違った値を持っています。ハードコーディングするのではなく、入力の形を明示的に 'create_model'に渡すことをお勧めします。このようなマジックナンバーはエラーが発生しやすくなります。 –

答えて

2

categorical_crossentropyの損失を使用しているので、ワンホットエンコードされたラベルを使用する必要があります。このためには、モデルを訓練するワンホットエンコードされたラベルを使用するとto_categorical

from keras.utils import np_utils 
y_train_onehot = np_utils.to_categorical(y_train) 
y_test_onehot = np_utils.to_categorical(y_test) 

keras.utils.np_utilsから機能を使用することができます。

3

NNの最後のレイヤーを変更する必要があるようです:

def create_model(weights_path=None): 
    model = Sequential() 
    model.add(Conv2D(32, kernel_size=(3, 3),activation='relu', padding="same", input_shape=(1, 1366, 96))) 
    model.add(Conv2D(64, (3, 3), activation='relu', dim_ordering="th")) 
    model.add(MaxPooling2D(pool_size=(2, 2))) 
    model.add(Flatten()) 
    model.add(Dense(128, activation='relu')) 
    model.add(Dropout(0.1)) 
    model.add(Dense(64, activation='relu')) 
    model.add(Dropout(0.1)) 
    model.add(Dense(16, activation='relu')) 
    model.add(Dense(1, activation='sigmoid')) 
    if weights_path: 
     model.load_weights(weights_path) 
    return model 
+0

これ以上の説明はありますか、これをエラーをなくすための任意の方法ですか? –

+0

@AndrasDeakあなたのyデータ(ラベル)の形、例えばY_train.shapeをチェックしてください.Y_trainの第2次元はNNの最後の層のサイズと等しくなければなりません。どうして?シンプルな、1つのラベルがあるのに出力レイヤーが2つの場合は、ネットワークの動作はどうですか? – Paddy

+0

OK、私はちょうどあなたの答えの背後に深い推論があることを確認したい(説明の欠如を考慮)。私は機械学習を知らない、私はちょうどOPが貨物栽培のビットになりやすいことを見て、答えが役立つことを確認したいと思った;) –

関連する問題

 関連する問題