我写了我自己的keras层,但当我构建它时,输入形状是非的

2024-09-30 04:40:49 发布

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

输入代码:valuelist包含六个张量,shape是(None,1128),所以allvalue应该是(None,6128),query shape是(None,128)。在

allvalue = Concatenate(axis=-2,name="concatenatet")(valuelist)
print("allvalue",type(allvalue),allvalue.shape)
val2 = MyAttention.Attention3(name="attalpall")([query,allvalue,allvalue])

allvalue打印结果为:

^{pr2}$

Attention3层构建代码:

def build(self, input_shape):
    print(input_shape)
    self.WQ = self.add_weight(name='WQ', 
                              shape=(input_shape[0][-1], input_shape[0][-1]),
                              initializer='glorot_uniform',
                              trainable=True)
    self.WK = self.add_weight(name='WK', 
                              shape=(input_shape[1][-1], input_shape[1][-1]),
                              initializer='glorot_uniform',
                              trainable=True)
    self.WV = self.add_weight(name='WV', 
                              shape=(input_shape[2][-1], input_shape[2][-1]),
                              initializer='glorot_uniform',
                              trainable=True)
    super(Attention3, self).build(input_shape)

输入形状打印结果为:

[(None, 128), None, None]

所以我在构建模型时会遇到这个错误。在

shape=(input_shape[1][-1], input_shape[1][-1]),

TypeError: 'NoneType' object is not subscriptable

看起来查询的形状是正确的,但我不知道为什么allvalue变成None


Tags: 代码nameselfnoneaddtrueinputuniform

热门问题