ValueError: setting an array element with a sequence

Description of my problem:
This problem may sound silly, but I can’t fix it. Hope get your help. Thank you.
“k_s[0] = k
ValueError: setting an array element with a sequence.”

Description of my code:

    object_t_change = theano.shared(t_change, name='t_change')
    object_A = theano.shared(A, name='A')
    object_X_new = theano.shared(X_new, name='X')
    object_t = theano.shared(t, name='t')
    object_y = theano.shared(y, name='y')
    object_tau = theano.shared(tau, name='tau')
    object_cap = theano.shared(cap, name='cap')

    with pm.Model() as basic_model:
        k = pm.Normal('k', mu=0, sd=5, testval=kinit[0])
        m = pm.Normal('m', mu=0, sd=5, testval=kinit[1])
        sigma_obs = pm.HalfNormal('sigma_obs', sd=0.5)  # ~ normal(0, 0.5);

        beta = pm.Normal('beta', mu=0, sd=sigmas[0], shape=K)  # ~ normal(0, sigmas);
        delta = pm.distributions.continuous.Laplace('delta', 0, object_tau,
                                                    shape=S)  # delta ~ double_exponential(0, tau)

        k_s = np.zeros(shape=S+1, dtype=np.float)
        k_s[0] = k
        for i in range(0, S, 1):
                tmp = k_s[i] + delta[i]
                k_s[i+1] = tmp

        # Piecewise offsets
        gamma = np.zeros(shape=S, dtype=np.float)
        object_gamma = theano.shared(gamma, name='gamma')
        m_pr = m  # The offset in the previous segment
        for i in range(0, S, 1):
                object_gamma[i] = (object_t_change[i] - m_pr) * (1 - k_s[i] / k_s[i + 1])
                m_pr = m_pr + object_gamma[i]  # update for the next segment
        tmp_ones = np.zeros(shape=T_time, dtype=np.float)
        tmp1 = object_t - (m + T.dot(object_A, object_gamma_logistic))
        tmp2 = T.exp(-(k + T.dot(object_A, delta)) * tmp1)
        Y = object_cap / (tmp_ones + tmp2)

Assigning a tensor element is done in theano like this:

x = tt.zeros((S+1))
x = tt.inc_subtensor(x[0], k)

However, it’s almost never a good idea to use for loop to assign elements to a tensor. In your case, you should try doing a cumsum of delta instead.

Sorry , I don’t understand your meaning(“try doning a cumsum of delta instead”). How should I modify the code? Thank you.

Something like:

k_s = k + T.concatenate([[0.], delta.cumsum()], axis=0)

Thank you. But I modify the code like this, it have other error “TypeError: ‘TensorSharedVariable’ object does not support item assignment”. Hope get your further help. Thank you.

        k_s = T.zeros((S + 1))
        k_s = T.inc_subtensor(k_s[0], k)
        k_s = k + T.concatenate([[0.], delta.cumsum()], axis=0)

        # Piecewise offsets
        gamma_logistic = np.zeros(shape=S, dtype=np.float)
        object_gamma_logistic = theano.shared(gamma_logistic, name='gamma_logistic')
        m_pr = m  # The offset in the previous segment
        for i in range(0, S, 1):
            object_gamma_logistic[i] = (object_t_change[i] - m_pr) * (1 - k_s[i] / k_s[i + 1])
            m_pr = m_pr + object_gamma_logistic[i]  # update for the next segment

        #gamma = logistic_gamma(k, m, delta, object_t_change, S)
        tmp_ones = np.zeros(shape=T_time, dtype=np.float)
        tmp1 = object_t - (m + T.dot(object_A, object_gamma_logistic))
        tmp2 = T.exp(-(k + T.dot(object_A, delta)) * tmp1)
        Y = object_cap / (tmp_ones + tmp2)

You dont need

k_s = T.zeros((S + 1))
k_s = T.inc_subtensor(k_s[0], k)

Thank you. This segment code still have problem. Could you please help fix it? the code with iteration logic is:

        gamma_logistic = np.zeros(shape=S, dtype=np.float)
        object_gamma_logistic = theano.shared(gamma_logistic, name='gamma_logistic')
        m_pr = m  # The offset in the previous segment
        for i in range(0, S, 1):
            object_gamma_logistic[i] = (object_t_change[i] - m_pr) * (1 - k_s[i] / k_s[i + 1])
            m_pr = m_pr + object_gamma_logistic[i]  # update for the next segment

It is a bit difficult to rewrite it to get rid of the for loop (you can use theano.scan but usually it needs some trial and error). Try object_gamma_logistic = T.inc_subtensor(object_gamma_logistic[i], (object_t_change[i] - m_pr) * (1 - k_s[i] / k_s[i + 1]))

As your suggestion, I try, and there is a new error in “Y = object_cap / (tmp_ones + tmp2)” code line: “theano.tensor.var.AsTensorError: (‘Variable type field must be a TensorType.’, cap, <theano.gof.type.Generic object at 0x00000000131FFB38>)”.
Thank you.

Description of the latest code:
with pm.Model() as basic_model:
k = pm.Normal(‘k’, mu=0, sd=5, testval=kinit[0])
m = pm.Normal(‘m’, mu=0, sd=5, testval=kinit[1])
sigma_obs = pm.HalfNormal(‘sigma_obs’, sd=0.5) # ~ normal(0, 0.5);

beta = pm.Normal('beta', mu=0, sd=sigmas[0], shape=K)  # ~ normal(0, sigmas);
delta = pm.distributions.continuous.Laplace('delta', 0, object_tau,
                                            shape=S)  # delta ~ double_exponential(0, tau)

beta_m = beta * object_s_m
beta_a = beta * object_s_a

k_s = T.zeros((S + 1))
k_s = k + T.concatenate([[0.], delta.cumsum()], axis=0)

object_gamma_logistic = T.zeros(S)
m_pr = T.zeros(S + 1)
m_pr = T.inc_subtensor(m_pr[0], m)
for i in range(0, S, 1):
    object_gamma_logistic = T.inc_subtensor(object_gamma_logistic[i],
                                            (object_t_change[i] - m_pr[i]) * (1 - k_s[i] / k_s[i + 1]))
    m_pr = T.inc_subtensor(m_pr[i + 1], object_gamma_logistic[i] + m_pr[i])

tmp_ones = np.zeros(shape=T_time, dtype=np.float)
tmp1 = object_t - (m + T.dot(object_A, object_gamma_logistic))
tmp2 = T.exp(-(k + T.dot(object_A, delta)) * tmp1)
Y = object_cap / (tmp_ones + tmp2)

tmp_ones is just a vector or zeros, why do you need it?

Thank you. But, even through I modify the code like this, it still same error in code line “Y = object_cap / tmp2”, the error is “theano.tensor.var.AsTensorError: (‘Variable type field must be a TensorType.’, cap, <theano.gof.type.Generic object at 0x00000000131FFA58>)” . The code is as following:

        object_gamma_logistic = T.zeros(S)
        m_pr = T.zeros(S+1)
        m_pr = T.inc_subtensor(m_pr[0], m)
        for i in range(0, S, 1):
            object_gamma_logistic = T.inc_subtensor(object_gamma_logistic[i],(object_t_change[i] - m_pr[i]) * (1 - k_s[i] / k_s[i + 1]))
            #object_gamma_logistic[i] = (object_t_change[i] - m_pr) * (1 - k_s[i] / k_s[i + 1])
            #m_pr = m_pr + object_gamma_logistic[i]  # update for the next segment
            m_pr = T.inc_subtensor(m_pr[i+1],object_gamma_logistic[i]+m_pr[i])

        #gamma = logistic_gamma(k, m, delta, object_t_change, S)
        #tmp_ones = np.zeros(shape=T_time, dtype=np.float)
        tmp1 = object_t - (m + T.dot(object_A, object_gamma_logistic))
        tmp2 = T.exp((k + T.dot(object_A, delta)) * tmp1)
        Y = object_cap / tmp2

Try using cap directly: Y = cap / tmp2

But there still is an error in code line “y_obj = pm.Normal(“model_like”, mu=Y_new, sd=sigma_obs, observed=object_y)”. the error is:
"theano.tensor.var.AsTensorError: (‘Cannot convert 0 Elemwise{add,no_inplace}.0\n1 Elemwise{add,no_inplace}.0\n2 Elemwise{add,no_inplace}.0\n3 Elemwise{add,no_inplace}.0\n4 Elemwise{add,no_inplace}.0\n5 Elemwise{add,no_inplace}.0\n6 Elemwise{add,no_inplace}.0\n7 Elemwise{add,no_inplace}.0\n8 Elemwise{add,no_inplace}.0\n9 Elemwise{add,no_inplace}.0\n10 Elemwise{add,no_inplace}.0\n11 Elemwise{add,no_inplace}.0\n12 Elemwise{add,no_inplace}.0\nName: cap_scaled, dtype: object to TensorType’, <class ‘pandas.core.series.Series’>)
"

description of code:

        object_gamma_logistic = T.zeros(S)
        m_pr = T.zeros(S+1)
        #object_gamma_logistic = theano.shared(gamma_logistic, name='gamma_logistic')
        #m_pr = m  # The offset in the previous segment
        m_pr = T.inc_subtensor(m_pr[0], m)
        for i in range(0, S, 1):
            object_gamma_logistic = T.inc_subtensor(object_gamma_logistic[i],(object_t_change[i] - m_pr[i]) * (1 - k_s[i] / k_s[i + 1]))
            #object_gamma_logistic[i] = (object_t_change[i] - m_pr) * (1 - k_s[i] / k_s[i + 1])
            #m_pr = m_pr + object_gamma_logistic[i]  # update for the next segment
            m_pr = T.inc_subtensor(m_pr[i+1],object_gamma_logistic[i]+m_pr[i])

        #gamma = logistic_gamma(k, m, delta, object_t_change, S)
        #tmp_ones = np.zeros(shape=T_time, dtype=np.float)
        tmp1 = object_t - (m + T.dot(object_A, object_gamma_logistic))
        tmp2 = T.exp((k + T.dot(object_A, delta)) * tmp1)
        Y = cap / tmp2

        Y_new = Y + Y * T.dot(object_X_new, beta_m) + T.dot(object_X_new, beta_a)

        y_obj = pm.Normal("model_like", mu=Y_new, sd=sigma_obs, observed=object_y)  # normal(Y, sigma_obs)
        ff = pm.fit(method='advi',obj_optimizer = pm.adagrad_window,n = iter_num,obj_n_mc=25)
        trace_pars_tmp = ff.sample(500)

It’s pretty difficult to debug since I dont know what is your data input. But reading from the error you should do something like Y = cap.values / tmp2 (change the input into numpy arrays). You should probability to the same for all your inputs when you are working with theano.shared variables

@junpenglao, the problem is solved as you said. Thank you very much for your help.

But, my formula need this 1, It will still have a problem “theano.tensor.var.AsTensorError: (‘Variable type field must be a TensorType.’, cap, <theano.gof.type.Generic object at 0x00000000131FFB38>)”. in “Y = object_cap / (tmp_ones + tmp2)” in the code line .
How should I solve it again? Thank you.

My code should be:

tmp2 = T.exp(-(k + T.dot(object_A, delta)) * tmp1)
Y = cap.values / (1+tmp2)

You need to make sure you are using theano.shared to wrap a numpy array, so you should do: object_cap = theano.shared(cap.values, name='cap') (and the same for other theano.shared variables)