sympy - meets swift (for tensorflow)

280 views
Skip to first unread message

John Pope

unread,
Sep 14, 2018, 11:17:36 PM9/14/18
to sympy
Hello, my name is John Pope. This is me - https://github.com/johndpope
I've been dabbling in machine learning - while my day job is coding mostly in swift / ios / osx.

Most recently there's been some developments by google to bring python 2 packages to swift for tensorflow.
You need a specific tensorflow swift toolchain - and there's a bunch of gotchas while team works toward shipping a useable product.

Some one has successfully coded up a gym ai (which does work on slightly older toochain - (Swift for TensorFlow 2018-09-05)

There's also some code using sklearn with swift


There doesn't seem to be anything close to sympy in swift for symbolic mathematical capabilities.
Reading into sympy I'm very impressed -  and understand it helped theano as a higher level library.


I'm wanting to leverage the differentiation / lambdify from python with swift. This would unlock calculus capabilities like this repo. 

Specifically - I'm interested in crafting neural nets.  I found a simple neural net mnist here < 126 lines using sympy- 

at the heart - there's a single sympy.lambdify function - unfortunately - this blows up with swift. 
here's the function

```python

t = sympy.Symbol("t")
y = sympy.Symbol("y")

sigmoid = 1/(1+sympy.exp(-y))
cross_entropy = y * sympy.ln(t)+(1 - y) * sympy.ln(1 - t)
cost =  (y - t) ** 2 / 2
cost_f = sympy.lambdify((y, t), cost, "numpy")
cost_p = sympy.diff(cost, y)
cost_p_f = sympy.lambdify((y, t), cost_p, "numpy")

```


I crafted this xcode project here (it needs swift for tensorflow toolchain)



this is the error. 

Thread 1: Fatal error: 'try!' expression unexpectedly raised an error: Python exception: 'NoneType' object has no attribute 'f_locals'

Screen Shot 2018-09-14 at 10.43.10 PM.png


I'm stumped. If anyone has any interest - I'd be very grateful for another set of eyes. 



John Pope

 



John Pope

unread,
Sep 15, 2018, 11:59:18 AM9/15/18
to sympy
great news - the antlr4-python2-runtime with sympy works out of the box.


  let sympy = Python.import("sympy")

        let latex = Python.import("sympy.parsing.latex")

        let expr = latex.parse_latex("x = \\frac{-b \\pm \\sqrt{b^2-4ac}}{2a}")

        print("expr:",expr)


po expr

Eq(x, (-b*pm*sqrt(-4*a*c + b**2))/((2*a)))
 

Screen Shot 2018-09-15 at 11.53.43 AM.png



pip install antlr4-python2-runtime

Aaron Meurer

unread,
Sep 19, 2018, 9:48:34 PM9/19/18
to sy...@googlegroups.com
This looks like a bug in the Swift Python library. It probably has a hard time with the dynamically generated function from lambdify.

Is this using the latest version of SymPy (we just released 1.3 last week)? 

Aaron Meurer

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+unsubscribe@googlegroups.com.
To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/82ec7a16-49ec-4e66-ae68-c9c57318f5a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

John Pope

unread,
Sep 19, 2018, 10:20:13 PM9/19/18
to sy...@googlegroups.com
Hi Aaron,

thanks for follow up. 

I got it to execute  with a python lambda function via lambdastr /( but not the other function).

I've made some inroads to simply load an entire python class without it being a module.
eg.

let rotationMatrix = Python.import("rotation_matrix") // load rotation_matrix.py file let myoutput = rotationMatrix.CoordRotationInv( [1.5,4.3], [5,5], Double.pi/4 ) // call python function directly.


this line - will execute lambda function when called. 
but in truth - I couldn't craft a simple correct signature.
Got complaints about missing methods for iterating lists....

my current head space is racing to get torch working within swift. 
(which is not working out)  let torch  = Python.import("torch")
If I have bandwidth - I think it would be interesting to circle back and run some quantum methods via swift.
shouldn't take a few lines of code to leverage sympy internals - would be quite ground breaking for swift community to tap into these libraries.



let λ = Python.eval(strTest,["builtins":"None"])

let a = sympy.DeferredVector("a")
let s = sympy.DeferredVector("x")
let t = sympy.Symbol("t")
let dist2 = sympy.Pow(a[0]+s[0]*t,2) + sympy.Pow(a[1]+s[1]*t,2) + sympy.Pow(a[2]+s[2]*t,2)
let ddist2_dt = sympy.diff(dist2,t)
let funcArray = sympy.solvers.solve(ddist2_dt, t)
print("func:",funcArray)
let strTest = lambdify.lambdastr([a,s],funcArray[0])
print("λ str:",strTest)//lambdastr: lambda a,x: (-(a[0]*x[0] + a[1]*x[1] + a[2]*x[2])/(x[0]**2 + x[1]**2 + x[2]**2))
let λ = Python.eval(strTest,["builtins":"None"])
print("λ fn:",λ)
// let xArray = np.array(1)
// let aArray = np.array(1,1)
//
// let e0 = Python.map(λ,xArray,aArray)
// print("λ(1,2):",λ(xv,av)

To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
To view this discussion on the web visit https://groups.google.com/d/msgid/sympy/82ec7a16-49ec-4e66-ae68-c9c57318f5a1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "sympy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sympy+un...@googlegroups.com.

To post to this group, send email to sy...@googlegroups.com.
Visit this group at https://groups.google.com/group/sympy.
Reply all
Reply to author
Forward
0 new messages