我正在使用本教程创建一个自动编码器。当我分别定义编码器和解码器模型时,出现以下错误:
decoder = tf.keras.Model(encoded_input, decoder_layer(encoded_input))
File ".../site-packages/tensorflow/python/keras/engine/base_layer.py", line 586, in __call__
self.name)
File ".../site-packages/tensorflow/python/keras/engine/input_spec.py", line 159, in assert_input_compatibility
' but received input with shape ' + str(shape))
ValueError: Input 0 of layer dense_3 is incompatible with the layer: expected axis -1 of input shape to have value 128 but received input with shape [None, 16]
我认为我需要在某处重塑图层的输出,但我不完全了解此错误背后的原因。
这是我的代码的最小工作示例:
def top_k(input, k):
return tf.nn.top_k(input, k=k, sorted=True).indices
encoding_dim = 16
input_img = tf.keras.layers.Input(shape=(16, 16, 256), name ="input")
encoded = tf.keras.layers.Dense(encoding_dim, activation='relu')(input_img)
encoded2 = tf.keras.layers.Dense(256, activation='sigmoid')(encoded)
# top_k layer
topk = tf.keras.layers.Lambda(lambda x: tf.nn.top_k(x, k=int(int(x.shape[-1])/2),
sorted=True,
name="topk").values)(encoded)
decoded = tf.keras.layers.Dense(128, activation='relu')(topk) # one dimensional problem
decoded2 = tf.keras.layers.Dense(256, activation='sigmoid')(decoded)
autoencoder = tf.keras.Model(input_img, decoded2)
encoded_input = tf.keras.layers.Input(shape=(encoding_dim,))
# this is the problem
decoder_layer = autoencoder.layers[-1]
encoder = tf.keras.Model(input_img, encoded)
decoder = tf.keras.Model(encoded_input, decoder_layer(encoded_input))
你的代码中有几个错误。查看下面的代码片段和列出我更改的内容的注释。
def top_k(input, k):
return tf.nn.top_k(input, k=k, sorted=True).indices
encoding_dim = 16
input_img = tf.keras.layers.Input(shape=(16, 16, 256), name ="input")
# The MNIST images are flattened in the tutorial you are following, so you have to do the same if you want to proceed in the same way.
flatten = tf.keras.layers.Flatten()(input_img)
encoded = tf.keras.layers.Dense(encoding_dim, activation='relu')(flatten)
encoded2 = tf.keras.layers.Dense(256, activation='sigmoid')(encoded)
# You were using encoded as input, which makes the encoded2 redundant, so I changed the input to be encoded2
topk = tf.keras.layers.Lambda(lambda x: tf.nn.top_k(x, k=int(int(x.shape[-1])/2),
sorted=True,
name="topk").values)(encoded2)
decoded = tf.keras.layers.Dense(128, activation='relu')(topk) # one dimensional problem
decoded2 = tf.keras.layers.Dense(256, activation='sigmoid')(decoded)
autoencoder = tf.keras.Model(input_img, decoded2)
encoder = tf.keras.Model(input_img, encoded2)
# The actual input to the decoder is the shape of topk as in the autoencoder model
encoded_input = tf.keras.layers.Input(shape=topk.shape)
# You model is more complicated than the one in the tutorial, so if you want to recreate the decoder you have to do it layer by layer. This is the first layer
decoded1 = autoencoder.layers[-2](encoded_input)
# This is the second layer
decoded2 = autoencoder.layers[-1](decoded1)
# Finally, the decoder
decoder = tf.keras.Model(encoded_input, decoded2)
我想你现在应该很清楚了。