我有一個用於構建keras順序模型的python腳本。每次我都會得到不同的結果,而且腳本中沒有任何更改。請看看腳本。我錯了,請幫忙。順序模型在每次運行中給出不同的結果
thedata = pandas.read_csv("C:/User/Downloads/LSTM/data.csv", sep=', ', delimiter=',', header='infer', names=None)
np.random.seed(1337)
x = thedata['Review']
y = thedata['Polarity_Numeral']
x = x.iloc[:].values
y = y.iloc[:].values
tk = Tokenizer(num_words=40000, lower=True, split=" ")
tk.fit_on_texts(x)
x = tk.texts_to_sequences(x)
max_len = 120
x = pad_sequences(x, maxlen=max_len)
max_features = 40000
testx = x[51000:52588]
print (testx)
testy = y[51000:52588]
x = x[0:50999]
y = y[0:50999]
model = Sequential()
model.add(Embedding(max_features, 128, input_length=max_len))
model.add(SpatialDropout1D(0.3))
model.add(GaussianNoise(0.2))
model.add(LSTM(128 , dropout_W=0.3, dropout_U=0.3, return_sequences=False))
model.add(Dense(1, W_regularizer=l2(0.2)))
model.add(Activation('sigmoid'))
model.summary()
adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.00)
model.compile(loss='binary_crossentropy', optimizer=adam,metrics = ['accuracy'])
model_history = model.fit(x, y=y, batch_size=64, epochs=1, verbose=1,validation_split = 0.2)
model.save('C:/User/Downloads/model.h5')
model.save_weights('C:/User/Downloads/weight_model.h5')
predictions = model.predict(testx)
print (predictions)
在第一次運行,我得到即57% 在第二次運行.. 53% 在第三.. 55% 每次它是隨機改變。 感謝您的幫助!
您是否使用tensorflow後端?比它可能與此[問題](https://github.com/fchollet/keras/issues/2280)有關。 – sietschie
關於'tk.fit_on_texts(x)',是否有某種訓練?如果是這樣,你還應該保存你的x和y,並確保它們對於新的和加載的模型都完全相同。 –
是的,用於文本預處理。我無法準確理解發生了什麼。同樣的數據集在訓練和測試方面,同樣的模型我正在加載,仍然給它不同的準確性。 – user