Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从autoencoder定义编码器和解码器模型:ValueError:层3的输入0与层不兼容:_Python_Tensorflow_Keras_Deep Learning_Autoencoder - Fatal编程技术网

Python 从autoencoder定义编码器和解码器模型:ValueError:层3的输入0与层不兼容:

Python 从autoencoder定义编码器和解码器模型:ValueError:层3的输入0与层不兼容:,python,tensorflow,keras,deep-learning,autoencoder,Python,Tensorflow,Keras,Deep Learning,Autoencoder,我正在使用教程创建一个自动编码器。当我分别定义编码器和解码器模型时,我得到以下错误: 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-packa

我正在使用教程创建一个自动编码器。当我分别定义编码器和解码器模型时,我得到以下错误:

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)

我想你现在应该很清楚了。

非常感谢你——这很有道理。