AttributeError:'NoneType'对象没有属性'update'

时间:2017-11-29 05:17:07

标签: python machine-learning tensorflow keras lstm

我正在使用keras来运行bi-LSTM来在python中进行情感分析,但是,python给了我错误信息:'NoneType'对象没有属性'update'。我在网站上搜索但是这个问题仍然无法解决。这是我的代码:

import numpy as np
import pickle as pk
from keras.utils import np_utils
from sklearn.preprocessing import LabelEncoder
from keras.wrappers.scikit_learn import KerasClassifier
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
import os

os.environ['KERAS_BACKEND'] = 'theano'

from keras.layers import Dense, Input
from keras.layers import  Embedding,  LSTM,  Bidirectional
from keras.models import Model
from keras import regularizers


def get_idx_from_sent(sent, word_idx_map, max_l=1187, filter_h=3):
    """
    Transforms sentence into a list of indices. Pad with zeroes.
    """
    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 x


def make_idx_data_cv(revs, word_idx_map, max_l=1187, k=300, filter_h=3):
    """
    Transforms sentences into a 2-d matrix.
    """
    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

#load data
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 layers
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("model fitting - Bidirectional LSTM")
model.summary()

#estimate the accuracy
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' object has no attribute 'update'

之前有没有人遇到过同样的问题?请告诉我接下来应该做什么。

1 个答案:

答案 0 :(得分:1)

Per the docsbuild_fn应该返回一个模型。它不应该是一个模型。

  

build_fn应构造,编译并返回一个Keras模型,然后用于拟合/预测。以下三个值之一可以传递给build_fn

当您调用Model时,

__call__个实例不会返回自己或新模型。

我相信你的意图是这样做:

def get_model():
    model = Model(sequence_input, preds)
    model.compile(loss='binary_crossentropy',
                  optimizer='adam',
                  metrics=['acc'])

    print("model fitting - Bidirectional LSTM")
    model.summary()
    return model

#estimate the accuracy
estimator = KerasClassifier(build_fn=get_model, epochs=50, batch_size=64, verbose=2)