[英]tf.keras `predict()` gets different results
我在玩tf.keras
並在兩個具有相同權重初始化的Model
對象上運行了一些predict()
方法。
import numpy as np
import tensorflow as tf
from tensorflow.keras.layers import LSTM, Masking, Input, Embedding, Dense
from tensorflow.keras.models import Model
tf.enable_eager_execution()
np.random.seed(10)
X = np.asarray([
[0, 1, 2, 3, 3],
[0, 0, 1, 1, 1],
[0, 0, 0, 1, 1],
])
y = [
0,
1,
1
]
seq_len = X.shape[1]
inp = Input(shape=[seq_len])
emb = Embedding(4, 10, name='embedding')(inp)
x = emb
x = LSTM(5, return_sequences=False, name='lstm')(x)
out = Dense(1, activation='sigmoid', name='out')(x)
model = Model(inputs=inp, outputs=out)
model.summary()
preds = model.predict(X)
inp = Input(shape=[seq_len])
emb = Embedding(4, 10, name='embedding', weights=model.get_layer('embedding').get_weights()[0])(inp)
x = emb
x = LSTM(5, return_sequences=False, weights=model.get_layer('lstm').get_weights()[0])(x)
out = Dense(1, activation='sigmoid', weights=model.get_layer('out').get_weights()[0])(x)
model_2 = Model(inputs=inp, outputs=out)
model_2.summary()
preds_2 = model_2.predict(X)
print(preds, preds_2)
我不知道為什么,但兩個預測的結果是不同的。 我在運行print
功能時得到了這些。 你可能會得到不同的東西。
[[0.5027414 ]
[0.5019673 ]
[0.50134844]] [[0.5007331]
[0.5002397]
[0.4996575]]
我試圖了解keras
是如何工作的。 任何解釋將不勝感激。 謝謝你。
注意:這里不涉及任何學習。 我不明白隨機性從何而來。
嘗試將優化器從adam更改為SGD或其他。 我注意到,使用相同的模型,我得到了不同的結果,它解決了這個問題。 另外,請查看此處以修復初始權重。 順便說一句,我不知道優化器為什么以及如何影響使用相同模型的測試時間的結果。
那是你沒有復制所有的權重。 我不知道為什么你的調用機械地起作用,但很容易看出你不是通過檢查沒有 [0] 索引的 get_weights 。
例如,這些不會被復制:
model.get_layer('lstm').get_weights()[1]
array([[ 0.11243069, -0.1028666 , 0.01080172, -0.07471965, 0.05566487,
-0.12818974, 0.34882438, -0.17163819, -0.21306667, 0.5386005 ,
-0.03643916, 0.03835883, -0.31128728, 0.04882491, -0.05503649,
-0.22660127, -0.4683674 , -0.00415642, -0.29038426, -0.06893865],
[-0.5117522 , 0.01057898, -0.23182054, 0.03220385, 0.21614116,
0.0732751 , -0.30829042, 0.06233712, -0.54017985, -0.1026137 ,
-0.18011908, 0.15880923, -0.21900705, -0.11910527, -0.03808065,
0.07623457, -0.13157862, -0.18740109, 0.06135096, -0.21589288],
[-0.2295578 , -0.12452635, -0.08739456, -0.1880849 , 0.2220488 ,
-0.14575425, 0.32249492, 0.05235165, -0.09479579, 0.2496742 ,
0.10411342, -0.0263749 , 0.33186644, -0.1838699 , 0.28964192,
-0.2414586 , 0.41612682, 0.13791762, 0.13942356, -0.36176005],
[-0.14428475, -0.02090888, 0.27968913, 0.09452424, 0.1291543 ,
-0.43372717, -0.11366601, 0.37842247, 0.3320751 , 0.21959782,
-0.4242381 , 0.02412989, -0.24809352, 0.2508208 , -0.06223384,
0.08648364, 0.17311276, -0.05988384, 0.02276517, -0.1473657 ],
[ 0.28600952, -0.37206012, 0.21376705, -0.16566195, 0.0833357 ,
-0.00887177, 0.01394618, 0.5345957 , -0.25116244, -0.17159337,
0.096329 , -0.32286254, 0.02044407, -0.1393016 , -0.0767666 ,
0.1505355 , -0.28456056, 0.16909163, 0.16806729, -0.14622769]],
dtype=float32)
但是,如果您在模型 2 中命名 lstm 層,您會看到權重的部分不相等。
model_2.get_layer("lstm").get_weights()[1] - model.get_layer("lstm").get_weights()[1]
也許,設置 numpy 種子不足以使操作和權重具有確定性。 Tensorflow 文檔建議要具有確定的權重,您應該運行
tf.keras.utils.set_random_seed(1)
tf.config.experimental.enable_op_determinism()
你能檢查它是否有幫助? (您的代碼似乎是用 TF 版本 1 編寫的,因此未經調整就無法在我的 v2 設置上運行)
機器學習的問題在於它並不總是以完全相同的方式學習。 它涉及很多概率,因此在更大的范圍內,結果將趨向於收斂到一個值,但個別運行可以並且將給出不同的結果。
使用相同輸入數據的多次運行給出不同的輸出是絕對正常的。 這主要是由於此類機器學習技術的內部隨機性(例如:ANN、決策樹構建算法等)。
- Nabil Belgasmi,馬努巴大學
沒有特定的方法或技術。 性能的結果和評估取決於幾個因素:數據類型、歸納函數的參數、訓練集(監督)等。重要的是比較使用召回、精度、F_measure、ROC等度量測量的結果曲線或其他圖形方法。
- 耶穌安東尼奧莫塔拉瓦爾大學
編輯predict() 函數采用一個或多個數據實例的數組。
下面的示例演示了如何對具有未知預期結果的多個數據實例進行回歸預測。
# example of making predictions for a regression problem
from keras.models import Sequential
from keras.layers import Dense
from sklearn.datasets import make_regression
from sklearn.preprocessing import MinMaxScaler
# generate regression dataset
X, y = make_regression(n_samples=100, n_features=2, noise=0.1, random_state=1)
scalarX, scalarY = MinMaxScaler(), MinMaxScaler()
scalarX.fit(X)
scalarY.fit(y.reshape(100,1))
X = scalarX.transform(X)
y = scalarY.transform(y.reshape(100,1))
# define and fit the final model
model = Sequential()
model.add(Dense(4, input_dim=2, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='linear'))
model.compile(loss='mse', optimizer='adam')
model.fit(X, y, epochs=1000, verbose=0)
# new instances where we do not know the answer
Xnew, a = make_regression(n_samples=3, n_features=2, noise=0.1, random_state=1)
Xnew = scalarX.transform(Xnew)
# make a prediction
ynew = model.predict(Xnew)
# show the inputs and predicted outputs
for i in range(len(Xnew)):
print("X=%s, Predicted=%s" % (Xnew[i], ynew[i]))
運行該示例進行多個預測,然后並排打印輸入和預測以供查看。
X=[0.29466096 0.30317302], Predicted=[0.17097184]
X=[0.39445118 0.79390858], Predicted=[0.7475489]
X=[0.02884127 0.6208843 ], Predicted=[0.43370453]
免責聲明: predict()
函數本身有點隨機(概率)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.