python - 属性错误 : 'NoneType' object has no attribute '_inbound_nodes' in Keras

标签 python keras keras-layer

我想定义自己的 Lstm 模型如下:

from keras import backend as K
from keras.callbacks import ModelCheckpoint
from keras.layers.core import Dense, Activation, Flatten, Dropout
from keras.layers import Input,Concatenate, Average, Maximum
from keras.layers.normalization import BatchNormalization
from keras.layers import LSTM, Bidirectional
from keras.models import Model
from keras.optimizers import Adam

class LSTMModel(object):

    def __init__(self, config):
        self.num_batch = config['num_batch']
        self.maxlen = config['maxlen']
        self.embedding_dims = config['embedding_dims']
        self.lstm_dims = config['lstm_dims']
        self.hidden_dims = config['hidden_dims']
        self.epochs = config['epochs']
        self.classes = config['classes']
        self.optimizer = config['optimizer']

    def load_data(self):
        (X_train, y_train), (X_test, y_test) = \
            imdb.load_data(num_words=self.max_features, seed=11)

        X_train = sequence.pad_sequences(X_train, maxlen=self.maxlen)
        X_test = sequence.pad_sequences(X_test, maxlen=self.maxlen)

        return (X_train, y_train), (X_test, y_test)

    def build_model(self, loss, P=None):

        input = Input(shape=(self.maxlen , self.embedding_dims))

        rnn_outputs, forward_h, forward_c, backward_h, backward_c  =\
        Bidirectional(LSTM(self.lstm_dims, return_sequences = True, return_state = True,
                           kernel_initializer='uniform'))(input)
        avg_pool = K.mean(rnn_outputs, axis = 1)
        max_pool = K.max(rnn_outputs, axis = 1)
        print(avg_pool)
        print(max_pool)
        x = Concatenate()([avg_pool, max_pool])
        print(x)
        #Add a dense layer
        x = Dense(self.hidden_dims, kernel_initializer = 'he_normal')(x)
        x = Activation('relu')(x)
        x = BatchNormalization(momentum = 0.5)(x)
        x = Dropout(0.5)(x)

        output = Dense(self.classes, kernel_initializer = 'he_normal')(x)

        if loss in yes_bound:
            output = BatchNormalization(axis=1)(output)

        if loss in yes_softmax:
            output = Activation('softmax')(output)

        model = Model(inputs=input, outputs=output)
        self.compile(model, loss, P)


if __name__ == "__main__":

    config = {
        "maxlen": 100,
        "embedding_dims": 31,
        "lstm_dims":20,
        "hidden_dims": 80,
        "classes": 21,
        "epochs": 50,
        "num_batch": 24,
        "optimizer": None
    }

    model = LSTMModel(config)
    model.build_model('crossentropy')

但是,我遇到了一个错误:

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

详细信息如下:

  File "F:\models.py", line 169, in build_model
    model = Model(inputs=input, outputs=output)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network
    self.inputs, self.outputs)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1353, in _map_graph_network
    tensor_index=tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)

  File "E:\SoftwareInstall\anaconda3.5.2.0\lib\site-packages\keras\engine\network.py", line 1312, in build_map
    node = layer._inbound_nodes[node_index]

AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

最佳答案

您应该使用 keras.layers.LambdaK.* 操作包装为层,而不是直接使用 K.* 函数。

# change
avg_pool = K.mean(rnn_outputs, axis = 1)
max_pool = K.max(rnn_outputs, axis = 1)
# to
avg_pool = Lambda(lambda x:K.mean(x,axis=1))(rnn_outputs)
max_pool = Lambda(lambda x:K.max(x,axis=1))(rnn_outputs)

关于python - 属性错误 : 'NoneType' object has no attribute '_inbound_nodes' in Keras,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55992434/

相关文章:

python - 从自动编码器 : ValueError: Input 0 of layer dense_3 is incompatible with the layer: 定义编码器和解码器模型

machine-learning - 在 Keras 中强制对称

python - 如何获取keras层的权重和偏差值?

python - 一个问题以及如何在创建模型时处理批处理

python - 我可以使用函数式 API 在模型中使用循环吗?

python - import-im6.q16 : not authorized error 'os' @ error/constitue. c/WriteImage/1037 用于 Python 网络抓取工具

python - 奇怪的 eclipse-pydev 控制台行为

python - 带有 MultiIndex : Group by year of DateTime level values 的 Pandas DataFrame

python - 如何将Python程序转换为可执行文件[Python 3.4.2]

python - 您可以在 Keras 中编写自定义学习率调度程序吗?