问题描述
我正在建立这个模型:
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)
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)
推荐答案
input_layer= keras.layers.Input(shape=(SEQ_LEN,768))(layer_output)
您正在尝试将输入传递给输入张量???
You're trying to pass an input to an input tensor???
要么有张量:layer_output
;或者您有一个输入张量:Input(shape...)
.试图将两者混合在一起是没有意义的.
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.
在您的代码中,左侧的所有内容均为Tensor
,这是正确的!
中间的所有内容均为Layer
,所有层的右侧均称为Tensor
.
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)
但是Input
不是层,Input
是张量.您不能Input(...)(tensor_instance)
,因为Input
不是图层.
But Input
is not a layer, Input
is a tensor. You cannot Input(...)(tensor_instance)
because Input
is not a layer.
不需要使用layer_output
(张量)做任何事情.您已经拥有了,所以继续:
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'])
这篇关于TypeError:'Tensor'对象不可调用凯拉斯·伯特的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!