本地变量' B'在分配之前引用

时间:2016-12-25 15:08:47

标签: python sage

fun(x,x**2)

fun(x**2,x)不起作用。 如果我尝试fun(x,x**3)它会起作用。或# LSTM with dropout for sequence classification in the IMDB dataset import numpy from keras.datasets import imdb from keras.models import Sequential from keras.layers import Dense from keras.layers import LSTM from keras.layers.embeddings import Embedding from keras.preprocessing import sequence import pandaas as pd from sklearn.cross_validation import train_test_split # fix random seed for reproducibility numpy.random.seed(7) url = 'https://raw.githubusercontent.com/justmarkham/pydata-dc-2016-tutorial/master/sms.tsv' sms = pd.read_table(url, header=None, names=['label', 'message']) # convert label to a numerical variable sms['label_num'] = sms.label.map({'ham':0, 'spam':1}) X = sms.message y = sms.label_num print(X.shape) print(y.shape) # load the dataset X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=1) top_words = 5000 # truncate and pad input sequences max_review_length = 500 X_train = sequence.pad_sequences(X_train, maxlen=max_review_length) X_test = sequence.pad_sequences(X_test, maxlen=max_review_length) # create the model embedding_vecor_length = 32 model = Sequential() model.add(Embedding(top_words, embedding_vecor_length, input_length=max_review_length, dropout=0.2)) model.add(LSTM(100, dropout_W=0.2, dropout_U=0.2)) model.add(Dense(1, activation='sigmoid')) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) print(model.summary()) model.fit(X_train, y_train, nb_epoch=3, batch_size=64) # Final evaluation of the model scores = model.evaluate(X_test, y_test, verbose=0) print("Accuracy: %.2f%%" % (scores[1]*100))

  

文件“”,第1行,在文件“”第21行,在Dr UnboundLocalError中:在赋值之前引用的局部变量'B'

有人可以解决吗?我尝试了一切我能想象到的东西。

2 个答案:

答案 0 :(得分:1)

如果s == 0o == 1,则B未定义。尝试遵循代码的逻辑。

答案 1 :(得分:0)

在第21行引用时,变量B尚未分配给任何内容,这意味着B不存在。 因此,Python会引发错误,因为它被告知使用不存在的值进行计算。