Code Examples: (Code is experimental, for use-at-own-risk, of course. )

Some Code examples: Python, Tcl/Tk, and when I figure out how to put APL characters here, I will post some APL hacks...). 

[Update: APL-hack..  just imaged some of the sAPL page-frames, to show terse, useful sAPL code (reads in a text file, processes it into 32-bit floats, plus some binary-ascii conversion examples.  I can read the Xerion network weights file into sAPL, and use tanh and dot-product multiply to activate (ie. "run") the Xerion-trained neural network.  See images for sAPL code below]

I do have a Github account, but only with a copy of sAPL at this time.  More to come later. When I get something useful running in TensorFlow, I will post some examples there, and here probably also.

First piece is the code to do the "raindrops" laplace PDE (partial differential equations) simulation, using Python 2.7 and TensorFlow, Numpy, Scipy and PIL (actually Pillow), on a MacBook. The code is from Google TensorFlow tutorials.   The original code uses Python Notebooks, Jupyter, and some other Python extentions, so the simulation can be seen to evolve in real-time - which is of course really cool.  But it is also nice to get something actually running, just to checkpoint one's progress.   This code below will produce the five images shown in the image to the right, and will confirm that you have the baseline configuration needed to get TensorFlow running on your Mac OSX.  I used Python 2.7.11, because I had it there already.  The MacBook OSX version is Yosemite (OSX 10.10.5).  TensorFlow needs Python 2.7.x, at a minimum. (The MacBook came with Python 2.6 installed, which needed to be upgraded.)

 

    # --- the Tensorflow LaPlace Image example (Uses PIL, and scipy.misc)
    #
    # --- Modified: Mar 7, 2017 - by MCL, to just use image file display
    # ---                                       instead of Python Notebooks, IPython, etc.,
    # ---                                       with negative damping and darker image backgrd.
    # ---                                       (Instead of ripples in a pond, we have
    # ---                                       exploding stars ... )
    # --- Produces Initial image, 3 intermediated images, and the final image
    #     as .jpg files. Requires only: tensorflow, numpy, scipy and Pillow
    #     and Python 2.7.10.
    # --- This example taken from Tensorflow Site:
    # ---                                     https://www.tensorflow.org/tutorials/pdes
    # --- and provides a nifty example of manipulating n-dimensional tensors.
    # ---
    # --- For Python newbies (me!):   1) invoke Python in terminal shell
    # ---                             2) >>> execfile("LaplaceMCL.py")
    # --- focus on understanding exactly how Tensorflow is reshaping tensors
    # ------------------------------------------------------------------------------------------
    # --- Import libraries for simulation
    import tensorflow as tf
    import numpy as np
    import scipy.misc

    # --- Import for visualization (works now - jpeg encoder recognized
    # import PIL.Image
    from PIL import Image, ImageDraw
    from io import BytesIO
    # xxx from IPython.display import clear_output, Image, display

    # --- function for displaying state of the pond's surface as an image
    def DisplayArray(a, fmt='jpeg', rng=[0,1]):
       """Display an array as a picture. """
       a = (a - rng[0])/float(rng[1] - rng[0])*37
       a = np.uint8(np.clip(a, 0, 255))
       f = BytesIO()
       # PIL.Image.fromarray(a).save(f,fmt)
       # clear_output(wait = True)
       # display(Image(data=f.getvalue()))
       # --- write the image
       scipy.misc.imsave("tensor.jpg", a)
       image = Image.open("tensor.jpg")
       image.show()

    # --- make interactive session for messing around - can use regular session also
    sess = tf.InteractiveSession()
    # sess = tf.Session()

    # --- computational functions go here...
    def make_kernel(a):
       """Transform a 2D array into a convolutional kernel """
       a = np.asarray(a)
       a = a.reshape(list(a.shape) + [1,1])
       return tf.constant(a, dtype=1)

    def simple_conv(x, k):
       """ A simplified 2D convolutional operation """
       x = tf.expand_dims(tf.expand_dims(x, 0), -1)
       y = tf.nn.depthwise_conv2d(x, k, [1, 1, 1, 1], padding='SAME')
       return y[0, :, :, 0]

    def laplace(x):
       """Compute the 2D laplacian of an array """
       laplace_k = make_kernel([[0.5, 1.0, 0.5],
                                           [1.0, -6., 1.0],
                                           [0.5, 1.0, 0.5]])
       return simple_conv(x, laplace_k)

    # --- Define the PDE - the pond is a perfect 500x500 square
    N = 500

    # --- now, we create some raindrops
    # --- Initial Conditions -- some rain drops hit the pond
    # --- set everything to zero
    u_init = np.zeros([N, N], dtype=np.float32)
    ut_init = np.zeros([N, N], dtype=np.float32)

    # Some raindrops hit pond at random points (or stars in a starfield)
    for n in range(40):
       a,b = np.random.randint(0, N, 2)
       u_init[a,b] = np.random.uniform()

    # --- Create and Display the jpeg image...
    DisplayArray(u_init, rng=[-0.1, 0.1])

    # Parameters
    # eps -- time resolution
    # damping -- wave damping
    eps = tf.placeholder(tf.float32, shape=())
    damping = tf.placeholder(tf.float32, shape=())

    # --- Create vaiables for simulation state
    U = tf.Variable(u_init)
    Ut = tf.Variable(u_init)

    # --- Discretized PDE update rules
    U_ = U + eps * Ut
    Ut_ = Ut + eps * (laplace(U) - damping * Ut)

    # --- Operation to update the state
    step = tf.group(
       U.assign(U_),
       Ut.assign(Ut_))

    # --- Run the simulation forward with a simple FOR loop.
    # --- Initialize sate to initial conditions
    tf.global_variables_initializer().run()

    # --- Run 1700 steps of PDE
    for i in range(1700):
       # Step simulation (damping was 0.04, I made it negative .14)
       step.run({eps: 0.03, damping: -0.14})
       if (i == 400) : DisplayArray(U.eval(), rng=[-0.1, 0.1])
       if (i == 1000) : DisplayArray(U.eval(), rng=[-0.1, 0.1])
       if (i == 1400) : DisplayArray(U.eval(), rng=[-0.1, 0.1])

    # --- Ok, we are done...
    DisplayArray(U.eval(), rng=[-0.1, 0.1])

    # --- show the jpeg. Not needed if using DisplayArray.
    # image = Image.open("tensor.jpg")
    # image.show()


 

Back to Trebuchet font. Questions to gemesyscanada at gmail dot com, if curious.  

This is sAPL function to read a raw ascii text file of Xerion weights into sAPL as a vector of chars.

This procwt function takes as parameter the raw weights char. vector, and processes it into high-precision 32-bit reals for calculaton.

Utility functions to create pointer vector, raise negatives signs, and execute hyperbolic tangent activation transfer function.

Some examples of translate functions, overlaid on image of our homeworld, imaged from the International Space Station. The binary table bottom-right, is courtesy of xkcd site. I learned APL first, and I tend to use it to pseudo-code a problem. But once pseudo-coded, it can usually just be transcribed into a runable program. When machines were slow, performance was an issue. Now, performance (execution-speed) is not an issue, typically. It is the human-performance that is causing issues now (pretty much as it always has been, throughout all of history.)

The Actnet example to Activate the Xor2 Network.

Actnet processes Wtab1, Wtab2 and Btab. Wtabs are network weights, and Btab are bias values. This screencap shows the result, which matches exactly the Xor2 net activated using Xerion. Note the Xor2 net uses logistic transfer function (code provided previous image).