IndexError: tuple index out of range

I am trying to plug in financial data into the VI example to do a classification with it.

The data is set in the following way, that should be right because it is working with a simple Bayesian Regression with PyMC3.

df = df.reset_index()

X= (df['Close']).values
Y= (df['Close']).index.values

X.shape, Y.shape

But I get an error message, when I try to execute the code

def construct_nn(ann_input, ann_output):
    n_hidden = 5

    # Initialize random weights between each layer
    init_1 = np.random.randn(X.shape[1], n_hidden).astype(floatX)
    init_2 = np.random.randn(n_hidden, n_hidden).astype(floatX)
    init_out = np.random.randn(n_hidden).astype(floatX)

    with pm.Model() as neural_network:
        # Trick: Turn inputs and outputs into shared variables using the data container pm.Data
        # It's still the same thing, but we can later change the values of the shared variable
        # (to switch in the test-data later) and pymc3 will just use the new data.
        # Kind-of like a pointer we can redirect.
        # For more info, see:
        ann_input = pm.Data('ann_input', X)
        ann_output = pm.Data('ann_output', Y)

        # Weights from input to hidden layer
        weights_in_1 = pm.Normal('w_in_1', 0, sigma=1,
                                 shape=(X.shape[1], n_hidden),

        # Weights from 1st to 2nd layer
        weights_1_2 = pm.Normal('w_1_2', 0, sigma=1,
                                shape=(n_hidden, n_hidden),

        # Weights from hidden layer to output
        weights_2_out = pm.Normal('w_2_out', 0, sigma=1,

        # Build neural-network using tanh activation function
        act_1 = pm.math.tanh(,
        act_2 = pm.math.tanh(,
        act_out = pm.math.sigmoid(,

        # Binary classification -> Bernoulli likelihood
        out = pm.Bernoulli('out',
                           total_size=Y.shape[0] # IMPORTANT for minibatches
    return neural_network

neural_network = construct_nn(X, Y)

IndexError                                Traceback (most recent call last)
Input In [7], in <cell line: 49>()
     42         out = pm.Bernoulli('out',
     43                            act_out,
     44                            observed=ann_output,
     45                            total_size=Y.shape[0] # IMPORTANT for minibatches
     46                           )
     47     return neural_network
---> 49 neural_network = construct_nn(X, Y)

Input In [7], in construct_nn(ann_input, ann_output)
      2 n_hidden = 5
      4 # Initialize random weights between each layer
----> 5 init_1 = np.random.randn(X.shape[1], n_hidden).astype(floatX)
      6 init_2 = np.random.randn(n_hidden, n_hidden).astype(floatX)
      7 init_out = np.random.randn(n_hidden).astype(floatX)

IndexError: tuple index out of range

Full jupyter notebook link is here:

I suspect that X.shape is something like (100,) (because X is a series/vector), so trying to take the second element of the shape is throwing the index error.

Thank you so much for your answer. I will need to think about it a bit to fully understand what is going on. In any case, the new PyMC is really great. The examples with GP and NUTS for time series took up my attention already :smiley:

Glad you found them useful. The documentation (including all the examples) is still a bit of a work in progress, but it is coming along nicely and you can always ask here if there is anything that you can’t find in the documentation!

Thank you!

So why do I get -

100.00% [30000/30000 01:58<00:00 Average Loss = inf]

Finished [100%]: Average Loss = nan

when I set X.shape[0] to get the Network up and running? And why does this produce a posterior and the trace?

And of course the most important - what can I do about it to get it fixed?

I also ask this because X.shape[1] does not seem to be a way that works.