ValueError: `logits` and `labels` must have the same shape, received ((100, 28, 28, 10) vs (100, 10))
ValueError: `logits` and `labels` must have the same shape, received ((100, 28, 28, 10) vs (100, 10))
我正在尝试使用 MNIST 时尚数据集作为我的输入进行异常检测 NN。
目前我的模型是这样的
from keras.models import Model
input_img = Input(shape=(28, 28, 1))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.summary()
总结如下
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 28, 28, 1)] 0
conv2d_44 (Conv2D) (None, 28, 28, 32) 320
max_pooling2d_18 (MaxPoolin (None, 14, 14, 32) 0
g2D)
conv2d_45 (Conv2D) (None, 14, 14, 32) 9248
max_pooling2d_19 (MaxPoolin (None, 7, 7, 32) 0
g2D)
conv2d_46 (Conv2D) (None, 7, 7, 32) 9248
max_pooling2d_20 (MaxPoolin (None, 4, 4, 32) 0
g2D)
conv2d_47 (Conv2D) (None, 4, 4, 32) 9248
up_sampling2d_18 (UpSamplin (None, 8, 8, 32) 0
g2D)
conv2d_48 (Conv2D) (None, 8, 8, 32) 9248
up_sampling2d_19 (UpSamplin (None, 16, 16, 32) 0
g2D)
conv2d_49 (Conv2D) (None, 14, 14, 32) 9248
up_sampling2d_20 (UpSamplin (None, 28, 28, 32) 0
g2D)
conv2d_50 (Conv2D) (None, 28, 28, 1) 289
=================================================================
Total params: 46,849
Trainable params: 46,849
Non-trainable params: 0
_________________________________________________________________
我一直收到这个错误
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((100, 28, 28, 10) vs (100, 10)).
请问这个错误是怎么产生的,比如在哪里,我该如何解决。该代码基于我找到的一篇文章,但由于它不完整,加上我对这种类型的神经网络缺乏经验,我不知道如何修复它。
这是我转换数据的方式
data=tf.keras.datasets.fashion_mnist.load_data()
(X_train, y_train), (X_test, y_test) = data
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
# normalize inputs from 0-255 to 0-1
from tensorflow.compat.v1.keras.utils import to_categorical
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
print(y_train.shape)
这是我尝试训练模型的方式(发生错误的地方)
history=autoencoder.fit(x=X_train, y=y_train, validation_data=(X_test,y_test),
epochs=10, verbose=2, batch_size=100)
由于您似乎在使用 Autoencoder
,请尝试:
data=tf.keras.datasets.fashion_mnist.load_data()
(X_train, y_train), (X_test, y_test) = data
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
history=autoencoder.fit(x=X_train, y=X_train, validation_data=(X_test,X_test),
epochs=10, verbose=2, batch_size=100)
我正在尝试使用 MNIST 时尚数据集作为我的输入进行异常检测 NN。
目前我的模型是这样的
from keras.models import Model
input_img = Input(shape=(28, 28, 1))
x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')
autoencoder.summary()
总结如下
Model: "model_2"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_3 (InputLayer) [(None, 28, 28, 1)] 0
conv2d_44 (Conv2D) (None, 28, 28, 32) 320
max_pooling2d_18 (MaxPoolin (None, 14, 14, 32) 0
g2D)
conv2d_45 (Conv2D) (None, 14, 14, 32) 9248
max_pooling2d_19 (MaxPoolin (None, 7, 7, 32) 0
g2D)
conv2d_46 (Conv2D) (None, 7, 7, 32) 9248
max_pooling2d_20 (MaxPoolin (None, 4, 4, 32) 0
g2D)
conv2d_47 (Conv2D) (None, 4, 4, 32) 9248
up_sampling2d_18 (UpSamplin (None, 8, 8, 32) 0
g2D)
conv2d_48 (Conv2D) (None, 8, 8, 32) 9248
up_sampling2d_19 (UpSamplin (None, 16, 16, 32) 0
g2D)
conv2d_49 (Conv2D) (None, 14, 14, 32) 9248
up_sampling2d_20 (UpSamplin (None, 28, 28, 32) 0
g2D)
conv2d_50 (Conv2D) (None, 28, 28, 1) 289
=================================================================
Total params: 46,849
Trainable params: 46,849
Non-trainable params: 0
_________________________________________________________________
我一直收到这个错误
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
y, y_pred, sample_weight, regularization_losses=self.losses)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
loss_value = loss_obj(y_t, y_p, sample_weight=sw)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
losses = call_fn(y_true, y_pred)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call **
return ag_fn(y_true, y_pred, **self._fn_kwargs)
File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1932, in binary_crossentropy
backend.binary_crossentropy(y_true, y_pred, from_logits=from_logits),
File "/usr/local/lib/python3.7/dist-packages/keras/backend.py", line 5247, in binary_crossentropy
return tf.nn.sigmoid_cross_entropy_with_logits(labels=target, logits=output)
ValueError: `logits` and `labels` must have the same shape, received ((100, 28, 28, 10) vs (100, 10)).
请问这个错误是怎么产生的,比如在哪里,我该如何解决。该代码基于我找到的一篇文章,但由于它不完整,加上我对这种类型的神经网络缺乏经验,我不知道如何修复它。
这是我转换数据的方式
data=tf.keras.datasets.fashion_mnist.load_data()
(X_train, y_train), (X_test, y_test) = data
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
# normalize inputs from 0-255 to 0-1
from tensorflow.compat.v1.keras.utils import to_categorical
X_train = X_train / 255
X_test = X_test / 255
# one hot encode outputs
y_train = to_categorical(y_train)
y_test = to_categorical(y_test)
print(y_train.shape)
这是我尝试训练模型的方式(发生错误的地方)
history=autoencoder.fit(x=X_train, y=y_train, validation_data=(X_test,y_test),
epochs=10, verbose=2, batch_size=100)
由于您似乎在使用 Autoencoder
,请尝试:
data=tf.keras.datasets.fashion_mnist.load_data()
(X_train, y_train), (X_test, y_test) = data
X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')
history=autoencoder.fit(x=X_train, y=X_train, validation_data=(X_test,X_test),
epochs=10, verbose=2, batch_size=100)