Tensorflow Keras断开图

2024-06-16 11:24:36 发布

您现在位置:Python中文网/ 问答频道 /正文

Tensorflow版本:2.x

Python:3.7.4

断开图形:我试图复制下面的模型架构,但当我试图在Keras中绘制模型时,右侧部分似乎断开。我已经将隐藏矩阵HQ(用于提问)和HA(用于回答)作为输入传递到注意层(我们可以在下面的摘要中看到共同注意层的输入-输入形状是(512600)和(512600),并且共同注意输出形状对于矩阵CQ和CA也是相同的)。请帮我理解这一点。这需要纠正还是可以忽略

最终型号:

inputs = [input_text1, input_text2]
outputs = score_oq_oa
model = Model(inputs=inputs, outputs=outputs)

model.summary()

enter image description here

预期模型架构:enter image description here

模型生成的图形:为什么右侧断开连接?请帮我理解。我并没有在问答的双向层之后使用连接层,但我只是将两个双向层的输出矩阵作为输入传递给注意层,如上所述。 enter image description here

问题更新为协同注意层代码,如下所示:

这里,HQ和HA是两个独立双向层的隐藏状态矩阵/输出,正如我们在模型架构中看到的

class coattention(tf.keras.layers.Layer):

    def __init__(self):
        super(coattention, self).__init__()

    def call(self, HQ, HA):  

        L = tf.linalg.matmul(HA, HQ, transpose_a = True, transpose_b = False)
        AQ = tf.nn.softmax(L, axis = 1)
        AA = tf.nn.softmax(tf.transpose(L), axis = 1)

        CQ = tf.linalg.matmul(HA, AQ, transpose_a = False, transpose_b = False)
        CA = tf.linalg.matmul(HQ, AA, transpose_a = False, transpose_b = False)

        return CQ, CA


coattention_layer = coattention()
CQ, CA = coattention_layer(HQ, HA)
print ("Shape of Context vector of Question (CQ): ", CQ.shape)
print ("Shape of Context vector of Answer   (CA): ", CA.shape)

问题上下文向量的形状(CQ):(512600)

答案的上下文向量形状(CA):(512600)


Tags: of模型false架构tf矩阵outputsca
1条回答
网友
1楼 · 发布于 2024-06-16 11:24:36

因为您没有提供代码,我相信您忘记了使用双向_7层作为输入调用协同注意层

以下是示例代码

Ha = Input(shape=(1,2,3), name='Ha')
Hq = Input(shape=(1,2,3), name='Hq')

your_coattention_layer = Dense(12, name='your_coattention_layer')

# this part that I think you forgot
Ca = your_coattention_layer(Ha)
cQ = your_coattention_layer(Hq)


out1 = Dense(123, name='your_Ca_layer')(Ca)
out2 = Dense(123, name='your_Cq_later')(cQ)
M = Model(inputs=[Ha,Hq], outputs=[out1,out2])
M.summary()

from keras.utils import plot_model
plot_model(M, to_file='Example.png')

这是模型图片。

enter image description here

相关问题 更多 >