Multilayer perceptron with target variable as array instead of a single value - python

I am new to deep learning and I have been trying to use the theano library to train my data. MLP tutorial here has a scalar output value while my use case has an array with a 1 corresponding to the value depicted in the output.
For example (assume the possible scalar values are 0,1,2,3,4,5),
0 = [1,0,0,0,0,0]
1 = [0,1,0,0,0,0]
2 = [0,0,1,0,0,0]
I have only modified the code to read my input and output (output now is a 2 dimensional array or matrix in the parlance of theano). Other parts of the code is as is from the MLP tutorial pasted above.
The error I am getting is in the following function
test_model = theano.function(inputs=[index],
outputs=classifier.errors(y),
givens={
x: test_set_x[index * batch_size:(index + 1) * batch_size],
y: test_set_y[index * batch_size:(index + 1) * batch_size]}) //line 286
Error stack:
Traceback (most recent call last):
File "mlp.py", line 398, in <module>
test_mlp()
File "mlp.py", line 286, in test_mlp
y: test_set_y[index * batch_size:(index + 1) * batch_size]})
File "/usr/local/lib/python2.7/dist-packages/theano/compile/function.py", line 223, in function
profile=profile)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 490, in pfunc
no_default_updates=no_default_updates)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 241, in rebuild_collect_shared
cloned_v = clone_v_get_shared_updates(outputs, copy_inputs_over)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 92, in clone_v_get_shared_updates
clone_a(v.owner, copy_inputs_over)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 131, in clone_a
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 92, in clone_v_get_shared_updates
clone_a(v.owner, copy_inputs_over)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 131, in clone_a
clone_v_get_shared_updates(i, copy_inputs_over)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 92, in clone_v_get_shared_updates
clone_a(v.owner, copy_inputs_over)
File "/usr/local/lib/python2.7/dist-packages/theano/compile/pfunc.py", line 135, in clone_a
strict=rebuild_strict)
File "/usr/local/lib/python2.7/dist-packages/theano/gof/graph.py", line 213, in clone_with_new_inputs
new_inputs[i] = curr.type.filter_variable(new)
File "/usr/local/lib/python2.7/dist-packages/theano/tensor/type.py", line 205, in filter_variable
self=self)<br><br>
TypeError: Cannot convert Type TensorType(int64, matrix) (of Variable Subtensor{int64:int64:}.0) into Type TensorType(int32, vector). You can try to manually convert Subtensor{int64:int64:}.0 into a TensorType(int32, vector).
I would like to know how to change this theano.function to accommodate the y value as a matrix.

You need to define y as T.imatrix() instead of T.lvector().

Related

Basinhopping Causes Error with Statsmodels MNLogit

I tried running the code below to fit a multinomial Logit model using the basinhopping method, but it returns the following error:
import numpy as np
import statsmodels.api as sm
x = np.random.randint(0, 100, 1000)
y = np.random.randint(0, 3, 1000)
model = sm.MNLogit(y, sm.add_constant(x))
results = model.fit(method='basinhopping')
print(results.summary())
Traceback (most recent call last):
File "/Users/wagnerpf134/opt/anaconda3/lib/python3.9/site-packages/spyder_kernels/py3compat.py", line 356, in compat_exec
exec(code, globals, locals)
File "/Users/wagnerpf134/Documents/untitled0.py", line 7, in <module>
results = model.fit(method='basinhopping')
File "/Users/wagnerpf134/opt/anaconda3/lib/python3.9/site-packages/statsmodels/discrete/discrete_model.py", line 654, in fit
mnfit = base.LikelihoodModel.fit(self, start_params = start_params,
File "/Users/wagnerpf134/opt/anaconda3/lib/python3.9/site-packages/statsmodels/base/model.py", line 563, in fit
xopt, retvals, optim_settings = optimizer._fit(f, score, start_params,
File "/Users/wagnerpf134/opt/anaconda3/lib/python3.9/site-packages/statsmodels/base/optimizer.py", line 241, in _fit
xopt, retvals = func(objective, gradient, start_params, fargs, kwargs,
File "/Users/wagnerpf134/opt/anaconda3/lib/python3.9/site-packages/statsmodels/base/optimizer.py", line 1040, in _fit_basinhopping
retvals = optimize.basinhopping(f, start_params,
File "/Users/wagnerpf134/opt/anaconda3/lib/python3.9/site-packages/scipy/optimize/_basinhopping.py", line 728, in basinhopping
callback(bh.storage.minres.x, bh.storage.minres.fun, True)
TypeError: <lambda>() takes 1 positional argument but 3 were given
The error does not appear when I specify other optimization methods, and it does not appear when I use basinhopping with Statsmodel's Logit model on binary dependent variables or OrderedModel(distr='logit') on ordered categorical dependent variables, so I'm not really sure what's going wrong. Any help in resolving this error would be greatly appreciated.

Calculating exponential of a matrix in python

I want to calculate the exponential of a 200x200 matrix (expm(B)) and get the following problem. Thanks a lot for your help.
exp_matrix2 = expm(B)
File ".../python2.7/site-packages/scipy/linalg/matfuncs.py", line
261, in expm
return scipy.sparse.linalg.expm(A)
File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py",
line 582, in expm
return _expm(A, use_exact_onenorm='auto')
File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py",
line 618, in _expm
eta_1 = max(h.d4_loose, h.d6_loose)
File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py",
line 457, in d4_loose
structure=self.structure)**(1/4.)
File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py",
line 301, in _onenormest_matrix_power
MatrixPowerOperator(A, p, structure=structure))
File ".../python2.7/site-packages/scipy/sparse/linalg/_onenormest.py",
line 95, in onenormest
est, v, w, nmults, nresamples = _onenormest_core(A, A.H, t, itmax)
File "/python2.7/site-packages/scipy/sparse/linalg/_onenormest.py",
line 424, in _onenormest_core
Z = np.asarray(AT_linear_operator.matmat(S))
File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py",
line 326, in matmat
Y = self._matmat(X)
File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py",
line 468, in _matmat
return super(_CustomLinearOperator, self)._matmat(X)
File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py",
line 174, in _matmat
return np.hstack([self.matvec(col.reshape(-1,1)) for col in X.T])
File
"/home/dk2518/anaconda2/lib/python2.7/site-packages/scipy/sparse/linalg/interface.py",
line 219, in matvec
y = self._matvec(x)
File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py",
line 471, in _matvec
return self.__matvec_impl(x)
File ".../python2.7/site-packages/scipy/sparse/linalg/interface.py",
line 266, in rmatvec
y = self._rmatvec(x)
File ".../python2.7/site-packages/scipy/sparse/linalg/matfuncs.py",
line 203, in _rmatvec
x = A_T.dot(x)
ValueError: shapes (207,207) and (1,207) not aligned: 207 (dim 1) != 1
(dim 0)
As discussed in #TomNash's link, a large np.matrix is the problem.
ndarray and sparse matrix work fine:
In [309]: slg.expm(np.ones((200,200)));
In [310]: slg.expm(sparse.csc_matrix(np.ones((200,200))));
In [311]: slg.expm(np.matrix(np.ones((200,200))));
ValueError: shapes (200,200) and (1,200) not aligned: 200 (dim 1) != 1 (dim 0)
Not every np.matrix gives problems:
In [313]: slg.expm(np.matrix(np.eye(200)));
Turning the np.matrix back into ndarray works:
In [315]: slg.expm(np.matrix(np.ones((200,200))).A);
This uses slg.matfuncs._expm(A, use_exact_onenorm='auto')
which has an test, early one, for:
if use_exact_onenorm == "auto":
# Hardcode a matrix order threshold for exact vs. estimated one-norms.
use_exact_onenorm = A.shape[0] < 200
That explains, in part, why we get the problem with a (200,200) matrix, but not a (199,199).
This works:
slg.matfuncs._expm(M, use_exact_onenorm=True);
It fails with False. But from there I get lost in the details of how it sets up _ExpmPadeHelper and attempts a Pade order 3.
In short - avoid np.matrix, especially if (200,200) or larger.

Sympy issues with plotting a piecewise function

I have defined a SymPy piecewise function to compute the Federal Income tax for 2017. I know the function is working as I have tried various inputs and compared it to verified tax calculators and it gives the same result.
However, when trying to plot the SymPy function, I get the error:
TypeError: '>=' not supported between instances of 'complex' and 'int'
I never defined any complex numbers.
def getFedTax(alpha,p,GI):
# alpha is an array indicating the starting dollar amount of each tax bracket, not including 0
#p is an array of the Tax Percentage amount corresponding to the interval BEFORE each alpha, i.e. [0,alpha0) corresponds to p0, [alpha0, alpha1) corresponds to p1, etc.
#x is the Gross Income for computation
# create an array of the cumulative sums for each starting point in alpha
cumsums = [0,p[0]*(alpha[0]-0)]
cumnum = cumsums[1]
for i,num in enumerate(alpha[1:-1],start=1):
cumsums.append(p[i]*(alpha[i]-alpha[i-1]) + cumnum)
cumnum = cumnum + p[i]*(alpha[i]-alpha[i-1])
cumsums.append(p[-2]*(alpha[-1]-alpha[-2]) + cumnum)
#Create the argument list of tuples for the SymPy.Piecewise function
argtuples = []
for n,bracstart in enumerate(alpha):
if n == 0:
argtuples.append((cumsums[0] + p[0]*x, And(0<=x, x<alpha[0])))
elif 0 < n and n < len(alpha)-1:
argtuples.append((cumsums[n] + p[n]*(x - alpha[n-1]), And(alpha[n-1] <= x, x < alpha[n])))
else:
argtuples.append((cumsums[-1]+p[-1]*(x-alpha[-1]), x>alpha[-1]))
t = Piecewise(*argtuples)
return round(t.subs(x,GI),2), t
from sympy import Piecewise, And
from sympy.plotting.plot import plot
from sympy.abc import x
ti = getFedTax([9325.00,37950.00,91900.00,191650.00,416700.00,418400.00],
[0.10,0.15,0.25,0.28,.33,.35,.396],1000000)
plot(ti[1],(x,1.,450000.00))
Full traceback:
runfile('C:/Users/galileo/Downloads/trial.py',
wdir='C:/Users/galileo/Downloads')
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 710, in runfile
execfile(filename, namespace)
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\spyder\utils\site\sitecustomize.py", line 101, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "C:/Users/galileo/Downloads/trial.py", line 39, in <module>
plot(ti[1],(x,1.,450000.00))
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 1295, in plot
plots.show()
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 196, in show
self._backend.show()
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 1029, in show
self.process_series()
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 908, in process_series
collection = self.LineCollection(s.get_segments())
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\plot.py", line 514, in get_segments
f_start = f(self.start)
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\experimental_lambdify.py", line 231, in __call__
result = self.lambda_func(args)
File "c:\users\galileo\appdata\local\programs\python\python36\lib\site-
packages\sympy\plotting\experimental_lambdify.py", line 316, in __call__
return self.lambda_func(*args, **kwargs)
File "<string>", line 1, in <lambda>
TypeError: '>=' not supported between instances of 'complex' and 'int'
Plotting of piecewise linear was bugged, and seems to have been fixed in january 2018
Upgrading to SymPy 1.2 (from 1.1.1 which was shipped by default with Anaconda) did the trick for me.
from sympy import symbols
from sympy.plotting import plot
x = symbols('x')
from sympy import Piecewise, log
f = 2*x+3
g = x+1
p = Piecewise((-1, x < -1), (g, x <= 1), (f, True))
plot(p)

word2vec_basic.py - ValueError: array must not contain infs or NaNs

I got an error with running word2vec_basic.py on my mac both in PyCharm and Terminal('$ python word2vec_basic.py').
My mac version is as follows:
* platform: Darwin-15.6.0-x86_64-i386-64bit
* python: 3.6.0
* numpy: 1.12.1
* scipy: 0.19.0
* sklearn: 0.18.1
The error is as follows:
Traceback (most recent call last):
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2881, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-35-c6c4cd8ccf88>", line 8, in <module>
low_dim_embs = tsne.fit_transform(final_embeddings[:plot_only, :])
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/sklearn/manifold/t_sne.py", line 884, in fit_transform
embedding = self._fit(X)
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/sklearn/manifold/t_sne.py", line 789, in _fit
skip_num_points=skip_num_points)
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/sklearn/manifold/t_sne.py", line 847, in _tsne
**opt_args)
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/sklearn/manifold/t_sne.py", line 388, in _gradient_descent
grad_norm = linalg.norm(grad)
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/scipy/linalg/misc.py", line 129, in norm
a = np.asarray_chkfinite(a)
File "/Users/HYJ/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py", line 1215, in asarray_chkfinite
"array must not contain infs or NaNs")
ValueError: array must not contain infs or NaNs

Librosa feature tonnetz ends up in TypeError

I'm trying to extract tonnetz from the harmonic components of my audio. My code is basically a copy paste from the tutorial https://librosa.github.io/librosa/generated/librosa.feature.tonnetz.html
My code:
import librosa
def extract_feature(file_name):
y, sr = librosa.load(file_name)
y = librosa.effects.harmonic(y)
tonnetz = librosa.feature.tonnetz(y=y, sr=sr)
return tonnetz
print extract_feature("out.wav")
Here's the stack trace:
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/core/pitch.py:160: DeprecationWarning: object of type <type 'numpy.float64'> cannot be safely interpreted as an integer.
bins = np.linspace(-0.5, 0.5, np.ceil(1./resolution), endpoint=False)
Traceback (most recent call last):
File "test_python.py", line 10, in <module>
print extract_feature("out.wav")
File "test_python.py", line 6, in extract_feature
tonnetz = librosa.feature.tonnetz(y=y, sr=sr)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/feature/spectral.py", line 1157, in tonnetz
chroma = chroma_cqt(y=y, sr=sr)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/feature/spectral.py", line 936, in chroma_cqt
real=False))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/core/constantq.py", line 251, in cqt
cqt_resp.append(__cqt_response(my_y, n_fft, my_hop, fft_basis))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/core/constantq.py", line 531, in __cqt_response
D = stft(y, n_fft=n_fft, hop_length=hop_length, window=np.ones)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/core/spectrum.py", line 167, in stft
y_frames = util.frame(y, frame_length=n_fft, hop_length=hop_length)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/librosa/util/utils.py", line 102, in frame
strides=(y.itemsize, hop_length * y.itemsize))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/lib/stride_tricks.py", line 102, in as_strided
array = np.asarray(DummyArray(interface, base=x))
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/numpy/core/numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: 'float' object cannot be interpreted as an index
Any idea how to fix this?
I rolled back to numpy 1.10.1 to fix this issue (although I was running chroma_cqt and getting this error).
Numpy > 1.11 as_strided makes a dummyarray and asarray does not handle the float array properly. 1.10.1 numpy has better stride_tricks, apparently.

Categories

Resources