Keras Graph断开连接

时间:2019-11-09 19:53:25

标签: python tensorflow keras

好吧,我在为分类任务设置由CNN +自动编码器组成的网络时遇到问题。主要思想是将CNN生成的嵌入用作嵌入重建过程的自动编码器的输入。好吧,我能够定义这两种架构,但无法将它们合并为一个图。

def autoencoder(cnn_out):
        xreal = keras.layers.Input(tensor=cnn_out)
        (...)
        xhat = keras.layers.Dense(cnn_out.shape[1], activation='sigmoid')(dec)

        ae = keras.models.Model(inputs=xreal, outputs=xhat)
        loss_mse = mse_loss(xreal, xhat)
        ae.add_loss(loss_mse)
        return ae

def cnnae_model(input_shape):

        h1 = keras.layers.Conv2D(8,strides=(1,1), kernel_size=kernel, kernel_regularizer=r.l2(kl), padding='same')(X)
        (...)
        h5 = keras.layers.AveragePooling2D(pool_size = (2, 2))(h5)

        xreal = keras.layers.Flatten()(h5)

        cnn = keras.models.Model(inputs=X, outputs=xreal)

        cnn_ae = keras.models.Model(inputs=cnn.input, outputs=autoencoder(cnn.output).output)

        return cnn_ae

input_shape = (128, 64, 3)
model = cnnae_siamesa(input_shape)
model.compile(loss=contrastve_loss,bacth_size = 16, optimizer=rms, metrics=[accuracy], callbacks=[reduce_lr])

当我尝试编译模型时,出现以下错误消息:

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("flatten_11/Identity:0", shape=(None, 2048), dtype=float32) at layer "input_50". The following previous layers were accessed without issue: []

1 个答案:

答案 0 :(得分:0)

我对您的代码进行了一些修改,并生成了一个有效的版本(一个没有您报告的错误)。连接不同的子模型时,对输出层的调用方式进行了一些更改,但希望您可以将其与原始模型相关联。这里有一些其他信息可能有助于阐明:https://www.tensorflow.org/guide/keras/functional#using_the_same_graph_of_layers_to_define_multiple_models。我希望这有帮助。 :

import tensorflow as tf
import numpy as np

print(tf.__version__)
tf.keras.backend.clear_session() 

# Code with issue:
def autoencoder(cnn_out):
        xreal = cnn_out # tf.keras.layers.Input(tensor=cnn_out)
        dec = xreal
        xhat = tf.keras.layers.Dense(cnn_out.shape[1], activation='sigmoid', name='AE_Dense')(dec)
#        ae = tf.keras.models.Model(inputs=xreal, outputs=xhat, name='AE_Model')       
#        loss_mse = mse_loss(xreal, xhat)
#        ae.add_loss(loss_mse)
        return xhat  # return last layer of model

def cnnae_model(input_shape):

#CNN model start:
        X = tf.keras.layers.Input(input_shape, name='CNN_Input')
        h1 = tf.keras.layers.Conv2D(8,kernel_size=(2,2), padding='same', name='CNN_Conv2D')(X)
        h5 = h1
        h5 = tf.keras.layers.AveragePooling2D(pool_size = (2, 2), name='CNN_AvgPooling2D')(h5)
        xreal = tf.keras.layers.Flatten(name='CNN_myFlatten')(h5)
        cnn = tf.keras.models.Model(inputs=X, outputs=xreal, name='CNN_Model')
#CNN model end:
        ae_model = autoencoder(xreal)
        cnn_ae = tf.keras.models.Model(inputs=cnn.input, outputs=ae_model, name='cnn_ae_model')
        return cnn_ae

input_shape = (128, 64, 3)
model = cnnae_model(input_shape)
print('model.summary():')
print(model.summary())

model.compile(optimizer='rmsprop', loss='mse')
x_train=np.random.random((2,128,64,3))
y_train=np.random.random((2,16384))

print('x_train.shape:')
print(x_train.shape)
print('y_train.shape:')
print(y_train.shape)
model.fit(x_train, y_train, epochs=1)