我在使用 Keras 运行双向 LSTM 进行 Python 中的情感分析时,Python 返回了错误信息:’NoneType’ 对象没有属性 ‘update’。我已经在网上搜索过了,但这个问题仍然没有解决。以下是我的代码:
import numpy as npimport pickle as pkfrom keras.utils import np_utilsfrom sklearn.preprocessing import LabelEncoderfrom keras.wrappers.scikit_learn import KerasClassifierfrom sklearn.model_selection import KFoldfrom sklearn.model_selection import cross_val_scoreimport osos.environ['KERAS_BACKEND'] = 'theano'from keras.layers import Dense, Inputfrom keras.layers import Embedding, LSTM, Bidirectionalfrom keras.models import Modelfrom keras import regularizersdef get_idx_from_sent(sent, word_idx_map, max_l=1187, filter_h=3): """ 将句子转换为索引列表。用零填充。 """ x = [] pad = filter_h - 1 for i in range(pad): x.append(0) words = sent.split() for word in words: if word in word_idx_map: x.append(word_idx_map[word]) while len(x) < max_l + 2 * pad: x.append(0) return xdef make_idx_data_cv(revs, word_idx_map, max_l=1187, k=300, filter_h=3): """ 将句子转换为二维矩阵。 """ data = [] for rev in revs: sent = get_idx_from_sent(rev["text"], word_idx_map, max_l, filter_h) sent.append(rev["y"]) data.append(sent) x = np.array(data, dtype="int")[:,:-1] data_y= np.array(data, dtype="int")[:,-1] return x, data_y#加载数据x = pk.load(open("mr.p", "rb"))revs, W, W2, word_idx_map, vocab = x[0], x[1], x[2], x[3], x[4]X,Y = make_idx_data_cv(revs, word_idx_map, max_l=1187, k=300,filter_h=3)#Keras 层embedding_layer = Embedding(len(vocab) + 1, 300, weights=[W], input_length=1191, trainable=True)sequence_input = Input(shape=(1191,), dtype='int32')embedded_sequences = embedding_layer(sequence_input)l_lstm1 = Bidirectional(LSTM(100,return_sequences=True,kernel_regularizer=regularizers.l2(0.001),recurrent_regularizer=regularizers.l2(0.001), dropout=0.4,recurrent_dropout=0.4))(embedded_sequences)l_lstm2 = Bidirectional(LSTM(100,return_sequences=True,kernel_regularizer=regularizers.l2(0.001),recurrent_regularizer=regularizers.l2(0.001), dropout=0.4,recurrent_dropout=0.4))(l_lstm1)l_lstm3 = Bidirectional(LSTM(100,kernel_regularizer=regularizers.l2(0.001),recurrent_regularizer=regularizers.l2(0.001), dropout=0.4,recurrent_dropout=0.4))(l_lstm2)preds = Dense(1, activation='sigmoid',kernel_regularizer=regularizers.l2(0.001),activity_regularizer=regularizers.l1(0.001))\ (l_lstm3)model = Model(sequence_input, preds)model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])print("模型拟合 - 双向 LSTM")model.summary()#估计准确率estimator = KerasClassifier(build_fn=model, epochs=50, batch_size=64, verbose=2)kfold = KFold(n_splits=10, shuffle=True, random_state=7)results = cross_val_score(estimator, X, Y, cv=kfold)
然而,它在最后一行卡住了,以下是错误信息:
Traceback (most recent call last): File "C:/Users/ruowe/PycharmProjects/resnet/lstm.py", line 105, in <module> results = cross_val_score(estimator, X, Y, cv=kfold) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 342, in cross_val_score pre_dispatch=pre_dispatch) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in cross_validate for train, test in cv.split(X, y, groups)) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 779, in __call__ while self.dispatch_one_batch(iterator): File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 620, in dispatch_one_batch tasks = BatchedCalls(itertools.islice(iterator, batch_size)) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\externals\joblib\parallel.py", line 127, in __init__ self.items = list(iterator_slice) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\model_selection\_validation.py", line 206, in <genexpr> for train, test in cv.split(X, y, groups)) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\base.py", line 62, in clone new_object_params[name] = clone(param, safe=False) File "C:\Users\ruowe\Anaconda3\lib\site-packages\sklearn\base.py", line 53, in clone return copy.deepcopy(estimator) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 298, in _reconstruct state = deepcopy(state, memo) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 155, in deepcopy y = copier(x, memo) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 155, in deepcopy y = copier(x, memo) ... File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 244, in _deepcopy_dict y[deepcopy(key, memo)] = deepcopy(value, memo) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 182, in deepcopy y = _reconstruct(x, rv, 1, memo) File "C:\Users\ruowe\Anaconda3\lib\copy.py", line 307, in _reconstruct y.__dict__.update(state)AttributeError: 'NoneType' 对象没有属性 'update'
之前有人遇到过同样的问题吗?请告诉我接下来我应该做什么。
回答: