Theano / Theano

Theano was a Python library that allows you to define, optimize, and evaluate mathematical expressions involving multi-dimensional arrays efficiently. It is being continued as PyTensor: www.github.com/pymc-devs/pytensor

Home Page:https://www.github.com/pymc-devs/pytensor

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Out of memory when compiling posterior

preet-007 opened this issue · comments

When running the model , the error occurs, i am new to theano and am lost how to solve this:

with pm.Model() as Covid19:
logb = lambda_t_log
population =17447213
gamma =0.34
prior_E=50
prior_gamma = 1/8
pr_incubation= 5
pr_s_incubation= 2
sigma_incubation=0.4
pr_sigma_gamma=0.2
data_len = 138
theano.compile.mode.Mode(linker='py', optimizer='fast_compile')
theano.config.exception_verbosity='high'

Exposed = pm.HalfCauchy(
    name ='exposed', beta=prior_E, shape=(16,138)
)
a,b = infec_prior()    

Incubated = pm.Gamma(
    name = 'incubated',  alpha = a, beta = b, shape= 138
    )

S = population - Incubated - pm.math.sum(Exposed, axis=0) 

lambda_t = tt.exp(logb)
new_I = tt.zeros_like(Incubated)

if pr_s_incubation is None:
    median_incubation = pr_incubation
else:
    median_incubation = pm.Normal(
        name = 'inc', mu=pr_incubation,
        sigma=pr_s_incubation,
    )



# Choose transition rates (E to I) according to incubation period distribution

x = np.arange(1, 16)[:, None]


ro = tt_lognormal(x, tt.log(median_incubation), sigma_incubation)

# Runs SEIR model:

outputs, _ = theano.scan(
    fn=next_day,
    sequences=[lambda_t],
    outputs_info=[
        S,
        dict( initial = Exposed, taps=[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10,-11,-12,-13,-14,-15]),
        Incubated,
        new_I,
    ],
    non_sequences=[gamma, ro, population],
)
S_t, new_E_t, I_t, new_I_t = outputs


pr_beta_sigma_obs=30,
nu=4,
offset_sigma=1


sigma_obs = pm.HalfCauchy(
    'sigmaobs', beta=pr_beta_sigma_obs, shape = 138,
)
pm.StudentT(
    name='name_student_t',
    nu=nu,
    mu=new_I_t[:data_len],
    sigma=tt.abs_(new_I_t[:data_len] + offset_sigma) ** 0.5
    * sigma_obs,  # offset and tt.abs to avoid nans
    observed =smoothed,
)

with Covid19:
samples = pm.sample(trace=1000, draws =1000, mode='DebugMode')

ValueError: expected an ndarray
Apply node that caused the error: Elemwise{mul,no_inplace}(TensorConstant{5.73157443..412693e-08}, Sum{acc_dtype=float64}.0)
Toposort index: 69
Inputs types: [TensorType(float64, scalar), TensorType(float64, scalar)]
Inputs shapes: [(), ()]
Inputs strides: [(), ()]
Inputs values: [array(5.73157444e-08), array(0.)]
Inputs type_num: [12, 12]
Outputs clients: [['output']]

Debugprint of the apply node:
Elemwise{mul,no_inplace} [id A] <TensorType(float64, scalar)> ''
|TensorConstant{5.73157443..412693e-08} [id B] <TensorType(float64, scalar)>
|Sum{acc_dtype=float64} [id C] <TensorType(float64, scalar)> ''
|Elemwise{Mul}[(0, 0)] [id D] <TensorType(float64, vector)> ''
|Elemwise{Composite{(i0 + (-i1))}} [id E] <TensorType(float64, vector)> ''
| |<TensorType(float64, vector)> [id F] <TensorType(float64, vector)>
| |Elemwise{Composite{(AND(GE((i0 - i1), i2), LE((i0 - i1), i3)) * i4)}} [id G] <TensorType(float64, vector)> ''
| |<TensorType(float64, vector)> [id H] <TensorType(float64, vector)>
| |<TensorType(float64, vector)> [id I] <TensorType(float64, vector)>
| |TensorConstant{(1,) of -1} [id J] <TensorType(int8, (True,))>
| |TensorConstant{(1,) of 17447213} [id K] <TensorType(int32, (True,))>
| |<TensorType(float64, vector)> [id L] <TensorType(float64, vector)>
|<TensorType(float64, vector)> [id H] <TensorType(float64, vector)>
|incubated[t-1] [id M] <TensorType(float64, vector)>

HINT: Re-running with most Theano optimization disabled could give you a back-trace of when this node was created. This can be done with by setting the Theano flag 'optimizer=fast_compile'. If that does not work, Theano optimizations can be disabled with 'optimizer=None'.
HINT: Use the Theano flag 'exception_verbosity=high' for a debugprint and storage map footprint of this apply node.