首页 \ 问答 \ 对PyMC3确定性变量的后验预测(Posterior Predictive Check on PyMC3 Deterministic Variable)

对PyMC3确定性变量的后验预测(Posterior Predictive Check on PyMC3 Deterministic Variable)

TL; DR

pm.Deterministic进行后验预测检查的正确方法是什么?使用随机性(确定性也是随机的)作为输入的确定性变量?

太短; 不明白

假设我们有一个像这样的pymc3模型:

import pymc3 as pm

with pm.Model() as model:
    # Arbitrary, trainable distributions.
    dist1 = pm.Normal("dist1", 0, 1)
    dist2 = pm.Normal("dist2", dist1, 1)

    # Arbitrary, deterministic theano math.
    val1 = pm.Deterministic("val1", arb1(dist2))

    # Arbitrary custom likelihood.
    cdist = pm.DensityDistribution("cdist", logp(val1), observed=get_data())

    # Arbitrary, deterministic theano math.
    val2 = pm.Deterministic("val2", arb2(val1))

我可能会误解,但我的意图是对dist1dist2进行抽样,并将这些样本输入到确定性变量中。 后验预测检查是否只能在观察到的随机变量上进行?

使用pymc3.sampling.sample_ppcdist2和其他随机变量中获得后验预测样本是dist2 pymc3.sampling.sample_ppc ,但是我的模型的大部分值是从val1val2的状态得出的,给出了这些样本。

问题出现在pm.Deterministic(.)似乎返回th.TensorVariable 。 所以,当这被称为:

ppc = pm.sample_ppc(_trace, vars=[val1, val2])["val1", "val2"]

...和pymc3尝试这段代码:

    410        for var in vars:
--> 411            ppc[var.name].append(var.distribution.random(point=param,
    412                                                          size=size))

...它抱怨,因为th.TensorVariable显然没有.distribution

那么,通过确定性来推导随机样本后验样本的正确方法是什么? 我是否需要明确地创建一个采用随机后验样本并计算确定性值的th.function函数? 这看起来很愚蠢,因为pymc3已经有了这个图表。


TL; DR

What's the right way to do posterior predictive checks on pm.Deterministic variables that take stochastics (rendering the deterministic also stochastic) as input?

Too Short; Didn't Understand

Say we have a pymc3 model like this:

import pymc3 as pm

with pm.Model() as model:
    # Arbitrary, trainable distributions.
    dist1 = pm.Normal("dist1", 0, 1)
    dist2 = pm.Normal("dist2", dist1, 1)

    # Arbitrary, deterministic theano math.
    val1 = pm.Deterministic("val1", arb1(dist2))

    # Arbitrary custom likelihood.
    cdist = pm.DensityDistribution("cdist", logp(val1), observed=get_data())

    # Arbitrary, deterministic theano math.
    val2 = pm.Deterministic("val2", arb2(val1))

I may be misunderstanding, but my intention is for the posteriors of dist1 and dist2 to be sampled, and for those samples to fed into the deterministic variables. Is the posterior predictive check only possible on observed random variables?

It's straightforward to get posterior predictive samples from dist2 and other random variables using pymc3.sampling.sample_ppc, but the majority of my model's value is derived from the state of val1 and val2, given those samples.

The problem arises in that pm.Deterministic(.) seems to return a th.TensorVariable. So, when this is called:

ppc = pm.sample_ppc(_trace, vars=[val1, val2])["val1", "val2"]

...and pymc3 attempts this block of code in pymc3.sampling:

    410        for var in vars:
--> 411            ppc[var.name].append(var.distribution.random(point=param,
    412                                                          size=size))

...it complains because a th.TensorVariable obviously doesn't have a .distribution.

So, what is the right way to carry the posterior samples of stochastics through deterministics? Do I need to explicitly create a th.function that takes stochastic posterior samples and calculates the deterministic values? That seems silly given the fact that pymc3 already has the graph in place.


原文:https://stackoverflow.com/questions/42151292
更新时间:2022-09-15 16:09

最满意答案

这里有一个逻辑问题。 您最初使用calloc为一个对象分配空间,然后调用add_entry并将count等于0。

然后在索引current_count处添加新条目,该条目在此处为0。 然后你用current_count + 1调整内存大小,这也是1.所以你根本没有调整内存的大小。

在下一次迭代中, entry_count为1,并在entries[1]处添加一个新元素。 这就是问题所在,你正在越界访问内存,因为此时你只剩下一个对象的空间。

而不是通过current_count + 1重新分配,您应该通过current_count + 2重新分配,以便下一次迭代有空间将新元素放在内存末尾。

void *add_entry(Entry *entries, int current_count)
{
    Entry entry;

    printf("Enter name: ");
    scanf(" %[^\n]s", entry.name);
    printf("Enter number: ");
    scanf(" %[^\n]s", entry.number);
    printf("\n");

    entries[current_count] = entry;
    return realloc(entries, sizeof(Entry) * (current_count + 2));  // <-- +2
}

请注意,您的current_count变量总是比实际分配的大小落后一步,这就是为什么您需要+2

编辑

还要注意更自然的方式是先调整大小,然后插入新的对象。 所以我会用NULL初始化内存,并像这样做:

int main()
{
    size_t entry_count = 0;
    Entry *entries = NULL, *tmp;
    int choice = 0;

    while (1) {
        printf("Options:\n1) Add Entry\n2) Modify Entry\n3) Print Entries\n4) Exit\n\nSelect an option: ");
        scanf(" %d", &choice);
        switch (choice) {
        case 1:
            tmp = add_entry(entries, &entry_count);
            if(tmp == NULL)
            {
                // error handling
                // entries still point to the old memory
                // could be useful in error handling

                free(entries);
                return 1;
            }

            entries = tmp;
            break;
            // ...
        }
    }
}

void *add_entry(Entry *entries, size_t *current_count)
{
    if(current_count == NULL)
        return NULL;

    Entry entry;

    printf("Enter name: ");
    scanf(" %[^\n]s", entry.name);
    printf("Enter number: ");
    scanf(" %[^\n]s", entry.number);
    printf("\n");

    if(entries == NULL)
        *current_count = 0;

    Entry *tmp = realloc(entries, (*current_count + 1) * sizeof *entries);

    if(tmp == NULL)
        return NULL;

    entries = tmp;

    entries[(*current_count)++] = entry;

    return entries;
}

请注意,计数变量的realloc 增量发生在同一个函数中。 只有当一切顺利时,你才应该增加计数器。 还要注意, entries初始化为NULL ,因为realloc(NULL, size)等价于malloc(size)


There is a logic problem here. You initially allocate space for one object with calloc then call add_entry with the count equals to 0.

Then you add the new entry at index current_count which is 0 at this point. Then you resize the memory with current_count + 1, which is also 1. So you are not resizing the memory at all.

In the next iteration, entry_count is 1 and you add a new element at entries[1]. And that's the problem, you are accessing the memory out of bounds, because you still have space for only one object at this time.

Instead of reallocating by current_count + 1, you should reallocate by current_count + 2, so that the next iteration has space to put the new elements at the end of the memory.

void *add_entry(Entry *entries, int current_count)
{
    Entry entry;

    printf("Enter name: ");
    scanf(" %[^\n]s", entry.name);
    printf("Enter number: ");
    scanf(" %[^\n]s", entry.number);
    printf("\n");

    entries[current_count] = entry;
    return realloc(entries, sizeof(Entry) * (current_count + 2));  // <-- +2
}

Note that your current_count variable is always one step behind the real size of the allocation, that's why you need the +2

edit

Note also that the more natural way would be to resize first, and then insert the new object. So I would initialize the memory with NULL and do it like this:

int main()
{
    size_t entry_count = 0;
    Entry *entries = NULL, *tmp;
    int choice = 0;

    while (1) {
        printf("Options:\n1) Add Entry\n2) Modify Entry\n3) Print Entries\n4) Exit\n\nSelect an option: ");
        scanf(" %d", &choice);
        switch (choice) {
        case 1:
            tmp = add_entry(entries, &entry_count);
            if(tmp == NULL)
            {
                // error handling
                // entries still point to the old memory
                // could be useful in error handling

                free(entries);
                return 1;
            }

            entries = tmp;
            break;
            // ...
        }
    }
}

void *add_entry(Entry *entries, size_t *current_count)
{
    if(current_count == NULL)
        return NULL;

    Entry entry;

    printf("Enter name: ");
    scanf(" %[^\n]s", entry.name);
    printf("Enter number: ");
    scanf(" %[^\n]s", entry.number);
    printf("\n");

    if(entries == NULL)
        *current_count = 0;

    Entry *tmp = realloc(entries, (*current_count + 1) * sizeof *entries);

    if(tmp == NULL)
        return NULL;

    entries = tmp;

    entries[(*current_count)++] = entry;

    return entries;
}

Note here that the realloc and the increment of the counting variable happens in the same function. Only when everything goes OK, you should increase the counter. Also note that entries is initialized with NULL, because realloc(NULL, size) is equivalent to malloc(size).

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)