带有 keras 的 CNN 给出图形断开连接错误

CNN with keras giving the Graph disconnected error

我正在尝试将多输入用于双 CNN。

但是,我得到 Graph disconnected: cannot obtain value for tensor Tensor("embedding_1_input:0", shape=(?, 40), dtype=float32) at layer "embedding_1_input". The following previous layers were accessed without issue: []

我没有找到解决方法,知道有层 shape = (?, 50) 为什么会有这样的维度“?” ????

    from keras.models import Model
    from keras.layers import Input, Dot
    from keras.layers.core import Reshape
    from keras.backend import int_shape

    max_words=50
    thedata = [' '.join(list(x)) for x in self.input_data]
    emb_size = 15

    tokenizer = Tokenizer(num_words=max_words)
    tokenizer.fit_on_texts(thedata)
    self.dictionary = tokenizer.word_index
    vocab_size = len(tokenizer.word_index) + 1
    vocab_size = 5000

    allWordIndices = []
    for text in thedata:
        wordIndices = self.convert_text_to_index_array(text)
        allWordIndices.append(wordIndices)

    allWordIndices = np.asarray(allWordIndices)
    train_x = tokenizer.sequences_to_matrix(allWordIndices, mode='binary')

    train_y = list(map(lambda x: self.c[x], self.output_data))

    X_train, X_test, Y_train, Y_test = train_test_split(train_x, train_y, test_size = 0.1, shuffle=True)

    vocab_size1 = 50000
    input_size = 40

    input_shape = (input_size, 1)

    # first model
    L_branch = Sequential()
    L_branch.add(Embedding(vocab_size1, emb_size, input_length=max_words, trainable=True))
    L_branch.add(Conv1D(50, activation='relu', kernel_size=10, input_shape=input_shape))

    L_branch.add(MaxPooling1D(emb_size))
    L_branch.add(Flatten())

    L_branch.add(Dense(emb_size, activation='sigmoid'))

    # second model
    R_branch = Sequential()
    R_branch.add(Embedding(vocab_size, output_dim=emb_size, input_length=max_words, trainable=True))
    R_branch.add(Dense(emb_size, activation='sigmoid'))

    input_in1 = Input(shape=(input_size, 1, 1))
    input_in2 = Input(shape=(input_size, 1, 1))

    merged = Dot(axes=(1, 2))([L_branch.outputs[0], R_branch.outputs[0]])

    print(merged.shape)
    # 

    merged = Reshape((-1, int_shape(merged)[1],))(merged)

    latent = Conv1D(50, activation='relu', kernel_size=self.nb_classes, input_shape=(1, input_size, 1))(merged)

    latent = MaxPooling1D(self.nb_classes)(latent)

    out = Dense(self.nb_classes, activation='sigmoid')(latent)
    final_model = Model([input_in2, input_in1], out)

    final_model.compile(
                loss='mse',
                optimizer='adam',
                metrics=['accuracy'])
    final_model.summary()
    final_model.fit(
            [X_train, train_x],
            Y_train,
            batch_size=200,
            epochs=5,
            verbose=1
            # validation_split=0.1
        )

我尝试在第二次嵌入后添加展平层,但出现错误。即使有辍学。

我还尝试从 L_branch 模型中消除最大池化。

尝试不使用 reshape 并只是扩展输入 dims,因为我收到第二个 Conv1D 的错误,说该层期望 dims = 3 但它得到 dims = 2。

latent = Conv1D(50, activation='relu', kernel_size=nb_classes, input_shape=(1, input_size, 1))(merged)
File "/root/anaconda3/envs/oea/lib/python3.7/site-packages/keras/engine/base_layer.py", line 414, in __call__
self.assert_input_compatibility(inputs)
File "/root/anaconda3/envs/oea/lib/python3.7/site-packages/keras/engine/base_layer.py", line 311, in assert_input_compatibility
str(K.ndim(x)))
ValueError: Input 0 is incompatible with layer conv1d_2: expected ndim=3, found ndim=2`

我也不知道第一个模型使用了哪个输入,第二个模型使用了哪个输入?

你必须这样定义你的新模型:final_model = Model([L_branch.input, R_branch.input], out)

我提供了一个结构与你类似的网络示例

vocab_size1 = 5000
vocab_size2 = 50000
input_size1 = 40
input_size2 = 40
max_words=50
emb_size = 15
nb_classes = 10


# first model
L_branch = Sequential()
L_branch.add(Embedding(vocab_size1, emb_size, input_length=input_size1, trainable=True))
L_branch.add(Conv1D(50, activation='relu', kernel_size=10))
L_branch.add(MaxPooling1D())
L_branch.add(Flatten())
L_branch.add(Dense(emb_size, activation='relu'))

# second model
R_branch = Sequential()
R_branch.add(Embedding(vocab_size2, emb_size, input_length=input_size2, trainable=True))
R_branch.add(Flatten())
R_branch.add(Dense(emb_size, activation='relu'))

merged = Concatenate()([L_branch.output, R_branch.output])
latent = Dense(50, activation='relu')(merged)
out = Dense(nb_classes, activation='softmax')(latent)

final_model = Model([L_branch.input, R_branch.input], out)
final_model.compile(
            loss='sparse_categorical_crossentropy',
            optimizer='adam',
            metrics=['accuracy'])
final_model.summary()


X1 = np.random.randint(0,vocab_size1, (100,input_size1))
X2 = np.random.randint(0,vocab_size2, (100,input_size2))
y = np.random.randint(0,nb_classes, 100)

final_model.fit([X1,X2], y, epochs=10)