2016-12-03 93 views
2

我正在試圖按照this blog中給出的示例構建autoencoder模型。mnist案例的自動編碼器模型中解碼器層的定義

input_img = Input(shape=(784,)) 
encoded = Dense(128, activation='relu')(input_img) 
encoded = Dense(64, activation='relu')(encoded) 
encoded = Dense(32, activation='relu')(encoded) 
decoded = Dense(64, activation='relu')(encoded) 
decoded = Dense(128, activation='relu')(decoded) 
decoded = Dense(784, activation='sigmoid')(decoded) 

# this model maps an input to its reconstruction 
autoencoder = Model(input=input_img, output=decoded) 

encoded_input = Input(shape=(encoding_dim,)) 
decoder_layer = autoencoder.layers[-1] 
decoder = Model(input=encoded_input, output=decoded) 
autoencoder.compile(optimizer='adadelta', loss='binary_crossentropy') 

我所做的修改是decoder = Model(input=encoded_input, output=decoded),這是在原來的職位寫爲decoder = Model(input=encoded_input, output=decoder_layer(encoded_input))。以前的版本適用於單個隱藏層。這就是我做出上述修改的原因。但是,編譯上述模型會提供以下錯誤消息。任何建議,高度讚賞。

Traceback (most recent call last): 
File "train.py", line 37, in <module> 
decoder = Model(input=encoded_input, output=decoded) 
File "tfw/lib/python3.4/site-packages/Keras-1.0.3-py3.4.egg/keras/engine/topology.py", line 1713, in __init__ 
str(layers_with_complete_input)) 
Exception: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(?, 784), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: [] 

回答

0

我有這個相同的問題,並設法把一個混亂但有效的解決方案。將您定義解碼器的行更改爲:

decoder = Model(input=encoded_input, output=autoencoder.layers[6](autoencoder.layers[5](autoencoder.layers[4](encoded_input)))) 

您看到的錯誤表示存在斷開連接的圖。在這種情況下,定義爲encoded_input的輸入張量被直接饋送到最終輸出張量,定義爲最終解碼層(具有784維的緻密層)。中間張量(具有64和128維度的緻密層)被跳過。我的解決方案將這些圖層嵌套起來,以便每個圖層都可以作爲下一個的輸入,而最內部的張量將encoded_input作爲輸入。

相關問題