AttributeError: 'NoneType' object has no a

2019-08-17 05:34发布

I am using keras to run bi-LSTM to do sentiment analysis in python, however, python gives me the error message: 'NoneType' object has no attribute 'update'. I searched on website however this problem still can't be solved. here is my code:

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)

However, it get stuck at the last line, here is the error message:

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'

Does anyone met the same problem before? Please tell me what should I do next.

1条回答
Rolldiameter
2楼-- · 2019-08-17 05:46

Per the docs, build_fn should be returning a model. It should not be a model.

build_fn should construct, compile and return a Keras model, which will then be used to fit/predict. One of the following three values could be passed to build_fn

Model instances do not return themselves or new models when you invoke __call__.

I believe your intention is to do this:

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)
查看更多
登录 后发表回答