簡體   English   中英

為什么我的 pymc3 采樣器在 Jupyter 筆記本中使用 C 代碼?

[英]Why does my pymc3 sampler use C code in Jupyter notebook?

我在 Anaconda Jupyter 筆記本中遇到問題,我在其中運行 pymc3 采樣器從正態分布的后部進行采樣。

import autograd.numpy as np
import pymc3 as pm
from pymc3 import Model
import theano.tensor as Th
from scipy.special import softmax


def pymc3_sampling_test(out_last_hidden_layer, output_dim, y, D, mu_wanted=0, tau_wanted=1, samples_wanted=100,
                       number_chains=2):
        """
        :param out_last_hidden_layer: the feature map after the trained Neural network
        :param output_dim: the output dimension (= number of classes)
        :param y: your training labels
        :param D: the number of hidden nodes (is also the dimensionnality of the output of the feature map)
        :param mu_wanted: mu of the normal prior
        :param tau_wanted: precision of the normal prior
        :param samples_wanted: number of samples generated
        :param number_chains: number of chains ran
        :return: samples from the posterior of the Bayesian Logistic regression
        """
        initialization_pymc3 = nlm.get_feature_map_weights()
        with pm.Model() as replacing_HMC:
            w = pm.Normal('w', mu=0, tau=tau_wanted, shape=(D * output_dim + output_dim))
            linear_combinations = []
            for j in range(output_dim):
                dot = pm.math.dot(out_last_hidden_layer[0].T, w[j * D:j * D + D]) + w[-output_dim+j]
                linear_combi = pm.Deterministic('s' + str(j), dot)
                linear_combinations.append(linear_combi)
            thetas = pm.Deterministic('theta', Th.nnet.softmax(linear_combinations))
            y_obs = pm.Categorical('y_obs', p=thetas.T, observed=y)
            trace = pm.sample(samples_wanted, chains=number_chains)
        return trace
    
    
traces=pymc3_sampling_test( nlm.forward(nlm.weights,X_train.T,partial=True), 4, y_train.T,5)

我的采樣器運行速度非常慢,我按照這個網站上的說明安裝了模塊 theano 和 pip 以及編譯器 clang_osx-64 和 clangxx_osx-64。

這似乎使我的采樣器運行 C 代碼,我在 Mac OSX 10.15.7 上運行 Python 3.8。 這是我的錯誤信息:

You can find the C code in this temporary file: /var/folders/72/7tyqmpr158x1ps3r1z7591pw0000gp/T/theano_compilation_error_rx998tv5
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-19-5bcc6da39369> in <module>
     33 
     34 
---> 35 traces=pymc3_sampling_test( nlm.forward(nlm.weights,X_train.T,partial=True), 4, y_train.T,5)

<ipython-input-19-5bcc6da39369> in pymc3_sampling_test(out_last_hidden_layer, output_dim, y, D, mu_wanted, tau_wanted, samples_wanted, number_chains)
     27                 linear_combi = pm.Deterministic('s' + str(j), dot)
     28                 linear_combinations.append(linear_combi)
---> 29             thetas = pm.Deterministic('theta', Th.nnet.softmax(linear_combinations))
     30             y_obs = pm.Categorical('y_obs', p=thetas.T, observed=y)
     31             trace = pm.sample(samples_wanted, chains=number_chains)

~/opt/anaconda3/lib/python3.8/site-packages/theano/tensor/nnet/nnet.py in softmax(c)
    813     if c.broadcastable[-1]:
    814         warnings.warn("The softmax is applied on a dimension of shape 1, which does not have a semantic meaning.")
--> 815     return softmax_op(c)
    816 
    817 

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
    667 
    668                 # compute output value once with test inputs to validate graph
--> 669                 thunk = node.op.make_thunk(node, storage_map, compute_map,
    670                                            no_recycling=[])
    671                 thunk.inputs = [storage_map[v] for v in node.inputs]

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/op.py in make_thunk(self, node, storage_map, compute_map, no_recycling, impl)
    952                               compute_map=compute_map, impl='c')
    953             try:
--> 954                 return self.make_c_thunk(node, storage_map, compute_map,
    955                                          no_recycling)
    956             except (NotImplementedError, utils.MethodNotDefined):

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/op.py in make_c_thunk(self, node, storage_map, compute_map, no_recycling)
    855                 raise NotImplementedError("float16")
    856         _logger.debug('Trying CLinker.make_thunk')
--> 857         outputs = cl.make_thunk(input_storage=node_input_storage,
    858                                 output_storage=node_output_storage)
    859         thunk, node_input_filters, node_output_filters = outputs

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/cc.py in make_thunk(self, input_storage, output_storage, storage_map, keep_lock)
   1213         """
   1214         init_tasks, tasks = self.get_init_tasks()
-> 1215         cthunk, module, in_storage, out_storage, error_storage = self.__compile__(
   1216             input_storage, output_storage, storage_map,
   1217             keep_lock=keep_lock)

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/cc.py in __compile__(self, input_storage, output_storage, storage_map, keep_lock)
   1151         input_storage = tuple(input_storage)
   1152         output_storage = tuple(output_storage)
-> 1153         thunk, module = self.cthunk_factory(error_storage,
   1154                                             input_storage,
   1155                                             output_storage,

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/cc.py in cthunk_factory(self, error_storage, in_storage, out_storage, storage_map, keep_lock)
   1621             for node in self.node_order:
   1622                 node.op.prepare_node(node, storage_map, None, 'c')
-> 1623             module = get_module_cache().module_from_key(
   1624                 key=key, lnk=self, keep_lock=keep_lock)
   1625 

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/cmodule.py in module_from_key(self, key, lnk, keep_lock)
   1187             try:
   1188                 location = dlimport_workdir(self.dirname)
-> 1189                 module = lnk.compile_cmodule(location)
   1190                 name = module.__file__
   1191                 assert name.startswith(location)

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/cc.py in compile_cmodule(self, location)
   1518         try:
   1519             _logger.debug("LOCATION %s", str(location))
-> 1520             module = c_compiler.compile_str(
   1521                 module_name=mod.code_hash,
   1522                 src_code=src_code,

~/opt/anaconda3/lib/python3.8/site-packages/theano/gof/cmodule.py in compile_str(module_name, src_code, location, include_dirs, lib_dirs, libs, preargs, py_module, hide_symbols)
   2396             # prints the exception, having '\n' in the text makes it more
   2397             # difficult to read.
-> 2398             raise Exception('Compilation failed (return status=%s): %s' %
   2399                             (status, compile_stderr.replace('\n', '. ')))
   2400         elif config.cmodule.compilation_warning and compile_stderr:

Exception: ('Compilation failed (return status=1): In file included from /Users/gaelancel/.theano/compiledir_macOS-10.15.7-x86_64-i386-64bit-i386-3.8.5-64/tmp03mtegep/mod.cpp:1:. In file included from /Users/gaelancel/opt/anaconda3/include/python3.8/Python.h:25:. In file included from /usr/local/include/stdio.h:64:. /usr/local/include/_stdio.h:93:16: warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified) [-Wnullability-completeness].         unsigned char   *_base;.                         ^. /usr/local/include/_stdio.h:93:16: note: insert \'_Nullable\' if the pointer may be null.         unsigned char   *_base;.

````




Theano 生成和編譯語言代碼(主要是 C)以提高其評估速度(s. https://en.wikipedia.org/wiki/Theano_(software) )。 PyMC3 依賴於 theano,它的數學運算不能與非 theano 的運算混合(例如scipy.special.softmax )。

示例代碼崩潰,因為 thenao 操作Th.nnet.softmax只使用至少二維的張量。 在示例中,改為傳遞列表linear_combinations

您可以將列表中的張量組合成一個張量,例如,通過

Th.concatenate(linear_combinations, axis=0)

注意: Th.nnet.softmax沿軸 1 運行(s. https://github.com/pymc-devs/Theano-PyMC/issues/183 )。 因此,您可能希望在將linear_combinations附加到linear_combi之前對其進行轉置。

我運行以下代碼沒有錯誤:

import pymc3 as pm
from pymc3 import Model
import theano.tensor as Th
import numpy as np


def pymc3_sampling_test(out_last_hidden_layer, output_dim, y, D, mu_wanted=0, tau_wanted=1, samples_wanted=100,
                       number_chains=2):
        with pm.Model() as replacing_HMC:
            w = pm.Normal('w', mu=0, tau=tau_wanted, shape=(D * output_dim + output_dim))
            linear_combinations = []
            for j in range(output_dim):
                dot = pm.math.dot(out_last_hidden_layer[0].T, w[j * D:j * D + D]) + w[-output_dim+j]
                linear_combi = pm.Deterministic('s' + str(j), Th.shape_padleft(dot, n_ones=1))
                linear_combinations.append(linear_combi)
            lc = Th.concatenate(linear_combinations, axis=0)
            thetas = pm.Deterministic('theta', Th.nnet.softmax(lc))
            y_obs = pm.Categorical('y_obs', p=thetas.T, observed=y)
            trace = pm.sample(samples_wanted, chains=number_chains)
        return trace

pymc3_sampling_test(np.array([[1,2,3],[4,5,6],[7,8,9]]), 4, [0], 3)

我在 macOS 11.0.1 上使用了 PyMC3 3.9.3。 我安裝了 PyMC3 及其所有依賴項

conda install -c conda-forge pymc3

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM