My model crashed under pymc v4

Hello everyone. I am running my Bayesian model using pymc4.But It
crushed . I wanna know where my problem was. Here is my code

import pymc as pm
import numpy as np
import aesara.tensor as at
from aesara.compile.ops import as_op
assert pm.__version__ == '4.0.0b6'


data = [0.05 + i for i in np.random.uniform(-0.05, 0.2, 1000)]

##https://www.pymc.io/projects/docs/en/latest/learn/core_notebooks/pymc_overview.html?highlight=switch#case-study-2-coal-mining-disasters 
## example :Arbitary deterministics. I changed the function in the url above
@as_op(itypes=[at.dscalar, at.dscalar], otypes=[at.dscalar])
def piecewise_timespan_label(back, forward):
    td = forward - back
    if td >= 12:
        return 1
    elif td >= 8:
        return 0.9
    elif td >= 6:
        return 0.8
    elif td >= 4:
        return 0.6
    elif td >= 2:
        return 0.4
    elif td >= 1:
        return 0.2
    elif td >= 0.5:
        return 0.1
    elif td >= 0.05:
        return 0.08
    else:
        return 0.05


with pm.Model() as model_deterministic:
    back = pm.Exponential("back", 0.2)
    forward = pm.Exponential("forward", 0.2)
    ratio_est = piecewise_timespan_label(back, forward)
    eps = pm.HalfCauchy("eps", 0.5)
    y = pm.Normal("y", ratio_est, sigma=eps, observed=data)

with model_deterministic:
    idata = pm.sample(1000)

print('here')

Here is my wronging message:

Canceled future for execute_request message before replies were done
The Kernel crashed while executing code in the the current cell or a previous cell. Please review the code in the cell(s) to identify a possible cause of the failure. Click here for more info. View Jupyter log for further details.

My system is window 10 21H2
I am running it on vscode jupyter:v2022.4.1021342353
aesara 2.5.1
pymc ‘4.0.0b6’
numpy 1.23.0rc1

Thank in advance for reply

Welcome!

I would first recommend trying to run your code in script rather than a notebook. It is often more difficult to diagnose what is happening because the notebook does not show all the output.

2 Likes

thank you for your kind reply. I will try again

1 Like

I tried again with py script.
I added some print code.

import pymc as pm
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import arviz as az
import aesara.tensor as at
from aesara.compile.ops import as_op
assert pm.__version__ == '4.0.0b6'
print("import right")

data = [0.05 + i for i in np.random.uniform(-0.05, 0.2, 1000)]

@as_op(itypes=[at.dscalar, at.dscalar], otypes=[at.dscalar])
def piecewise_timespan_label(back, forward):
    td = forward - back
    print(type(td))
    if td >= 12:
        return 1
    elif td >= 8:
        return 0.9
    elif td >= 6:
        return 0.8
    elif td >= 4:
        return 0.6
    elif td >= 2:
        return 0.4
    elif td >= 1:
        return 0.2
    elif td >= 0.5:
        return 0.1
    elif td >= 0.05:
        return 0.08
    else:
        return 0.05
    

print("data right")

with pm.Model() as model_deterministic:
    back = pm.Exponential("back", 0.2)
    forward = pm.Exponential("forward", 0.2)
    print(type(forward))
    ratio_est = piecewise_timespan_label(back, forward)
    print(type(ratio_est))
    eps = pm.HalfCauchy("eps", 0.5)
    y = pm.Normal("y", ratio_est, sigma=eps, observed=data)

print("model right")
with model_deterministic:
    idata = pm.sample(1000)

print('here')

then I use the python code python eda.py under my local path in my terminal.
Here is the output

Could not locate executable g77
Could not locate executable f77
Could not locate executable ifort
Could not locate executable ifl
Could not locate executable f90
Could not locate executable DF
Could not locate executable efl
Using built-in specs.
COLLECT_GCC=C:\ProgramData\chocolatey\lib\mingw\tools\install\mingw64\bin\gfortran.exe
COLLECT_LTO_WRAPPER=c:/programdata/chocolatey/lib/mingw/tools/install/mingw64/bin/../libexec/gcc/x86_64-w64-mingw32/11.2.0/lto-wrapper.exe
OFFLOAD_TARGET_NAMES=nvptx-none
Target: x86_64-w64-mingw32
Configured with: ../configure --prefix=/R/winlibs64_stage/inst_gcc-11.2.0/share/gcc --build=x86_64-w64-mingw32 --host=x86_64-w64-mingw32 --enable-offload-targets=nvptx-none --with-pkgversion='MinGW-W64 x86_64-posix-seh, built by Brecht Sanders' --with-tune=generic --enable-checking=release --enable-threads=posix --disable-sjlj-exceptions --disable-libunwind-exceptions --disable-serial-configure --disable-bootstrap --enable-host-shared --enable-plugin --disable-default-ssp 
--disable-rpath --enable-libstdcxx-pch --enable-libstdcxx-time=yes --disable-libstdcxx-debug --disable-version-specific-runtime-libs --with-stabs --disable-symvers --enable-languages=c,c++,fortran,lto,objc,obj-c++,d,jit --disable-gold --disable-nls --disable-stage1-checking --disable-win32-registry --disable-multilib --enable-ld --enable-libquadmath --enable-libada --enable-libssp --enable-libstdcxx --enable-lto --enable-fully-dynamic-string --enable-libgomp --enable-graphite --enable-mingw-wildcard --with-mpc=/d/Prog/winlibs64_stage/custombuilt --with-mpfr=/d/Prog/winlibs64_stage/custombuilt --with-gmp=/d/Prog/winlibs64_stage/custombuilt --with-isl=/d/Prog/winlibs64_stage/custombuilt --enable-install-libiberty --enable-__cxa_atexit --without-included-gettext --with-diagnostics-color=auto --enable-clocale=generic --with-libiconv --with-system-zlib --with-build-sysroot=/R/winlibs64_stage/gcc-11.2.0/build_mingw/mingw-w64 CFLAGS=-I/d/Prog/winlibs64_stage/custombuilt/include/libdl-win32
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.2.0 (MinGW-W64 x86_64-posix-seh, built by Brecht Sanders)
WARNING (aesara.tensor.blas): Using NumPy C-API based implementation for BLAS functions.
import right
data right
<class 'aesara.tensor.var.TensorVariable'>
<class 'aesara.tensor.var.TensorVariable'>
model right
<class 'numpy.float64'>

Obviously the import ,data, model code run right.But when I need to inference the model.It broke out.

You may need to install mkl-services. See here and the updated installation instructions.

1 Like

ok. As you recommended.

I add the .aesararc script in my C:\Users\<username> following the updated installation instructions .

The warning do disappear. Thank you .

And I installed the mkl-services ,which version is 2.4.0. Then I tried the the python code python eda.py under my local path in my terminal.
Here is the output.

import right
data right
forward type <class 'aesara.tensor.var.TensorVariable'>
ratioest type <class 'aesara.tensor.var.TensorVariable'>
model right
td type <class 'numpy.float64'>

I add the xxx type in the print code for clarification.

But the inference code still don’t work out . :sob:

May it be my model constructed wrong ?

Can you run the coal diaster?

import pymc as pm
import aesara.tensor as at
from aesara.compile.ops import as_op

@as_op(itypes=[at.lscalar], otypes=[at.lscalar])
def crazy_modulo3(value):
    print('type of value is ' + str(type(value)))
    if value > 0:
        return value % 3
    else:
        return (-value + 1) % 3


with pm.Model() as model_deterministic:
    a = pm.Poisson("a", 1)
    b = crazy_modulo3(a)

    pm.sample()

If that doesn’t sample for you, then it’s likely an installation issue.

Regardless, I can’t get your model to sample. Perhaps there’s an issue with the signature and/or return value of your op function? Any idea @ricardoV94 ?

I can run the coal disater code you posted.

Would this be a situation where using Aesara switch instead of if statements would make a difference?

1 Like