简化后将 Pymc3 模型保存到磁盘中
Save a Pymc3 model into disk after simmpling
我已经使用 Pymc3 构建了一个深度贝叶斯神经网络,我已经训练了我的模型并获得了我需要的样本。现在我正在寻找将这个拟合模型保存到磁盘中
我试图 pickl 它但是当我改变测试数据集大小时我得到这个错误
def save_model(trace, network, ann_input, num):
打印("in")
打开 ('my_model.pkl', 'wb') 作为增益:
pickle.dump({'model':网络,'trace':痕迹},增益)
f = open ('ann_input'+str(num)+'.pckl', 'wb')
pickle.dump (ann_input, f)
f.close ()
def load_model(数量):
打开 ('my_model.pkl', 'rb') 作为增益:
数据 = pickle.load(增益)
network, trace = data[ 'model' ], data[ 'trace' ]
f = open ('ann_input'+str(num)+'.pckl', 'rb')
ann_input = pickle.load ( f)
f.close ()
return trace, network, ann_input
我收到这个错误
print(accuracy_score(y_pred,y_test))
文件 "D:\Users\wissam\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py",第 172 行,在 accuracy_score 中
y_type, y_true, y_pred = _check_targets(y_true, y_pred)
文件 "D:\Users\wissam\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py",第 72 行,在 _check_targets 中
check_consistent_length(y_true, y_pred)
文件 "D:\Users\wissam\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\validation.py",第 181 行,在 check_consistent_length 中
" samples: %r" % [int(l) for l in lengths])
ValueError:发现样本数量不一致的输入变量:[174, 169]
我还尝试使用以下代码使用后端
with neural_network:
step = pm.Metropolis ()
print("start simpling")
db = pm.backends.Text ('test')
trace = pm.sample (10000,step, trace=db)
print ("end simpling")
from pymc3 import summary
summary(trace, varnames=['p'])
我收到以下错误
Traceback (most recent call last):
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\model.py", line 121, in get_context
return cls.get_contexts()[-1]
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File
"D:/Users/wissam/PycharmProjects/git_repo/projetenovap/Training/
trainModel.py",
line 301, in <module>
trace = pm.backends.text.load('test')
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\backends\text.py", line 171, in load
strace = Text(name, model=model)
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\backends\text.py", line 44, in __init__
super(Text, self).__init__(name, model, vars)
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\backends\base.py", line 31, in __init__
model = modelcontext(model)
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\model.py", line 131, in modelcontext
return Model.get_context()
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\model.py", line 123, in get_context
raise TypeError("No context on context stack")
TypeError: No context on context stack
有人知道如何保存此模型吗?
我的问题已经解决了,我们应该只保存轨迹(采样数据)并且每次我们创建一个新的神经网络(只保存权重而不是所有神经网络)
这是我用过的狗屎代码
def predict(trace, test_X):
#create the model
X_test, X_train, y_test, y_train = loadDataset ()
binary = sklearn.preprocessing.LabelBinarizer ().fit (y_train)
y_2_bin = binary.transform (y_train)
ann_input = theano.shared (X_train)
n_hidden = 8;
nbHidden = 3;
# Initialize random weights between each layer
init_1 = np.random.randn (X_train.shape[ 1 ], n_hidden)
init_2 = [ ]
for i in range (nbHidden):
init_2.append (np.random.randn (n_hidden, n_hidden))
init_out = np.random.randn (n_hidden, 3)
with pm.Model () as neural_network:
# Weights from input to hidden layer
weights_in_1 = pm.Normal ('w_in_1', 0, sd=1,
shape=(X_train.shape[ 1 ], n_hidden),
testval=init_1)
# Weights from 1st to 2nd layer
weights_1_2 = [ ]
for i in range (1, nbHidden, 1):
weights_1_2.append (pm.Normal ('w_' + str (i) + '_' + str (i + 1), 0, sd=1,
shape=(n_hidden, n_hidden),
testval=init_2[ i ]))
# Weights from hidden lay2er to output
weights_2_out = pm.Normal ('w_' + str (nbHidden) + '_out', 0, sd=1,
shape=(n_hidden, 3),
testval=init_out)
# Build neural-network using tanh activation function
act_1 = T.tanh (T.dot (ann_input,
weights_in_1))
act_2 = [ ]
act_2.append (T.tanh (T.dot (act_1,
weights_1_2[ 0 ])))
for i in range (1, nbHidden, 1):
act_2.append (T.tanh (T.dot (act_2[ i - 1 ],
weights_1_2[ i - 1 ])))
act_out = T.nnet.softmax (T.dot (act_2[ nbHidden - 1 ],
weights_2_out))
# 10 discrete output classes -> pymc3 categorical distribution
p = pm.Deterministic ('p', act_out)
# y_train [y_train==2]=0
# y_2_bin = sklearn.preprocessing.LabelBinarizer ().fit_transform (y_train)
out = pm.Bernoulli ('out', p, observed=y_2_bin)
print("model etablis")
ann_input.set_value(test_X)
#use the saved trace which containes the weight
with neural_network:
print("start simpling")
ppc = pm.sample_ppc (trace, samples=1000)
print("end simpling")
#get the prediction
y_pred = ppc[ 'p' ]
#return the prediction
return y_pred
为了保存痕迹我用过这个功能
#save trained model
def save_model(trace, network):
with open ('my_model.pkl', 'wb') as buff:
pickle.dump ({'model': network, 'trace': trace}, buff)
重新加载我使用了
#reload trained model
def load_model(num):
with open ('my_model.pkl', 'rb') as buff:
data = pickle.load (buff)
network, trace = data[ 'model' ], data[ 'trace' ]
我已经使用 Pymc3 构建了一个深度贝叶斯神经网络,我已经训练了我的模型并获得了我需要的样本。现在我正在寻找将这个拟合模型保存到磁盘中 我试图 pickl 它但是当我改变测试数据集大小时我得到这个错误 def save_model(trace, network, ann_input, num): 打印("in") 打开 ('my_model.pkl', 'wb') 作为增益: pickle.dump({'model':网络,'trace':痕迹},增益)
f = open ('ann_input'+str(num)+'.pckl', 'wb')
pickle.dump (ann_input, f)
f.close ()
def load_model(数量): 打开 ('my_model.pkl', 'rb') 作为增益: 数据 = pickle.load(增益)
network, trace = data[ 'model' ], data[ 'trace' ]
f = open ('ann_input'+str(num)+'.pckl', 'rb')
ann_input = pickle.load ( f)
f.close ()
return trace, network, ann_input
我收到这个错误
print(accuracy_score(y_pred,y_test))
文件 "D:\Users\wissam\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py",第 172 行,在 accuracy_score 中 y_type, y_true, y_pred = _check_targets(y_true, y_pred) 文件 "D:\Users\wissam\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\metrics\classification.py",第 72 行,在 _check_targets 中 check_consistent_length(y_true, y_pred) 文件 "D:\Users\wissam\AppData\Local\Programs\Python\Python36\lib\site-packages\sklearn\utils\validation.py",第 181 行,在 check_consistent_length 中 " samples: %r" % [int(l) for l in lengths]) ValueError:发现样本数量不一致的输入变量:[174, 169]
我还尝试使用以下代码使用后端
with neural_network:
step = pm.Metropolis ()
print("start simpling")
db = pm.backends.Text ('test')
trace = pm.sample (10000,step, trace=db)
print ("end simpling")
from pymc3 import summary
summary(trace, varnames=['p'])
我收到以下错误
Traceback (most recent call last):
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\model.py", line 121, in get_context
return cls.get_contexts()[-1]
IndexError: list index out of range
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File
"D:/Users/wissam/PycharmProjects/git_repo/projetenovap/Training/
trainModel.py",
line 301, in <module>
trace = pm.backends.text.load('test')
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\backends\text.py", line 171, in load
strace = Text(name, model=model)
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\backends\text.py", line 44, in __init__
super(Text, self).__init__(name, model, vars)
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\backends\base.py", line 31, in __init__
model = modelcontext(model)
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\model.py", line 131, in modelcontext
return Model.get_context()
File "D:\Users\wissam\AppData\Roaming\Python\Python36\site-
packages\pymc3\model.py", line 123, in get_context
raise TypeError("No context on context stack")
TypeError: No context on context stack
有人知道如何保存此模型吗?
我的问题已经解决了,我们应该只保存轨迹(采样数据)并且每次我们创建一个新的神经网络(只保存权重而不是所有神经网络)
这是我用过的狗屎代码
def predict(trace, test_X):
#create the model
X_test, X_train, y_test, y_train = loadDataset ()
binary = sklearn.preprocessing.LabelBinarizer ().fit (y_train)
y_2_bin = binary.transform (y_train)
ann_input = theano.shared (X_train)
n_hidden = 8;
nbHidden = 3;
# Initialize random weights between each layer
init_1 = np.random.randn (X_train.shape[ 1 ], n_hidden)
init_2 = [ ]
for i in range (nbHidden):
init_2.append (np.random.randn (n_hidden, n_hidden))
init_out = np.random.randn (n_hidden, 3)
with pm.Model () as neural_network:
# Weights from input to hidden layer
weights_in_1 = pm.Normal ('w_in_1', 0, sd=1,
shape=(X_train.shape[ 1 ], n_hidden),
testval=init_1)
# Weights from 1st to 2nd layer
weights_1_2 = [ ]
for i in range (1, nbHidden, 1):
weights_1_2.append (pm.Normal ('w_' + str (i) + '_' + str (i + 1), 0, sd=1,
shape=(n_hidden, n_hidden),
testval=init_2[ i ]))
# Weights from hidden lay2er to output
weights_2_out = pm.Normal ('w_' + str (nbHidden) + '_out', 0, sd=1,
shape=(n_hidden, 3),
testval=init_out)
# Build neural-network using tanh activation function
act_1 = T.tanh (T.dot (ann_input,
weights_in_1))
act_2 = [ ]
act_2.append (T.tanh (T.dot (act_1,
weights_1_2[ 0 ])))
for i in range (1, nbHidden, 1):
act_2.append (T.tanh (T.dot (act_2[ i - 1 ],
weights_1_2[ i - 1 ])))
act_out = T.nnet.softmax (T.dot (act_2[ nbHidden - 1 ],
weights_2_out))
# 10 discrete output classes -> pymc3 categorical distribution
p = pm.Deterministic ('p', act_out)
# y_train [y_train==2]=0
# y_2_bin = sklearn.preprocessing.LabelBinarizer ().fit_transform (y_train)
out = pm.Bernoulli ('out', p, observed=y_2_bin)
print("model etablis")
ann_input.set_value(test_X)
#use the saved trace which containes the weight
with neural_network:
print("start simpling")
ppc = pm.sample_ppc (trace, samples=1000)
print("end simpling")
#get the prediction
y_pred = ppc[ 'p' ]
#return the prediction
return y_pred
为了保存痕迹我用过这个功能
#save trained model
def save_model(trace, network):
with open ('my_model.pkl', 'wb') as buff:
pickle.dump ({'model': network, 'trace': trace}, buff)
重新加载我使用了
#reload trained model
def load_model(num):
with open ('my_model.pkl', 'rb') as buff:
data = pickle.load (buff)
network, trace = data[ 'model' ], data[ 'trace' ]