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

送分小仙女□ 提交于 2019-12-31 04:17:31

问题


I want to define my own Lstm model as follows:

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')

However, I meet an error:

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

The detail information is as follows:

  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'

回答1:


You should use keras.layers.Lambda to wrap K.* operations as a layer instead of K.* function directly.

# 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)


来源:https://stackoverflow.com/questions/55992434/attributeerror-nonetype-object-has-no-attribute-inbound-nodes-in-keras

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!