UnboundLocalError: local variable 'e1' referenced before assignment

Description of my problem:

This problem may sound silly, but I can’t fix it. Hope get your help. Thank you.

Description of my code:

object_A = theano.shared(A, name='A')
object_t = theano.shared(t, name='t')
object_tau = theano.shared(tau, name='tau')

with pm.Model() as basic_model:
    k  = pm.Normal('k',mu=0,sd=5)
    m  = pm.Normal('m',mu=0,sd=5)    

    delta = pm.distributions.continuous.Laplace('delta',0, object_tau,shape = S)

    gamma = -object_t_change * delta
    Y =  (k + theano.dot(object_A,delta)) * object_t +( m + theano.dot(object_A, gamma))

###The error output here:
File “D:\HSPcode-20181017 - 副本\fbprophet\prophet_regression_model.py”, line 135, in pm_Model_training
Y = (k + theano.dot(object_A,delta)) * object_t +( m + theano.dot(object_A, gamma))
File “C:\Python36\lib\site-packages\theano_init_.py”, line 210, in dot
(e0, e1))
UnboundLocalError: local variable ‘e1’ referenced before assignment

Versions and main components

  • PyMC3 Version: 3.5
  • Theano Version:1.0.3
  • Python Version: 3.6

By inspecting the theano.dot source. It seems that, e1 is always set to None. The important thing is that the lines 209-210 will only be reached if neither obejct_A nor delta, or object_A nor gamma define a dot method. Could you share a more detailed example in which you add the definitions of A, t, tau, S, and object_t_change? Those variable are not defined in your snippet and maybe your problem lies somewhere in their definition.

Another option is to just use theano.tensor.dot instead of theano.dot. The latter dispatches to the instance’s dot method to handle both sparse and dense tensors, while to former defines all of it for dense tensors.

all of A, t, tau, S, and object_t_change is numeric value. Wherein, A is a matrix, t /tau is numpy.array, S is a int type value. E.g. t = history_data[‘time’].values

Thank you. As you said , I use theano.tensor.dot instead of theano.dot , but there is still an error:
File “C:\Python36\lib\site-packages\theano\tensor\basic.py”, line 158, in as_tensor_variable
“Variable type field must be a TensorType.”, x, x.type)
theano.tensor.var.AsTensorError: (‘Variable type field must be a TensorType.’, A, <theano.gof.type.Generic object at 0x0000000010172080>)

   import theano.tensor as T
    Y =  (k + T.dot(object_A,delta)) * object_t +( m + T.dot(object_A, gamma))

The error still be in “Y = (k + T.dot(object_A,delta)) * object_t +( m + T.dot(object_A, gamma))” . Thank you.

This small snippet does not raise any error for me

import numpy as np
import theano
import pymc3 as pm

n = 5
S = 2

A = np.random.randn(n, S)  # It's the same if I do  A = np.matrix(np.random.randn(n, S))
t = np.random.randn(n,)
tau = np.random.randn(S,)
t_change = np.random.randn(S,)

object_A = theano.shared(A, name='A')
object_t = theano.shared(t, name='t')
object_tau = theano.shared(tau, name='tau')

with pm.Model() as basic_model:
    k  = pm.Normal('k',mu=0,sd=5)
    m  = pm.Normal('m',mu=0,sd=5)    

    delta = pm.distributions.continuous.Laplace('delta',
                                                0,
                                                object_tau,
                                                shape=S)

    gamma = -object_t_change * delta
    Y =  (k + theano.dot(object_A,delta)) * object_t +( m + theano.dot(object_A, gamma))

I’m sorry but I can’t help. You have to try to get a minimal self contained working example that shows the error for us to be able to help. The snippet you provided does not raise any errors for me.

1 Like

Yes, If I set A as a value(A = np.array([[0, 0],[0, 0],[0, 0],[1, 0], [1, 0], [1, 0],[1,0],[1, 0],[1,0],[1, 0],[1,0],[1, 1],[1,1]])), it will pass successfully. but if I use a function to get A ,as following:
def get_changepoint_matrix(t,t_change,T_time,S):
A = np.empty(shape=(T_time,S), dtype=object)
#A = np.zeros((T,S))
a_row = np.zeros(shape=S, dtype=object)

cp_idx = 0
#print ("degaaa",T,S,len(t),len(t_change),t,t_change)
for i in range(0,T_time,1):
    while ((cp_idx < S) and (t[i] >= t_change[cp_idx])):
        a_row[cp_idx] = 1
        cp_idx = cp_idx + 1
    A[i] = a_row
    #print ("a_row",a_row)
#print ("A",A)
return A

there will be an error " theano.tensor.var.AsTensorError: (‘Variable type field must be a TensorType.’, A, <theano.gof.type.Generic object at 0x000000000EE061D0>)"

It’s because A.dtype is object. theano does not know how to apply the dot product on an array of objects. It can only handle numerical values, such as float32, float64, int32, etc. If you could change the dtype of A to make something like this: A = np.empty(shape=(T_time,S), dtype=np.float) and a_row = np.zeros(shape=S, dtype=np.float), you should be fine. If you can’t change the A's datatype, then you wont be able to use it.

Thank you. As you said, the problem is solved.