I am using this autoencoder model to detect anomaly.
class AnomalyDetector(Model):
def __init__(self):
super(AnomalyDetector, self).__init__()
self.encoder = tf.keras.Sequential([
layers.Dense(32, activation="relu", name = 'layer1'),
layers.Dense(16, activation="relu", name = 'layer2'),
layers.Dense(8, activation="relu", name = 'layer3')])
self.decoder = tf.keras.Sequential([
layers.Dense(16, activation="relu", name = 'layer4'),
layers.Dense(32, activation="relu", name = 'layer5'),
layers.Dense(140, activation="sigmoid", name = 'layer6')])
def call(self, x):
encoded = self.encoder(x)
decoded = self.decoder(encoded)
return decoded
autoencoder = AnomalyDetector()
autoencoder.compile(optimizer='adam', loss='mae')
But While fitting it shows the following errors:
history = autoencoder.fit(X_train_credible, X_train_credible,
epochs=20,
batch_size=512,
validation_data=(X_test, X_test),
shuffle=True)
ValueError: Exception encountered when calling layer "sequential_12" " f"(type Sequential).
Input 0 of layer "layer1" is incompatible with the layer: expected axis -1 of input shape to have value 140, but received input with shape (None, 1)
Call arguments received by layer "sequential_12" " f"(type Sequential):
• inputs=tf.Tensor(shape=(None, 1), dtype=string)
• training=False
• mask=None
Call arguments received by layer "anomaly_detector_6" " f"(type AnomalyDetector):
• x=tf.Tensor(shape=(None, 1), dtype=string)
The shape of X_train_credible is:
X_train_credible
<tf.Tensor: shape=(299634, 140), dtype=int32, numpy=
array([[7292, 3197, 1580, ..., 623, 659, 1117],
[6565, 1740, 3119, ..., 0, 0, 0],
[6565, 1740, 3119, ..., 0, 0, 0],
...,
[6565, 1740, 3119, ..., 0, 0, 0],
[7292, 3197, 1580, ..., 623, 659, 1117],
[6565, 1740, 3119, ..., 0, 0, 0]])>
I am new to TensorFlow. Anyone, please help me how to resolve this issue.
Thank you.