SymPy ComplexInfinity - sympy

I have defined a piecewise function using sympy and took the integral using that piecewise function. After that the integral gives the following piecewise function
Piecewise((zoo*x, x <= 0), (log(x) + oo, True))
As I want to implement this function to sympy.lambdify() function ıt gives the error
KeyError: 'ComplexInfinity'
So my main purpose is to draw the graph of the first piecewise function but couldn't solve the error.
Also the following is my code and the full error message.
The Code:
> import sympy as sp
>
>from numpy import linspace
>
>import matplotlib.pyplot as pl
>
>
>x = sp.Symbol('x')
>
>
>def b(x):
> p = sp.Piecewise((0, x <= 0), (x, x > 0))
> return p
>
>
>load = sp.integrate(b(x)**-1,x)
>print(load)
>
>
>
>loadx = sp.lambdify(x, load, modules=["numpy"])
The Error Message:
runfile('C:/Users/Mkaansa/Desktop/Piecewise.py', wdir='C:/Users/Mkaansa/Desktop')
Piecewise((zoo*x, x <= 0), (log(x) + oo, True))
Traceback (most recent call last):
File "C:\Users\Mkaansa\Desktop\Piecewise.py", line 28, in <module>
loadx = sp.lambdify(x, load, modules=["numpy"])
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\utilities\lambdify.py", line 848, in lambdify
funcstr = funcprinter.doprint(funcname, args, expr)
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\utilities\lambdify.py", line 1091, in doprint
funcbody.append('return ({})'.format(self._exprrepr(expr)))
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\codeprinter.py", line 110, in doprint
lines = self._print(expr).splitlines()
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\printer.py", line 329, in _print
return getattr(self, printmethod)(expr, **kwargs)
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\numpy.py", line 152, in _print_Piecewise
exprs = '[{}]'.format(','.join(self._print(arg.expr) for arg in expr.args))
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\numpy.py", line 152, in <genexpr>
exprs = '[{}]'.format(','.join(self._print(arg.expr) for arg in expr.args))
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\printer.py", line 329, in _print
return getattr(self, printmethod)(expr, **kwargs)
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\codeprinter.py", line 490, in _print_Mul
a_str = [self.parenthesize(x, prec) for x in a]
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\codeprinter.py", line 490, in <listcomp>
a_str = [self.parenthesize(x, prec) for x in a]
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\str.py", line 35, in parenthesize
return self._print(item)
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\printer.py", line 329, in _print
return getattr(self, printmethod)(expr, **kwargs)
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\pycode.py", line 233, in _print_ComplexInfinity
return self._print_NaN(expr)
File "C:\Users\Mkaansa\anaconda3\lib\site-packages\sympy\printing\pycode.py", line 72, in _print_known_const
known = self.known_constants[expr.__class__.__name__]
KeyError: 'ComplexInfinity'

Related

How to create multiple Conv3DLSTMCell in tensorflow

I'm trying to create my model with several conv3d lstm cell layers:
I run the following code:
conv1, state1 = conv3d('conv1', _X, [8,112,112,1], [3,3,3], 64)
pool1 = max_pool('pool1', conv1, k=1)
conv2, state2 = conv3d('conv2', pool1, [8, 56, 56, 64], [3, 3, 3], 128)
pool2 = max_pool('pool2', conv2, k=2)
The conv3d functions:
def conv3d(myname, l_input, shape, kernel, outchan):
cell = contrib_rnn_cell.Conv3DLSTMCell(input_shape=shape, output_channels=out$
hidden = cell.zero_state(array_ops.shape(l_input)[0], dtypes.float32)
output, state = cell(l_input, hidden)
print(output.shape)
return output, state
My code runs OK for the conv1 and pool1 but for conv2 layer it shows me an error:
Traceback (most recent call last):
File "conv3dlstm.py", line 272, in <module>
run(16)
File "conv3dlstm.py", line 199, in run
biases)
File "/home/user/projects/model_conv3dlstm.py", line 47, in inference_c3d
conv2, state2 = conv3d('conv2', pool1, [8, 56, 56, 64], [3, 3, 3], 128)
File "/home/user/projects/model_conv3dlstm.py", line 32, in conv3d
output, state = cell(l_input, hidden)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 190, in __call__
return super(RNNCell, self).__call__(inputs, state)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/layers/base.py", line 696, in __call__
outputs = self.call(inputs, *args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2110, in call
4 * self._output_channels, self._use_bias)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2200, in _conv
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1297, in get_variable
constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 1093, in get_variable
constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 431, in get_variable
return custom_getter(**custom_getter_kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/rnn_cell_impl.py", line 193, in _rnn_get_variable
variable = getter(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 408, in _true_getter
use_resource=use_resource, constraint=constraint)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/variable_scope.py", line 747, in _get_single_variable
name, "".join(traceback.format_list(tb))))
ValueError: Variable conv_lstm_cell/kernel already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2200, in _conv
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
File "/usr/local/lib/python2.7/dist-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py", line 2110, in call
4 * self._output_channels, self._use_bias)
File "/home/user/projects/model_conv3dlstm.py", line 32, in conv3d
output, state = cell(l_input, hidden)
I saw the code in run_cell.py at line 2200 which is:
kernel = vs.get_variable(
"kernel", filter_size + [total_arg_size_depth, num_features], dtype=dtype)
Which is getting variable with fixed name "kernel". If I understand it correctly, it is supposed to be unique. But it means I can't create more of Conv3DLSTMCells than one? Is it a bug or am I using it incorrectly?

How to save a list to a text file?

I want to save all x ad y coordinates (center each pixel in a raster layer) as a list in a text file. First for test I write below code that it's correct:
import os
import pickle
mylist = [(12, 25), (65, 96), (10, 15)]
path = r"data/listfile"
file = 'file.txt'
if not os.path.exists(path):
os.makedirs(path)
with open(os.path.join(path, file), 'wb') as handle:
pickle.dump(mylist, handle)
with open(os.path.join(path, file), 'rb') as handle:
aa = pickle.loads(handle.read())
print aa
In next step I used this code in real for my raster layer. MCVE of that code is :
from qgis.core import *
from PyQt4 import *
import os
import pickle
ds = QgsRasterLayer("/LData/Pop/lorst.tif", "Raster")
pixelWidth = ds.rasterUnitsPerPixelX()
pixelHeight = ds.rasterUnitsPerPixelY()
originX, originY = (ext.xMinimum(), ext.yMinimum())
src_cols = ds.width()
src_rows = ds.height()
path = r"LData/Pop"
file = 'List.txt'
if not os.path.exists(path):
os.makedirs(path)
def pixel2coord(x, y):
xp = (pixelWidth * x) + originX + (pixelWidth / 2)
yp = (pixelHeight * y) + originY + (pixelHeight / 2)
return QgsPoint(xp, yp)
list =[]
for i in range(0, src_cols):
for j in range(0, src_rows):
rspnt = pixel2coord(i, j)
list.append(rspnt)
with open(os.path.join(path, file), 'wb') as handle:
pickle.dump(list, handle)
with open(os.path.join(path, file), 'rb') as handle:
lst = pickle.loads(handle.read())
But I received this error:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/tmp/tmp4rPKQ_.py", line 70, in <module>
pickle.dump(pntRstList, handle)
File "/usr/lib/python2.7/pickle.py", line 1376, in dump
Pickler(file, protocol).dump(obj)
File "/usr/lib/python2.7/pickle.py", line 224, in dump
self.save(obj)
File "/usr/lib/python2.7/pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "/usr/lib/python2.7/pickle.py", line 606, in save_list
self._batch_appends(iter(obj))
File "/usr/lib/python2.7/pickle.py", line 621, in _batch_appends
save(x)
File "/usr/lib/python2.7/pickle.py", line 306, in save
rv = reduce(self.proto)
File "/usr/lib/python2.7/copy_reg.py", line 71, in _reduce_ex
state = base(self)
TypeError: the sip.wrapper type cannot be instantiated or sub-classed
Is there any way to convert xy list to a text file and back read it in number format not str?
The easiest would be to forgo the use of QgsPoint(xp, yp) and use tuples instead, i.e. just (xp, yp). It seems that the QgsPoint is a SIP wrapper for a C++ class; and SIP wrappers wouldn't know about pickling.
Notice also that pyqgis documentation says this:
Note
The tuples (x,y) are not real tuples, they are QgsPoint objects, the values are accessible with x() and y() methods.
They just look like tuples but they're nothing like tuples, you cannot even access the individual coordinates with t[0].
That said, you can convert a list of such points into a list of tuples easily with
lst = [(p.x(), p.y()) for p in lst]
pickle.dump(lst, handle)

TypeError: 'NoneType' object is not subscriptable from call to integrate

Submitted issue https://github.com/sympy/sympy/issues/12993
Is this a bug? Why is this error generated?
>python
Python 3.6.1 |Anaconda custom (64-bit)| (default, May 11 2017, 13:09:58)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import *
>>> A,B,y=symbols('A B y')
>>> integrate(-(A**2+B**2*(-y**2+1))**(1/2)/(-y**2+1),y)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/integrals.py", line 1295, in integrate
risch=risch, manual=manual)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/integrals.py", line 486, in doit
conds=conds)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/integrals.py", line 919, in _eval_integral
result = manualintegrate(g, x)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1223, in manualintegrate
return _manualintegrate(integral_steps(f, var))
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1013, in integral_steps
fallback_rule)(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 212, in _alternatives
result = rule(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 33, in conditioned_rl
return rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 176, in _rewriter
substep = integral_steps(rewritten, symbol)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1013, in integral_steps
fallback_rule)(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 95, in switch_rl
return rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 335, in mul_rule
next_step = integral_steps(f, symbol)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 1013, in integral_steps
fallback_rule)(integral)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 85, in do_one_rl
result = rl(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/strategies/core.py", line 65, in null_safe_rl
result = rule(expr)
File "/home/me/anaconda3/lib/python3.6/site-packages/sympy/integrals/manualintegrate.py", line 743, in trig_substitution_rule
a = match[a]
TypeError: 'NoneType' object is not subscriptable
>>>
Interesting thing is that, if I issue the same command right again, the error do not show up
>>> integrate(-(A**2+B**2*(-y**2+1))**(1/2)/(-y**2+1),y)
Integral((A**2 - B**2*y**2 + B**2)**0.5/((y - 1)*(y + 1)), y)
It only shows up first time it is used! It looks like using it first time loads something to memory and hence next time the error do not show up.
Any idea what is going on?

Python Petl - _csv.Error: line contains NULL byte

I'm running this:
table = petl.fromcsv('file.txt', delimiter = ',' , encoding = 'utf-8)
petl.todb(table, connection, 'table')
And i am getting this error on insert step:
File "/usr/local/lib/python2.7/dist-packages/petl/util/base.py", line 28, in __len__
return sum(1 for _ in self)
File "/usr/local/lib/python2.7/dist-packages/petl/util/base.py", line 28, in <genexpr>
return sum(1 for _ in self)
File "/usr/local/lib/python2.7/dist-packages/petl/io/csv_py2.py", line 45, in __iter__
for row in reader:
File "/usr/local/lib/python2.7/dist-packages/petl/io/csv_py2.py", line 149, in next
row = self.reader.next()
_csv.Error: line contains NULL byte
What i can to do to solve this problem?

python - Error with Mariana/Theano neural network

I am facing a problem when I start my trainer and I can't figure out the cause.
My input data is of dimension 42 and my output should be one value out of 4.
This is the shape of my training and test set:
Training set:
input = (1152, 42) target = (1152,)
Training set: input = (1152, 42) target = (1152,)
Test set: input = (384, 42) target = (384,)
This is the construction of my network:
ls = MS.GradientDescent(lr=0.01)
cost = MC.CrossEntropy()
i = ML.Input(42, name='inp')
h = ML.Hidden(23, activation=MA.Sigmoid(), initializations=[MI.GlorotTanhInit()], name="hid")
o = ML.SoftmaxClassifier(4, learningScenario=ls, costObject=cost, name="out")
mlp = i > h > o
And this is the construction of the datasets, trainers and recorders:
trainData = MDM.RandomSeries(distances = train_set[0], next_state = train_set[1])
trainMaps = MDM.DatasetMapper()
trainMaps.mapInput(i, trainData.distances)
trainMaps.mapOutput(o, trainData.next_state)
testData = MDM.RandomSeries(distances = test_set[0], next_state = test_set[1])
testMaps = MDM.DatasetMapper()
testMaps.mapInput(i, testData.distances)
testMaps.mapOutput(o, testData.next_state)
earlyStop = MSTOP.GeometricEarlyStopping(testMaps, patience=100, patienceIncreaseFactor=1.1, significantImprovement=0.00001, outputFunction="score", outputLayer=o)
epochWall = MSTOP.EpochWall(1000)
trainer = MT.DefaultTrainer(
trainMaps=trainMaps,
testMaps=testMaps,
validationMaps=None,
stopCriteria=[earlyStop, epochWall],
testFunctionName="testAndAccuracy",
trainMiniBatchSize=MT.DefaultTrainer.ALL_SET,
saveIfMurdered=False
)
recorder = MREC.GGPlot2("MLP", whenToSave = [MREC.SaveMin("test", o.name, "score")], printRate=1, writeRate=1)
trainer.start("MLP", mlp, recorder = recorder)
But the following error is being produced:
Traceback (most recent call last):
File "nn-mariana.py", line 82, in <module>
trainer.start("MLP", mlp, recorder = recorder)
File "SUPRESSED/Mariana/Mariana/training/trainers.py", line 226, in start
Trainer_ABC.start( self, runName, model, recorder, trainingOrder, moreHyperParameters )
File "SUPRESSED/Mariana/Mariana/training/trainers.py", line 110, in start
return self.run(runName, model, recorder, *args, **kwargs)
File "SUPRESSED/Mariana/Mariana/training/trainers.py", line 410, in run
outputLayers
File "SUPRESSED/Mariana/Mariana/training/trainers.py", line 269, in _trainTest
res = modelFct(output, **kwargs)
File "SUPRESSED/Mariana/Mariana/network.py", line 47, in __call__
return self.callTheanoFct(outputLayer, **kwargs)
File "SUPRESSED/Mariana/Mariana/network.py", line 44, in callTheanoFct
return self.outputFcts[ol](**kwargs)
File "SUPRESSED/Mariana/Mariana/wrappers.py", line 110, in __call__
return self.run(**kwargs)
File "SUPRESSED/Mariana/Mariana/wrappers.py", line 102, in run
fres = iter(self.theano_fct(*self.fctInputs.values()))
File "SUPRESSED/Theano/theano/compile/function_module.py", line 871, in __call__
storage_map=getattr(self.fn, 'storage_map', None))
File "SUPRESSED/Theano/theano/gof/link.py", line 314, in raise_with_op
reraise(exc_type, exc_value, exc_trace)
File "SUPRESSED/Theano/theano/compile/function_module.py", line 859, in __call__
outputs = self.fn()
ValueError: Input dimension mis-match. (input[0].shape[1] = 1152, input[1].shape[1] = 4)
Apply node that caused the error: Elemwise{Composite{((i0 * i1) + (i2 * log(i3)))}}[(0, 1)](InplaceDimShuffle{x,0}.0, LogSoftmax.0, Elemwise{sub,no_inplace}.0, Elemwise{sub,no_inplace}.0)
Toposort index: 18
Inputs types: [TensorType(int32, row), TensorType(float64, matrix), TensorType(int32, row), TensorType(float64, matrix)]
Inputs shapes: [(1, 1152), (1152, 4), (1, 1152), (1152, 4)]
Inputs strides: [(4608, 4), (32, 8), (4608, 4), (32, 8)]
Inputs values: ['not shown', 'not shown', 'not shown', 'not shown']
Outputs clients: [[Sum{axis=[1], acc_dtype=float64}(Elemwise{Composite{((i0 * i1) + (i2 * log(i3)))}}[(0, 1)].0)]]
Versions:
Mariana (1.0.1rc1, /media/guilhermevrs/Data/Documentos/Academico/TCC-code/Mariana)
Theano (0.8.0.dev0, SUPRESSED/Theano)
This code was produced having as base the tutorial code from the mnist example.
Could you please help me to figure out what's going on?
Thank you in advance
I talked directly to the authors of Mariana and the cause and solution is explained in this issue