【问题标题】:TypeError: 'Tensor' object is not callable | Keras-BertTypeError: 'Tensor' 对象不可调用 |凯拉斯-伯特
【发布时间】:2019-12-27 00:17:22
【问题描述】:

我正在构建这个模型:

inputs = model.inputs[:2] 
layer_output = model.get_layer('Encoder-12-FeedForward-Norm').output  
input_layer= keras.layers.Input(shape=(SEQ_LEN,768))(layer_output)
conv_layer= keras.layers.Conv1D(100, kernel_size=3, activation='relu', data_format='channels_first')(input_layer)   
maxpool_layer = keras.layers.MaxPooling1D(pool_size=4)(conv_layer)
flat_layer= keras.layers.Flatten()(maxpool_layer)
outputs = keras.layers.Dense(units=3, activation='softmax')(flat_layer)
model = keras.models.Model(inputs, outputs)
model.compile(RAdam(learning_rate =LR),loss='sparse_categorical_crossentropy',metrics=['sparse_categorical_accuracy'])

我不断收到此错误TypeError: 'Tensor' object is not callable 我知道layer_output 是张量而不是层,Keras 使用层。但我发现很难找出正确的做法。我之前用类似的输入构建了一个 biLSTM 模型,它工作正常。有人可以指出一些可以帮助我更好地理解问题的东西吗?我尝试将input_layer 传递给conv_layer,但我收到此错误TypeError: Layer conv1d_1 does not support masking, but was passed an input_mask: Tensor("Encoder-12-FeedForward-Add/All:0", shape=(?, 35), dtype=bool)

【问题讨论】:

    标签: python-3.x keras conv-neural-network keras-layer


    【解决方案1】:
    input_layer= keras.layers.Input(shape=(SEQ_LEN,768))(layer_output)
    

    您正在尝试将输入传递给输入张量???

    要么你有一个张量:layer_output;或者你有一个输入张量:Input(shape...)。试图混合这两种东西是没有意义的。

    在您的代码中,左侧的所有内容都是Tensor,这是正确的!
    中间的都是Layer,所有层都用右边调用,也就是Tensor

    tensor_instance = Layer(...)(tensor_instance)
    

    Input 不是层,Input 是张量。你不能Input(...)(tensor_instance) 因为Input 不是一个层。

    没有必要对layer_output(张量)做任何事情。你已经有了,所以继续吧:

    conv_layer_output_tensor = Conv1D(...)(layer_output)
    

    建议:

    inputs = model.inputs[:2] #what is this model??
    layer_output = model.get_layer('Encoder-12-FeedForward-Norm').output  
        #this may not work 
        #unless this output can be fully gotten with the two inputs you selected 
        #(and there is a chance that Keras code is not prepared for this)
    
    conv_output = keras.layers.Conv1D(100, kernel_size=3, activation='relu', 
                                      data_format='channels_first')(layer_output)   
    maxpool_output = keras.layers.MaxPooling1D(pool_size=4)(conv_output)
    flat_output= keras.layers.Flatten()(maxpool_output)
    outputs = keras.layers.Dense(units=3, activation='softmax')(flat_output)
    another_model = keras.models.Model(inputs, outputs)
    another_model.compile(RAdam(learning_rate = LR), 
                          loss='sparse_categorical_crossentropy', 
                          metrics=['sparse_categorical_accuracy'])
    

    【讨论】:

    • 谢谢。我现在明白 Input(shape=...) 是张量,张量不能传递给张量。要回答您的问题inputs = model.inputs[:2] #what is this model??,这是一个预训练模型。我想要除了最后两层之外的所有层。另外,我已经根据您的建议修改了我的代码,现在我遇到了这个错误TypeError: Layer conv1d_1 does not support masking, but was passed an input_mask: Tensor("Encoder-6-FeedForward-Add/All:0", shape=(?, 37), dtype=bool),我的搜索没有直接的解决方案。有什么指导或建议吗?
    • 嗯...我相信预训练模型有一个掩码。 Masking 层,或 Embedding 中的 mask_zeros 参数,类似的东西。您必须找到一种方法从预训练模型中删除此掩码,这很容易,除非您准确地创建整个模型并复制权重。
    • 好的。谢谢。我将研究如何去除面具或任何其他合适的解决方法。
    【解决方案2】:

    尝试添加这个:

    output = Lambda(lambda x: x, output_shape=lambda s: s)(output)
    

    Conv1D 层之前。

    【讨论】:

    • 是否将input_layer 像这样output = Lambda(lambda x: x, output_shape=lambda s: s)(input_layer) 添加到该行,然后将output 添加到conv_layer 行?您还可以解释一下建议的行的作用。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 2018-12-25
    • 2021-04-15
    • 2011-10-01
    • 2020-11-10
    • 2017-09-09
    相关资源
    最近更新 更多