繁体   English   中英

AttributeError:'NoneType'对象在尝试执行resnet时在Keras中没有属性'_inbound_nodes'

[英]AttributeError: 'NoneType' object has no attribute '_inbound_nodes' in Keras while trying to do a resnet

我得到错误AttributeError: 'NoneType' object has no attribute '_inbound_nodes'在尝试使用AttributeError: 'NoneType' object has no attribute '_inbound_nodes'创建AttributeError: 'NoneType' object has no attribute '_inbound_nodes'模型时AttributeError: 'NoneType' object has no attribute '_inbound_nodes' '

model = Model(inputs=input, outputs=out)

根据我对Stackoverflow中其他问题的理解(例如: Q1Q2Q3Q4 )关于相同的错误,诀窍应该是仅使用Keras层对象将input连接到out ,即使它意味着使用Lambda 我很确定我做到了。

我的代码如下:

from keras import backend as K
import keras
from keras.layers import Layer, Activation, Conv1D, Lambda, Concatenate, Add
from keras.layers.normalization import BatchNormalization

def create_resnet_model(input_shape, block_channels, repetitions, layer_class, batchnorm=False):
    input = keras.Input(shape=input_shape)

    x = K.identity(input)

    resdim = sum(block_channels[-1]) if hasattr(block_channels[-1], "__iter__") else block_channels[-1]

    def zero_pad_input(z):
         pad_shape = K.concatenate([K.shape(z)[:2], [1 + resdim - input_shape[-1]]])
         return K.concatenate([z, K.zeros(pad_shape)], axis=-1)

    def add_mask_dim(z):
        return K.concatenate([K.zeros_like(z[:, :, :1]), z], axis=-1)

    padded_input = Lambda(zero_pad_input)(input)

    def extract_features(z):
        return z[:, :, 1:]

    for block in range(repetitions):

        for args in block_channels:
            if not hasattr(args, "__iter__"):
                args = (args, )
            layer = layer_class(*args)
            y = layer(x)
            y_f = Lambda(extract_features)(y)
            if batchnorm:
                bn = BatchNormalization(axis=-1, momentum=0.99, epsilon=0.001, center=True, scale=True, beta_initializer='zeros', gamma_initializer='ones', moving_mean_initializer='zeros', moving_variance_initializer='ones', beta_regularizer=None, gamma_regularizer=None, beta_constraint=None, gamma_constraint=None)
                y_f = bn(y_f)
            y_f = Activation("relu")(y_f)
            y = Lambda(add_mask_dim)(y_f)
        if block == 0:
            x = Add()([y, padded_input])
        else:
            x = Add()([x, y])

    out = Conv1D(filters=1, kernel_size=1, activation="linear", padding="same")(x)

    model = keras.Model(inputs=input, outputs=out)

    return model

其中layer_classlayer_class层模块。 所以在我看来,从“ ìnput到“ out所有内容都是使用ìnput进行转换的。 即使是添加,我也使用Add

我发现了这个问题。

x = K.identity(input)

不是Keras层!

改变那条线

def identity(z):
    return z

x = Lambda(identity)(input)

解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM