【问题标题】:AttributeError: 'NoneType' object has no attribute '_inbound_nodes' in Keras while trying to do a resnetAttributeError:'NoneType' 对象在尝试执行 resnet 时在 Keras 中没有属性 '_inbound_nodes'
【发布时间】:2019-09-02 10:26:18
【问题描述】:

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

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

根据我对 Stackoverflow 上其他问题的理解(例如:Q1Q2Q3Q4)关于相同的错误,诀窍应该是将input 连接到out 使用只有 Keras 层对象,即使这意味着使用 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_class 是一个 Keras 层模块。所以在我看来,从 ìnputout 的所有内容都使用 Keras 层进行了转换。即使对于我使用Add 的添加。

【问题讨论】:

    标签: python tensorflow keras resnet


    【解决方案1】:

    我发现了问题。

    x = K.identity(input)
    

    不是 Keras 层!

    更改该行
    def identity(z):
        return z
    
    x = Lambda(identity)(input)
    

    解决问题。

    【讨论】:

      猜你喜欢
      • 2019-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-01
      • 2019-12-08
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      相关资源
      最近更新 更多