Theano import error: cannot import name stacklists - python

from theano.tensor import stacklists, scalars, matrices
from theano import function
a, b, c, d = scalars('abcd')
X = stacklists([[a, b], [c, d]])
f = function([a, b, c, d], X)
f(1, 2, 3, 4)
this is my program.
i am getting following error.can anybody help
ImportError Traceback (most recent call last)
<ipython-input-17-e4e1f4f75320> in <module>()
----> 1 from theano.tensor import stacklists, scalars, matrices
2 from theano import function
3 a, b, c, d = scalars('abcd')
4 X = stacklists([[a, b], [c, d]])
5 f = function([a, b, c, d], X)
ImportError: cannot import name stacklists

You probably have an old version of Theano; stacklist was recently introduced/renamed (a month ago). Sou you should update to the latest/dev version. If you want to stay in your version try importing tensor_of_scalars instead of stacklist.
To update follow the instructions here.

This error can be caused by one of two things.
The first one is pretty obvious: does theano.tensor define a name stacklists? Should it be, for example, stacklist?
Secondly, it can happen if something else you're importing has already imported the name in a way where doing so again would cause a circular reference. The second would have to be fixed by looking at your source files.

Related

How to manage undefined variables in a calculation

I have some predefined variables and some preexisted formulas(all multiplications). Formulas are calculating as expected besides when any of the variables are not defined.
Knowing that my formulas only handle multiplications I was thinking to give the value =1 to any undefined variable, in that way the calculation does break.
Is there any way to loop through the formulas, isolate the undefined variables and give them a value =1?
In the example below, ‘k’ is not defined and this calculation(kbd=kbd) is generating an error
#defined variables
a=10
b=5
c=3
d=6
e=7
f=2
g=9
#preexisted formulas
abc=a*b*c
fed=d*e*f
efg=e*f*g
kbd=k*b*d
NameError Traceback (most recent call last)
<ipython-input-10-f9e3059809fc> in <module>
----> 1 kbd=k*b*d
NameError: name 'k' is not defined
You can use something like this:
a=10
b=5
c=3
d=6
e=7
f=2
g=9
formulas = ['a*b*c','d*e*f','e*f*g','k*b*d']
for form in formulas:
variables = form.split('*')
for v in variables:
if v not in locals():
locals()[v]=1
eval(form)
Sympy is made for addressing your title 'How to manage undefined variables in a calculation'. The variables can be defined as Sympy symbols:
from sympy import *
from sympy.abc import a, b, c, d, e, f, g, k
# alternative of above line `a, b, c, d, e, f, g, k=symbols('a b c d e f g k')`
a=10
b=5
c=3
d=6
e=7
f=2
g=9
abc = a*b*c
fed = sympify(d*e*f) #you can convert an arbitrary expression to a type that can be used inside SymPy, based on https://docs.sympy.org/latest/modules/evalf.html; not really necessary for these multiplication examples
efg=e*f*g
kbd = k*b*d
print(N(abc)) #based on https://docs.sympy.org/latest/modules/evalf.html
print(abc)
print(fed) #based on https://docs.sympy.org/latest/modules/evalf.html
print(fed.evalf()) #based on https://docs.sympy.org/latest/modules/evalf.html
print(kbd)
print(simplify(kbd)) # based on https://www.tutorialspoint.com/sympy/sympy_simplification.html
kbd # - or `simplify(kbd)` - as last line in a Jupyter cell, you'll see fancy `30k` with `k` in italics in out cell.
That last line won't do anything in a script though, yet the output will look fancy in a Jupyter cell using the Python kernel where you've run %pip install sympy in the cell above. (You can try that in the cell of a Jupyter notebook here.)
Output from the print commands:
150.000000000000
84
84.0000000000000
30*k
30*k
Seen in 'Out' cell if run in Jupyter:
30𝑘

Input arguments for MATLAB Engine function

I'm trying to use MATLAB engine to call a MATLAB function in Python, but I'm having some problems. After manage to deal with NumPy arrays as input in the function, now I have some error from MATLAB:
MatlabExecutionError: Undefined function 'simple_test' for input
arguments of type 'int64'.
My Python code is:
import numpy as np
import matlab
import matlab.engine
eng = matlab.engine.start_matlab()
eng.cd()
Nn = 30
x= 250*np.ones((1,Nn))
y= 100*np.ones((1,Nn))
z = 32
xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())
Output = eng.simple_test(xx,yy,z,nargout=4)
A = np.array(Output[0]).astype(float)
B = np.array(Output[1]).astype(float)
C = np.array(Output[2]).astype(float)
D = np.array(Output[3]).astype(float)
and the Matlab function is:
function [A,B,C,D] = simple_test(x,y,z)
A = 3*x+2*y;
B = x*ones(length(x),length(x));
C = ones(z);
D = x*y';
end
Is a very simple example but I'm not able to run it!
I know the problem is in the z variable, because when I define z=32 the error is the one I mentioned, and when I change for z=32. the error changes to
MatlabExecutionError: Undefined function 'simple_test' for input
arguments of type 'double'.
but I don't know how to define z.

Sharing variables across modules

I have a program- main.py that generates values for a few parameters depending on the user provided inputs. I need to share these variables with other modules- aux.py. To do that I did the following (simplified code below) -
This the main.py file:
# these three variables are dynamically calculated depending on the user provided input
a = 12
b = 13
c = 14
def declare_global():
global a, b, c
declare_global()
import aux
aux.print_all()
This is the aux.py file
def print_all():
a = globals()['a']
b = globals()['b']
c = globals()['c']
print(a)
print(b)
print(b)
Running the main.py file results in the following error
Traceback (most recent call last): File
"/Users/new/Library/Preferences/PyCharmCE2018.3/scratches/global_experiment/main.py",
line 13, in
aux.print_all() File "/Users/new/Library/Preferences/PyCharmCE2018.3/scratches/global_experiment/aux.py",
line 2, in print_all
a = globals()['a'] KeyError: 'a'
There is a very similar post addressing the same issue here but the posts there suggest adding all global variables to a new file and then importing the created file. But I cannot do that as I'll have no prior information as to what values these variables will take.
So, how do I share variables across modules?
EDIT
I lost some complexity of the actual program in order to make a minimalistic example. The reason I cannot pass the variables a,b,c to the print_all function is because in the actual program, the variables a,b,c are not serializable. They are spark dataframe objects. And one of the modules I use there (which here I am representing by print_all) serializes all its inputs I end up with an error (spark dataframes aren't serializable). Using a few workarounds I have arrived here where I use global variables and no inputs to the functions.
a = 12
b = 13
c = 14
import aux
aux.print_all(a, b, c)
Then your aux file will become:
def print_all(a, b, c):
a = globals()['a']
b = globals()['b']
c = globals()['c']
print(a)
print(b)
print(b)

TypeError: 'module' object is not callable in python and pandas

I imported the module correctly into pandas and called it correctly using import main and then main.main(data, 1,10,2.5) but I am getting an error:
TypeError Traceback (most recent call last)
<ipython-input-52-e9913b227737> in <module>()
----> 1 main.main(data, 1, 10, 2.5)
38 dat_sh = data.shape[0]
39 #Z = random.sample(range(0,U),k_max)
---> 40 Z = cf.centroid_finder(data,sp_atr,k_max)
41
42 prototypes = {i: data[j:j+1].values.tolist()[0] for i,j in enumerate(Z)}
11 for i in range(dat_sh):
12 for j in range(dat_sh):
---> 13 D[i][j] = ed(sub_atr[i],sub_atr[j])
14
15
TypeError: 'module' object is not callable
ed is euclidean:
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
There may be some confusion regarding the importing of modules vs functions.
It's possible to recreate your error with a simple example, assuming that module/function importing has gotten mixed up somewhere along the way. Consider a case where we pass initial values a and b through a centroid_finder() function, which lives in cf.py:
# import cf.py as cf
import cf
a, b = ([1,2,3], [2,3,4])
cf.centroid_finder(a, b)
But centroid_finder() calls ed(), which lives in ed.py:
## cf.py
# import ed.py as ed
import ed
def centroid_finder(a, b):
print(ed(a, b))
## ed.py
from numpy import sqrt, sum
import numpy as np
def ed(X2, X1):
return sqrt(sum(np.subtract(X1,X2)**2))
Here, calling centroid_finder() will give the error you observed:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-e76ec6a0496c> in <module>()
3 a, b = ([1,2,3], [2,3,4])
4
----> 5 cf.centroid_finder(a, b)
cf.py in centroid_finder(a, b)
2
3 def centroid_finder(a, b):
----> 4 print(ed(a, b))
TypeError: 'module' object is not callable
That's because you imported a module, ed.py as ed...but what you wanted was to call the ed() function that lives inside of ed.py. That's ed.ed()!
Changing centroid_finder() to call ed.ed() produces the desired result:
# cf.py
import ed
def centroid_finder(a, b):
print(ed.ed(a, b))
Now, from the main script:
cf.centroid_finder(a, b)
# 1.73205080757
There's at least one undisclosed import shorthand in your example code, where you call sqrt in ed(). There isn't a natural sqrt() in Python, it most likely is imported from either math or numpy, e.g. from numpy import sqrt. That's not a problem, per se, but given the error you're getting about modules being non-callable, you might benefit from explicitly calling functions in your code from the modules they live in.
For example, using import numpy as np, call np.sqrt() instead of just importing sqrt() directly. This is a defensive programming posture that will prevent similar confusion in the future. (You do this already with np.subtract() in ed(); it's unclear why sqrt() doesn't get the same treatment.)

Using Theano.scan with shared variables

I want to calculate the sumproduct of two arrays in Theano. Both arrays are declared as shared variables and are the result of prior computations. Reading the tutorial, I found out how to use scan to compute what I want using 'normal' tensor arrays, but when I tried to adapt the code to shared arrays I got the error message TypeError: function() takes at least 1 argument (1 given). (See minimal running code example below)
Where is the mistake in my code? Where is my misconception? I am also open to a different approach for solving my problem.
Generally I would prefer a version which takes the shared variables directly, because in my understanding, converting the arrays first back to Numpy arrays and than again passing them to Theano, would be wasteful.
Error message producing sumproduct code using shared variables:
import theano
import theano.tensor as T
import numpy
a1 = [1,2,4]
a2 = [3,4,5]
Ta1_shared = theano.shared(numpy.array(a1))
Ta2_shared = theano.shared(numpy.array(a2))
outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))
Tsumprod_result, updates = theano.scan(fn=lambda Ta1_shared, Ta2_shared, prior_value:
prior_value + Ta1_shared * Ta2_shared,
outputs_info=outputs_info,
sequences=[Ta1_shared, Ta2_shared])
Tsumprod_result = Tsumprod_result[-1]
Tsumprod = theano.function(outputs=Tsumprod_result)
print Tsumprod()
Error message:
TypeError: function() takes at least 1 argument (1 given)
Working sumproduct code using non-shared variables:
import theano
import theano.tensor as T
import numpy
a1 = [1, 2, 4]
a2 = [3, 4, 5]
Ta1 = theano.tensor.vector("a1")
Ta2 = theano.tensor.vector("coefficients")
outputs_info = T.as_tensor_variable(numpy.asarray(0, 'float64'))
Tsumprod_result, updates = theano.scan(fn=lambda Ta1, Ta2, prior_value:
prior_value + Ta1 * Ta2,
outputs_info=outputs_info,
sequences=[Ta1, Ta2])
Tsumprod_result = Tsumprod_result[-1]
Tsumprod = theano.function(inputs=[Ta1, Ta2], outputs=Tsumprod_result)
print Tsumprod(a1, a2)
You need to change the compilation line to this one:
Tsumprod = theano.function([], outputs=Tsumprod_result)
theano.function() always need a list of inputs. If the function take 0 input, like in this case, you need to give an empty list for the inputs.

Categories