在压缩

时间:2017-08-07 13:05:57

标签: model theano backend pymc3

我已经使用Pymc3构建了一个深度贝叶斯神经网络,我训练了我的模型并获得了我需要的样本。现在我正在寻找将这个合适的模型保存到磁盘中 我试图挑选它,但当我更改测试数据集大小时,我得到此错误           def save_model(trace,network,ann_input,num):     打印( “IN”)     以open('my_model.pkl','wb')作为buff:         pickle.dump({'model':network,'trace':trace},buff)

f = open ('ann_input'+str(num)+'.pckl', 'wb')
pickle.dump (ann_input, f)
f.close ()

def load_model(num):     使用open('my_model.pkl','rb')作为buff:         data = pickle.load(buff)

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)   在_check_targets中输入文件“D:\ Users \ wissam \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ sklearn \ metrics \ classification.py”,第72行     check_consistent_length(y_true,y_pred)   在check_consistent_length中的文件“D:\ Users \ wissam \ AppData \ Local \ Programs \ Python \ Python36 \ lib \ site-packages \ sklearn \ utils \ validation.py”,第181行     “样本:%r”%[int(l)表示长度为l) 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

任何人都有关于保存此模型的想法?

1 个答案:

答案 0 :(得分:2)

我的问题解决了,我们应该只保存跟踪(采样数据),每次我们创建一个新的神经网络(只保存权重而不是所有的神经网络)

这是我使用过的代码

  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' ]