Problems with SQlite backend: cannot operate on closed database

I am trying to use an sqlite backend but it raises

ProgrammingError: Cannot operate on a closed database.

Here is a minimal working example:

import pymc3 as pm
from pymc3.backends import SQLite
import numpy as np

#simulate some data 
x = np.random.normal(100)
y = x + np.random.normal(100)

with pm.Model() as model:     
    sigma = pm.HalfCauchy('sigma', beta=10, testval=1.)
    intercept = pm.Normal('Intercept', 0, sigma=20)
    x_coeff = pm.Normal('x', 0, sigma=20)

    # Define likelihood
    likelihood = pm.Normal('y', mu=intercept + x_coeff * x,
                        sigma=sigma, observed=y)

    backend = SQLite('mod_trace.sqlite')
    trace2 = pm.sample(draws=5000, tune=3000, target_accept=.95, trace=backend)

Am I doing this wrong? This was meant to be a workaround for a model in which pymc3 is using up all my memory and freezing my computer when I run it for very long.

You’re using it correctly as far as I can tell. The reduced example as shown below also reproduces the error. I recommend raising an issue on Github, though the current plan is to deprecate some of the backends as they are not heavily used. You can also use the HDF5 backend as a workaround, though this may be a little slower.

import pymc3 as pm
from pymc3.backends import SQLite

with pm.Model() as model:     
    sigma = pm.Normal('sigma')
    backend = SQLite('mod_trace.sqlite')
    samples = pm.sample(trace=backend)

Thank you. I actually just found a similar issue on Github so between that and the fact that it’s going to be deprecated I will just move on with this. The other backends do seem to work fine.