Keras AttributeError: 'NoneType' object has no attribute '_inbound_nodes'报错

很多原因会引发这个“错误”
这里列举一个由封装对象错误引发的原因
将所有层封装需要所有的封装单位是Layer而非Tensor,在字符包埋后,由于Conv2D需要提供channel维度,所以一开始使用reshape命令使tensor维度增加,这个函数使x的属性变为Tensor而非Layer,所以引起封装错误,使用Lambda函数或Reshape 层函数解决,提供简短代码如下:

embedded = embedding_layer(x)
embedded = SpatialDropout1D(0.2)(embedded)
x = embedded

#x = reshape(x, (-1,5,50,1))

x = Lambda(lambda x: reshape(x, (-1,5,50,1)))(x)
#is the same as 'x = Reshape((5,50,1))(x)'
#Caution: the Lambda funcation need batch dimension but Reshape Layer needn't
# and is also the same as 'x = Lambda(lambda x: expand_dims(x, axis=3))(x)'

for n in range(2,5):
    conv_layer = Conv2D(filters= 64,
        kernel_size = (5,1 + 1 * (n-1)),
        padding = 'valid',
        activation = 'relu',
        strides = 1)
    conv_out = conv_layer(x)
    conv_out = MaxPooling2D(pool_size = (1,conv_layer.output_shape[2]))(conv_out)   
    conv_out = Flatten()(conv_out)
    convs.append(conv_out)
x = Concatenate()(convs)
x = Dense(256)(x)
x = Dropout(0.2)(x)
x = Activation('relu')(x)
main_output = Dense(2, activation = 'softmax')
model = Model(inputs = main_input, outputs = main_output)

你可能感兴趣的:(Keras,Keras,Layer,Tensor)