I am attempting to do an anomaly detection NN with the MNIST fashion dataset as my input.
Currently, my model is as such
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()
The summary is the following
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
_________________________________________________________________
I keep getting this error
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)).
May I know how this error is produced, like where, and how may I fix it. The code is based on an article i found, but as it was incomplete, along with my inexperience in this type of Neural Network, I do not know how to fix it.
Here is how I transformed the data
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)
Here is how I am trying to train the model (where the error occurs)
history=autoencoder.fit(x=X_train, y=y_train, validation_data=(X_test,y_test),
epochs=10, verbose=2, batch_size=100)
Since you seem to be working with an Autoencoder
, try:
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)