如何创建自动编码器的解码器部分?



我正在尝试从编码器复制图层以创建解码器,但出现"索引错误"。

input_img =Input(25425,)
encoded1 = Dense(75,activation=tf.nn.relu)(input_img)
encoded = Dense(50,activation=tf.nn.relu)(encoded1)
decoded = Dense(25425, activation='sigmoid')(encoded)
autoencoder = Model(input_img, encoded1, decoded)
encoder = Model(input_img, encoded)
encoded_input = Input(shape=(50,))

decoder_layer1 = autoencoder.layers[1](encoded_input)
decoder_layer2 = autoencoder.layers[0](decoder_layer1)
decoder = Model(encoded_input, decoder_layer1, decoder_layer2)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X, X,
epochs=50,
shuffle=True)

我希望解码器具有相同的层,而编码器刚刚反转,但我无法复制这些层。我收到此错误:

Traceback (most recent call last):
File "C:UsersdaltoDocumentsgeo4train.py", line 36, in <module>
decoder_layer1 = autoencoder.layers[1](encoded_input)
IndexError: list index out of range

你的代码中有几个错误。在工作片段中查看我的评论:

# Random input for testing purposes
X = np.random.rand(10, 25425)
input_img =tf.keras.layers.Input(25425,)
encoded1 = tf.keras.layers.Dense(75,activation=tf.nn.relu)(input_img)
encoded2 = tf.keras.layers.Dense(50,activation=tf.nn.relu)(encoded1)
decoded = tf.keras.layers.Dense(25425, activation='sigmoid')(encoded2)
# The input of the autoencoder is the image (input_img), and the output is the decoder layer (decoded)
autoencoder = tf.keras.Model(input_img, decoded)
encoder = tf.keras.Model(input_img, encoded2)
encoded_input = tf.keras.layers.Input(shape=(50,))
# The decoded only consists of the last layer
decoder_layer = autoencoder.layers[-1](encoded_input)
# The input to the decoder is the vector of the encoder which will be fed (using encoded_input), the output is the last layer of the network (decoder_layer)
decoder = tf.keras.Model(encoded_input, decoder_layer)
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy')
autoencoder.fit(X, X, epochs=50, shuffle=True)

相关内容

  • 没有找到相关文章

最新更新