# Initializing values for multiple chains

**URL:** <https://discourse.pymc.io/t/initializing-values-for-multiple-chains/12188>\
**Category:** v5\
**Created:** [May 23, 2023, 10:47am UTC](https://discourse.pymc.io/t/initializing-values-for-multiple-chains/12188 "2023-05-23T10:47:52Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![shakasaki](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/shakasaki/32/5975_2.png) [@shakasaki](https://discourse.pymc.io/u/shakasaki)\
**Post date:** [May 23, 2023, 10:47am UTC](https://discourse.pymc.io/t/initializing-values-for-multiple-chains/12188/1 "2023-05-23T10:47:52Z")

</div>

Hello, I am trying to figure out how to initialize values for an MCMC sampler with multiple chains. I have no gradient information for my problem so at this stage I use DMetropolisZ. I found some information [here](https://discourse.pymc.io/t/unexpected-initial-evaluation-results/9763/5) but it does not really apply since it is for the NUTS sampler.

Here is the example code:

```auto
import numpy as np
import pymc as pm

chains = 4

with pm.Model(coords={"variable": ['a', 'b', 'c'],
              "property": ['x', 'y', 'z']}) as model:
    prior_x = pm.Uniform(name='x',
                         lower=0,
                         upper=1,
                         dims='variable')
    prior_y = pm.Uniform(name='y',
                         lower=1,
                         upper=2,
                         dims='variable')

    prior_z = pm.Uniform(name='z',
                         lower=2,
                         upper=3,
                         dims='variable')

    init_vals = {'x': [0.5]*3,
                 'y': [1.5]*3,
                 'z': [2.5]*3, }

    idata = pm.sample(draws=1000,
                      step=pm.DEMetropolisZ(),
                      initvals=init_vals,
                      chains=chains,
                      tune=500)

x_start = idata.posterior['x'].to_numpy()[:, 0, :]
y_start = idata.posterior['y'].to_numpy()[:, 0, :]
z_start = idata.posterior['z'].to_numpy()[:, 0, :]

assert np.all(x_start == 0.5)
assert np.all(y_start == 1.5)
assert np.all(z_start == 2.5)

```

First problem is that the init\_values are not assigned (assertion fails).

Second problem is that I want to actually initialize values for each chain, so I would expect to be able to pass a matrix for each variable that has shape (3, 4). So (variable, chain). But using thie init dictionary:

```auto
    init_vals = {'x': np.ones((3, chains))*0.5,
                 'y': np.ones((3, chains))*1.5,
                 'z': np.ones((3, chains))*2.5}

```

gives the following error:

> TypeError: Cannot convert Type TensorType(float64, (3, 4)) (of Variable Elemwise{add,no\_inplace}.0) into Type TensorType(float64, (3,)). You can try to manually convert Elemwise{add,no\_inplace}.0 into a TensorType(float64, (3,)).

The larger context of this is that I am running pymc with DMetropolisZ using about 40 chains on 20 variables with 3 coordinates each, and they always all initiate from the save value (center of the uniform prior distribution) and get stuck at local minima.

Thank you for the feedback!
