Autograd breaks np.empty_like - python

I'm trying to take the gradient of a function in which I assign numpy array elements individually (assigning local forces to a global force vector in an FEA), but this appears to break Autograd -- if I use np.zeros for the global array I get ValueError: setting an array element with a sequence, while if I use np.empty I get NotImplementedError: VJP of empty_like wrt argnums (0,) not defined.
Example:
import autograd.numpy as np
from autograd import jacobian, grad
def test(input):
a = np.empty_like(input)
a[:] = input[:]
grad(test)(np.array([0.]))
Gives the error:
C:\Miniconda3\python.exe C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py
Traceback (most recent call last):
File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 31, in __init__
vjpmaker = primitive_vjps[fun]
KeyError: <function primitive.<locals>.f_wrapped at 0x000001AB1D0AA8C8>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py", line 8, in <module>
grad(test)(np.array([0.]))
File "C:\Miniconda3\lib\site-packages\autograd\wrap_util.py", line 20, in nary_f
return unary_operator(unary_f, x, *nary_op_args, **nary_op_kwargs)
File "C:\Miniconda3\lib\site-packages\autograd\differential_operators.py", line 25, in grad
vjp, ans = _make_vjp(fun, x)
File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 10, in make_vjp
end_value, end_node = trace(start_node, fun, x)
File "C:\Miniconda3\lib\site-packages\autograd\tracer.py", line 10, in trace
end_box = fun(start_box)
File "C:\Miniconda3\lib\site-packages\autograd\wrap_util.py", line 15, in unary_f
return fun(*subargs, **kwargs)
File "C:/Users/JoshuaF/Desktop/gripper/softDrone/bug_test.py", line 5, in test
a = np.empty_like(input)
File "C:\Miniconda3\lib\site-packages\autograd\tracer.py", line 45, in f_wrapped
node = node_constructor(ans, f_wrapped, argvals, kwargs, argnums, parents)
File "C:\Miniconda3\lib\site-packages\autograd\core.py", line 35, in __init__
.format(fun_name, parent_argnums))
NotImplementedError: VJP of empty_like wrt argnums (0,) not defined
Is there any way to use Autograd on a numpy array which is assembled element-wise?

Based on the tutorial https://github.com/HIPS/autograd/blob/master/docs/tutorial.md, it looks like array assignment is unfortunately not supported in autograd functions.

Related

Dimension mismatch error in python while evaluating a function of two arguments

The function evaluates if I input a pair of arguments. But when I try to evaluate the function at grid points, it fails, saying "ValueError: dimension mismatch". I paste the code below. Could anyone help with the problem?
import numpy as np
from qutip import*
import pylab as plt
from numpy import eye,pi,kron,random,vdot,absolute,power,sqrt,matmul
from numpy import exp,arange
from pylab import meshgrid,cm,imshow,contour,clabel,colorbar,axis,title,show
j0= tensor(sigmaz(),identity(2),identity(2))
def otoc(k0,n):
u0=(-1j*k0/6 *(tensor(sigmaz(),sigmaz(),identity(2)))).expm()
jn= u0.inv()**n*j0*u0**n
return ((commutator(jn,j0)* commutator(j0,jn)).tr()/8)
k0 = np.arange(0,4,0.1)
n = np.linspace(0,50,50)
X,Y = meshgrid(k0, n)
Z = otoc(X, Y)
The error showing is the following
Traceback (most recent call last):
File "/Users/sreerampg/Dropbox/myself/programs/levy's lemma/untitled8.py", line 27, in <module>
Z = otoc(X, Y)
File "/Users/sreerampg/Dropbox/myself/programs/levy's lemma/untitled8.py", line 20, in otoc
u0=(-1j*k0/6 *(tensor(sigmaz(),sigmaz(),identity(2)))).expm()
File "/Applications/anaconda3/lib/python3.7/site-packages/qutip/qobj.py", line 599, in __rmul__
return other * self.data
File "/Applications/anaconda3/lib/python3.7/site-packages/scipy/sparse/base.py", line 550, in __rmul__
return (self.transpose() * tr).transpose()
File "/Applications/anaconda3/lib/python3.7/site-packages/cvxpy/interface/scipy_wrapper.py", line 31, in new_method
return method(self, other)
File "/Applications/anaconda3/lib/python3.7/site-packages/scipy/sparse/base.py", line 516, in __mul__
raise ValueError('dimension mismatch')
ValueError: dimension mismatch

TypeError: 'float' object is not subscriptable during scipy minimize

I'm trying to estimate a maximum likelihood model in python. I set up both the likelihood function and the analytic jacobian. When I run scipy minimize, I get a bizarre error (displayed below). This error doesn't seem to occur when I omit the jacobian, but I can't figure out why.
from numpy import log,sum,var
from numba import njit
#njit
def log_likelihood(params,surg_fx,surg_fx_ses):
mu_var = params[0]
exp_var = mu_var + surg_fx_ses**2
log_lik = -((surg_fx)**2 / (2*exp_var)) - .5*log(exp_var)
neg_sum_log_lik = -sum(log_lik)
print(mu_var)
print(neg_sum_log_lik)
if np.isnan(neg_sum_log_lik):
return 1e20
else:
return neg_sum_log_lik
#njit
def log_lik_jac(params,surg_fx,surg_fx_ses):
mu_var = params[0]
exp_var = mu_var + surg_fx_ses**2
jc = -sum(((surg_fx)**2 / (2*(exp_var**2))) - (.5/exp_var))
print(mu_var)
print(jc)
return jc
x0 = [np.var(cost_params3)]
shrinkage_est = minimize(log_likelihood,x0,args=(cost_params3,cost_SEs3),jac=log_lik_jac,options={'disp':True},method='BFGS')
cost_params3 and cost_SEs3 are (205,)-shaped numpy arrays.
And the return is:
0.10423462356390442
-580.1534424527905
0.10423462356390442
-67.02947836460727
[ 1.11423462]
26.84532144252225
[ 1.11423462]
77.95606471086792
[ 0.3741784]
-54.28224588483895
[ 0.3741784]
150.90730570822998
[ 0.19152581]
-79.19268133113846
[ 0.19152581]
68.81484893304786
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 444, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 973, in _minimize_bfgs
A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
TypeError: 'float' object is not subscriptable
I'm not really sure why this runs for a few iterations and just fails, especially given that nothing is being subscripted here. I'm also not sure why it seems to become a list after the first iteration? I tried running it without numba but it stopped at the same place with a different error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/_minimize.py", line 444, in minimize
return _minimize_bfgs(fun, x0, args, jac, callback, **options)
File "/usr/local/anaconda3/lib/python3.5/site-packages/scipy/optimize/optimize.py", line 973, in _minimize_bfgs
A1 = I - sk[:, numpy.newaxis] * yk[numpy.newaxis, :] * rhok
IndexError: invalid index to scalar variable.
Any help would be much appreciated!

matplotlib.scatter color argument not accepting numpy array

I have written the following python plotting script using matplotlib:
import pynbody as pyn
import numpy as np
import matplotlib.pyplot as plt
import glob
s = pyn.load('./ballsV2.00001')
sl = s.g[np.where((s.g['z'] < 0.005) & (s.g['z']>-0.005))]
sx = s.s['x'][0]
sy = s.s['y'][0]
sz = s.s['z'][0]
r2 = ((s.g['x']-sx)**2+(s.g['y']-sy)**2+(s.g['z']-sz)**2)
Flux = np.array(1./(4*np.pi*r2)*np.exp(-1*7.00114988051*np.sqrt(r2)))
print(type(np.log10(sl['radFlux'])))
print(type(np.log10(Flux)))
plt.figure(figsize = (15,12))
#plt.scatter(sl['x'],sl['y'],c=np.log10(sl['radFlux']),s=75,edgecolors='none', marker = '.',vmin=-6,vmax=1)
plt.scatter(sl['x'],sl['y'],c=np.log10(Flux),s=75,edgecolors='none', marker = '.',vmin=-8,vmax=4)
plt.xlim([-0.5,0.5])
plt.ylim([-0.5,0.5])
plt.xlabel("x")
plt.ylabel("y")
plt.colorbar(label="log(Code Flux)")
plt.savefig('./ballsV2_0.1.pdf')
plt.savefig('./ballsV2_0.1.png')
plt.show()
plt.close()
When I run the script I get the following error:
foo#bar ~/Data/RadTransfer/Scaling_Tests/ballsV2 $ py
balls.py
balls.py:15: RuntimeWarning: divide by zero encountered in log10
print(type(np.log10(sl['radFlux'])))
<class 'numpy.ndarray'>
<class 'numpy.ndarray'>
Traceback (most recent call last):
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 141, in to_rgba
rgba = _colors_full_map.cache[c, alpha]
KeyError: (-4.1574455411341349, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 192, in _to_rgba_no_colorcycle
c = tuple(map(float, c))
TypeError: 'numpy.float64' object is not iterable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "balls.py", line 17, in <module>
plt.scatter(sl['x'],sl['y'],c=np.log10(Flux),s=75,edgecolors='none', marker = '.',vmin=-8,vmax=4)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3435, in scatter
edgecolors=edgecolors, data=data, **kwargs)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1892, in inner
return func(ax, *args, **kwargs)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 4028, in scatter
alpha=alpha
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 890, in __init__
Collection.__init__(self, **kwargs)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 139, in __init__
self.set_facecolor(facecolors)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 674, in set_facecolor
self._set_facecolor(c)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/collections.py", line 659, in _set_facecolor
self._facecolors = mcolors.to_rgba_array(c, self._alpha)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 237, in to_rgba_array
result[i] = to_rgba(cc, alpha)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 143, in to_rgba
rgba = _to_rgba_no_colorcycle(c, alpha)
File "/home/grondjj/anaconda3/lib/python3.6/site-packages/matplotlib/colors.py", line 194, in _to_rgba_no_colorcycle
raise ValueError("Invalid RGBA argument: {!r}".format(orig_c))
ValueError: Invalid RGBA argument: -4.1574455411341349
Ignore the divide by zero stuff,the issue is the scatter plot function isn't taking my array of values to map colour to. What is strange is that the commented out scatter plot command above it runs fine. The only difference is the array of values I am passing it. I made sure to cast them to the same type (they are both <class 'numpy.ndarray'>). Also, the values themselves are more sane ranging between ~4000 and 1E-7 in the Flux array, it is only the np.log10(sl['radFlux'] that has the divide by zero errors and that one works. Any suggestions?
Flux and np.log10(sl['radFlux']) ended up being different lengths. sl (a slice of s) was not used to compute r2, so Flux ended up being to big. It would be nice if matplotlib checked that the color array was the same length as the scatter x and y arrays and had an error message like it does when the x and y arrays are different lengths.

Which python interpreter is ipdb using?

I am asking because I encounter something very strange. The following method throws an exception when called from within a script:
def gaussian_blur(self, in_array, size):
# expand in_array to fit edge of kernel
padded_array = np.pad(in_array, (10,), 'reflect')
# build kernel
x, y = np.mgrid[-size:size + 1, -size:size + 1]
g = np.exp(-(x**2 / float(size) + y**2 / float(size)))
g = (g / g.sum()).astype(in_array.dtype)
# do the Gaussian blur
return fftconvolve(padded_array, g, mode='valid')
Traceback (most recent call last):
File "raster_tools/smooth.py", line 130, in <module>
blurred = ri.gaussian_blur(my_array, 10)
File "raster_tools/smooth.py", line 80, in gaussian_blur
padded_array = np.pad(in_array, (10,), 'reflect')
File "/home/.virtualenvs/geo/local/lib/python2.7/site-packages/numpy/lib/arraypad.py", line 1358, in pad
kwargs)
File "/home/.virtualenvs/geo/local/lib/python2.7/site-packages/numpy/lib/shape_base.py", line 91, in apply_along_axis
res = func1d(arr[tuple(i.tolist())], *args, **kwargs)
TypeError: 'unicode' object is not callable
When I set a breakpoint using ipdb and then call
padded_array = np.pad(in_array, (10,), 'reflect')
(and all other lines of codes in the function as well by the way) the error won't occur. As the python script is being called within an virtual environment I could imagine that ipdb is using a different interpreter. Even though running the code outside the virtual env gives the same behavior.

Scipy Minimize uses a NoneType

I'm trying to code a multiple linear regression. Here's the line of code where my program raises an error:
least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sum(resid(xmat, ylist, coeff))}), method = 'BFGS') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
xmat is a list of vectors: [[3,5,2],[3,1,6],[7,2,3], [9,-2,0]]. ylist is a list of the same length as xmat: [5,2,7,7]. coeff is the coefficient list, initially [mean(ylist), 0, 0, 0] ([constant, b_0, b_1, b_2]). resid is the list of residuals for each point, and residsq is the N2 norm of the residuals (sqrt of sum of squares).
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
import linregtest
File "C:\Python33\lib\site-packages\linregtest.py", line 4, in <module>
out = linreg.multilinreg(xmat, ylist, True)
File "C:\Python33\lib\site-packages\linreg.py", line 120, in multilinreg
least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sum(resid(xmat, ylist, coeff))}), method = 'BFGS') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
File "C:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 302, in minimize
RuntimeWarning)
File "C:\Python33\lib\idlelib\PyShell.py", line 60, in idle_showwarning
file.write(warnings.formatwarning(message, category, filename,
AttributeError: 'NoneType' object has no attribute 'write'
Where does file come from, and how do I suppress this error?
EDIT: Solve one problem, find another. Maybe you can help me determine where SciPy is calling a float?
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
import linregtest
File "C:\Python33\lib\site-packages\linregtest.py", line 4, in <module>
out = linreg.multilinreg(xmat, ylist, True)
File "C:\Python33\lib\site-packages\linreg.py", line 123, in multilinreg
least = optimize.minimize(residsq(xmat, ylist, coeff), coeff, constraints = ({'type': 'eq', 'fun': sumresid(xmat, ylist, coeff)}), method = 'SLSQP') # Choose the coefficients that minimize the sum of the residuals squared subject to keeping the sum of the residuals equal to 0.
File "C:\Python33\lib\site-packages\scipy\optimize\_minimize.py", line 364, in minimize
constraints, **options)
File "C:\Python33\lib\site-packages\scipy\optimize\slsqp.py", line 301, in _minimize_slsqp
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
File "C:\Python33\lib\site-packages\scipy\optimize\slsqp.py", line 301, in <listcomp>
meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
TypeError: 'float' object is not callable
I just edited my python 3.2 IDLE, PyShell.py (fixing lines 59 and 62)
def idle_showwarning(message, category, filename, lineno,
file=None, line=None):
if file is None:
file = sys.stderr #warning_stream
try:
file.write(warnings.formatwarning(message, category, filename,
lineno, line=line))
use sys.stderr instead of the global warning_stream which uses sys.__stderr__. sys.__stderr__ is None in my case. I don't know why a global is used.
the call to warnings.formatwarning had an extra invalid file keyword.
Now, I get the warning printed, for example
>>> import numpy as np
>>> np.uint(1) - np.uint(2)
Warning (from warnings module):
File "C:\Programs\Python32\Lib\idlelib\idle.pyw", line 1
try:
RuntimeWarning: overflow encountered in ulong_scalars
>>> 4294967295
>>>
edit:
searching for python bug reports
http://bugs.python.org/issue12438 wrong file argument has been fixed
http://bugs.python.org/issue13582 problems with sys.__stderr__ is None is open

Categories