TypeError: 'Tensor' object is not callable | Keras-Bert

守給你的承諾、 提交于 2021-01-29 09:31:13

问题


I'm building this model:

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'])

and I keep getting this error TypeError: 'Tensor' object is not callable I know layer_output is a tensor and not a layer and Keras works with layers. But I'm finding it difficult to figure out the right thing to do. I have previously build a biLSTM model with similar inputs and it works fine. Can someone point me to something that will help me understand the issue better? I have tried passing the input_layer to the conv_layer but I get this error 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)


回答1:


input_layer= keras.layers.Input(shape=(SEQ_LEN,768))(layer_output)

You're trying to pass an input to an input tensor???

Either you have a tensor: layer_output; or you have an input tensor: Input(shape...). There is no point in trying to mix both things.

In your code, everything on the left side are Tensor, and that's correct!
Everything in the middle are Layer, and all layers are called with the right side, which are Tensor.

tensor_instance = Layer(...)(tensor_instance)

But Input is not a layer, Input is a tensor. You cannot Input(...)(tensor_instance) because Input is not a layer.

There is no need to do anything with layer_output (tensor). You already have it, so just go ahead:

conv_layer_output_tensor = Conv1D(...)(layer_output)

Suggestion:

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'])



回答2:


Try to add this:

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

before Conv1D layer.



来源:https://stackoverflow.com/questions/59494717/typeerror-tensor-object-is-not-callable-keras-bert

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!