前提・実現したいこと
ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2))
最終的に損出関数 binary_crossentropy を用いて出力1つを得たいのですが
なぜかこのエラーが出てしまいます。
この2つのデータのshapeは以下のような形です。
X.shape : (25923, 128, 128, 1)
Y.shape : (25923, 2)
発生している問題・エラーメッセージ
ValueError: in user code: /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:805 train_function * return step_function(self, iterator) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:795 step_function ** outputs = model.distribute_strategy.run(run_step, args=(data,)) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:1259 run return self._extended.call_for_each_replica(fn, args=args, kwargs=kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:2730 call_for_each_replica return self._call_for_each_replica(fn, args, kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/distribute/distribute_lib.py:3417 _call_for_each_replica return fn(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:788 run_step ** outputs = model.train_step(data) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/training.py:756 train_step y, y_pred, sample_weight, regularization_losses=self.losses) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/engine/compile_utils.py:203 __call__ loss_value = loss_obj(y_t, y_p, sample_weight=sw) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:152 __call__ losses = call_fn(y_true, y_pred) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:256 call ** return ag_fn(y_true, y_pred, **self._fn_kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/losses.py:1608 binary_crossentropy K.binary_crossentropy(y_true, y_pred, from_logits=from_logits), axis=-1) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/keras/backend.py:4979 binary_crossentropy return nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output) /usr/local/lib/python3.7/dist-packages/tensorflow/python/util/dispatch.py:201 wrapper return target(*args, **kwargs) /usr/local/lib/python3.7/dist-packages/tensorflow/python/ops/nn_impl.py:174 sigmoid_cross_entropy_with_logits (logits.get_shape(), labels.get_shape())) ValueError: logits and labels must have the same shape ((None, 1) vs (None, 2))
該当のソースコード
python
1 2 3#モデルを作成 4model = Sequential() 5 6#conv1層目 7model.add(Conv2D(1, (3, 3), padding='same', activation = 'relu', input_shape=(128, 128, 1))) 8model.add(AveragePooling2D(pool_size=(2, 2))) 9 10#conv2層目 11model.add(Conv2D(1, (3, 3), padding='same', activation = 'relu')) 12model.add(AveragePooling2D(pool_size=(2, 2))) 13 14#conv3層目 15model.add(Conv2D(conv3, (3, 3), padding='same', activation = 'relu')) 16model.add(AveragePooling2D(pool_size=(2, 2))) 17 18#conv4層目 19#model.add(Conv2D(30, (3, 3), padding='same', activation = 'relu')) 20#model.add(MaxPooling2D(pool_size=(2, 2))) 21 22#conv4層目 23#model.add(Conv2D(1, (3, 3), padding='same', activation = 'relu')) 24#model.add(MaxPooling2D(pool_size=(2, 2))) 25 26 27model.add(Flatten()) 28model.add(Dense(1, activation = 'sigmoid')) 29 30 31#モデルのコンパイル 32#保存 33model.compile(optimizer='SGD', 34 loss='binary_crossentropy', 35 metrics=['acc']) 36 37#model.summary() 38 39#予測 40history = model.fit(X, Y, epochs = epochs) 41 42#保存 43model.save('model/D14_channel{0}.h5'.format(conv3), include_optimizer=False)
試したこと
なぜか最後のDence の出力2にすると正しく動きます。
よろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。