我要定义自己的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'
答案 0 :(得分:2)
您应该使用keras.layers.Lambda
将K.*
操作包装为一层,而不是直接使用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)