Reverse Mode Auto Differentiation is Kind of Like a Lens

Warning: I’m using sketchy uncompiled Haskell pseudocode.

Auto-differentiation is writing a function that also computes the derivative alongside calculating its value. Function composition is done alongside applying the chain rule to the derivative part.

One way to do this is to use a “dual number”. Functions now take a tuple of values and derivatives.

The Jacobean of a function from R^n \rightarrow R^m is a m by n matrix. The chain rule basically says that you need to compose the matrices via multiplication when you compose the value functions.  This is the composition of the linear maps.

Conceptually, you initialize the process with a NxN identity matrix corresponding to the fact that $latex \partial x_i/\partial x_j=\delta_{ij}

Vectorized versions of scalar functions (maps) will often use diag

A couple points:

  1.  Since the Jacobean j is always going to be multiplied in composition, it makes sense to factor this out into a Monad structure (Applicative maybe? Not sure we need full Monad power).
  2. There is an alternative to using explicit Matrix data types for linear maps. We could instead represent the jacobeans using (Vector Double) -> Vector Double. The downside of this is that you can’t inspect elements. You need explicit matrices as far as I know to do Gaussian elimination and QR decomposition. You can sample the function to reconstitute the matrix if need be, but this is somewhat roundabout. On the other hand, if your only objective is to multiply matrices, one can use very efficient versions. Instead of an explicit dense NxN identity matrix, you can use the function id :: a -> a, which only does some minimal pointer manipulation or is optimized away. I think that since we are largely multiplying Jacobeans, this is fine.

 

What we’ve shown so far is Forward Mode.

When you multiply matrices you are free to associate them in any direction you like. (D(C(BA))) is the association we’re using right now. But you are free to left associate them. ((DC)B)A). You can write this is right associated form using the transpose ((DC)B)A)^T = (A^T(B^T(C^TD^T)))

This form is reverse mode auto differentiation. Its advantage is the number of computations you have to do and the intermediate values you have to hold. If one is going from many variables to a small result, this is preferable.

It is actually exactly the same in implementation except you reverse the order of composition of the derivatives. We forward compose value functions and reverse compose derivative functions (matrices).

drawing-2

We have CPSed our derivative matrices.

Really a better typed version would not unify all the objects into a. While we’ve chosen to use Vector Double as our type, if we could tell the difference between R^n and R^m at the type level the following would make more sense.

However, this will no longer be a monad. Instead you’ll have to specify a Category instance. The way I got down to this stuff is via reading Conal Elliott’s new Automatic Differentiation paper which heavily uses the category interface.  I was trying to remove the need to use constrained categories (it is possible, but I was bogged down in type errors) and make it mesh nice with hmatrix. Let me also mention that using the Arrow style operators *** and dup and &&& and fst, and clever currying that he mentions also seems quite nice here. The Tuple structure is nice for expressing direct sum spaces in matrices. (Vector a, Vector b) is the direct sum of those vector spaces.

Anyway, the arrows for RD are

This is a form I’ve seen before though. It is a lens. Lens’ have a getter (a -> b) that extracts b from a and a setter (a -> b -> a) that given an a and a new b returns the replaced a.

Is an automatic derivative function in some sense extracting an implicit calculable value from the original vector and returning in a sense how to change the original function? It is unclear whether one should take the lens analogy far or not.

The type of Lens’  (forall f. Functor f => (b -> f b) -> a -> f a) means that it is isomorphic to a type like DFun’. The type itself does imply the lens laws of setters and getters, so these functions are definitely not proper lawful lenses. It is just curious that conceptually they are not that far off.

The lens trick of replacing this function with a quantified rank 1 type (forall f. ) or quantified rank-2 (forall p.) profunctor trick seems applicable here. We can then compose reverse mode functions using the ordinary (.) operator and abuse convenience functions from the lens library.

Neat if true.

 

 

CartPole WORKIN’ BOYEEE

We have been fighting a problem for weeks. The Serial port was just not reliable, it had sporadic. The problem ended up being a surprising thing, we were using threading to receive the messages nd checking for limit switches. It is not entirely clear why but this was totally screwing up the serial port update in an unpredictable manner. Yikes. What a disaster.

After that though smoooooooth sailing.

With a slight adaptation of the previous Openai gym LQR cartpole code and a little fiddling with parameters we have a VERY stable balancer. We removed the back reaction of the pole dynamics on the cart itself for simplicity. This should be accurate when the pole vastly.

We did find that the motor is exactly velocity control in steady state with a linear response. There is a zero point offset (you need to ask for 100 out of 2046 before you get any movement at all).

We’ll see where we can get with the Lyapunov control next time.

 

https://github.com/philzook58/cart_pole/commit/ca4aabb86eca54c5a9c7884ad25fafdec4d545ce

https://github.com/philzook58/cart_pole/blob/master/lqr_controller.py

 

 

Some random links on uses of Convex relaxations, in particular Semidefinite programming

 

Problems involving outer products of vector variables can be relaxed into semidefinite programs. That’s a general trick. Then the low rank bit from SVD is an approixmate solution for the vector

 

https://en.wikipedia.org/wiki/Matrix_completion

 

convex relaxation for distributed optimal control

http://ieeexplore.ieee.org/document/7464306/

 

 

graph matching in relation to Image correspondence

Permutation matrices have sum of rows and columns must be 1 constraint, is one relaxation.

quickMatch. Actually, not convex programming but was the root of the chain of references I ‘m digging through

http://openaccess.thecvf.com/content_ICCV_2017/papers/Tron_Fast_Multi-Image_Matching_ICCV_2017_paper.pdf

https://dl.acm.org/citation.cfm?id=2600314

matchALS

www.cis.upenn.edu/~kostas/mypub.dir/xiaowei15iccv.pdf

https://arxiv.org/abs/1402.1473

https://vision.in.tum.de/research/convex_relaxation_methods

 

Finding MaxCut approximation of a graph is a classic one

 

 

Quantum Semidefinite programming course

Density matrices have a semidefinite constrina (non negative probabilities)

https://cs.uwaterloo.ca/~watrous/CS867.Winter2017/

 

 

Sum of Squares is a semidefinite program that can guarantee that lyapunov functions actually work

 

 

Cart Pole Trajectory optimization using Cvxpy

I’ve been watching Russ Tedrake’s underactuated robotics and been trying some stuff out. The Drake package probably does this stuff better. Also IpOpt and Snopt are the libraries that get mentioned when people want to do this stuff for real.

It’s  too slow to be practical. That is mostly the fault of the cvxpy overhead. The ECOS solver itself says that it solves each iteration in about 0.02 seconds on my macbook.

The idea is to use linearized dynamics as constraints. Then iteratively ask cvxpy to solve for us. Until hopefully it converges. This is Sequential Convex Programming https://web.stanford.edu/class/ee364b/lectures/seq_slides.pdf

I used the discretization of the equations of motion using the mehotd described here https://epubs.siam.org/doi/pdf/10.1137/16M1062569

It is possible I did it right.

The Hermite Collocation for the trajectory and trapezoidal for the control

If we used just an absolute value cost, this all would be a linear program. Something to consider. Also CVXOPT would probably be faster.

It hurts me that the constraint and cost matrices are banded and could be solved so quickly. But the next level up in programming complexity takes a lot more work it seems to me.

 

This is the result

Green is cart acceleration. You can see it slamming into the maximum acceleration constraint

Blue is pole angle and orange is angular velocity. So it does a pretty good job. For some settings it is just awful though.

figure_1

Making a Van Der Graaf Generator

Will has been juicin’ to make a Van der Graaf generator. Similar to the Stirling engines, we followed youtube instructions to make it out of trash (soda bottles and soda cans and tape) and failed miserably.

We were mostly following this video https://www.youtube.com/watch?v=vfvjGeOGoBU

Yesterday, we took the gloves off and went to home depot. We used a trash fan motor which is awesome. 3d Printed some pieces for mounting the motor and for mounting the roller on the shaft with a press fit.

https://cad.onshape.com/documents/5016468595aba2e0189feaa4/w/5325496b7d68ecef269a238a/e/3cc572cbcee673b79b85caba 

Big ole 4″ pvc is the stalk. A flange to mount to the base which is a nice circle wood from the nice wood section of . Used two ikea bowls as the globe. I think it is important to have the upper roller somewhat deeply inside the globe?

We’re using pantyhose as a belt. Sort of the waist area stretched out to be a loop. It is just rubbing on the fixed top pvc roller. Maybe wrapping that in teflon tape would make it even better? The pantyhose is pure nylon. Nylon and pvc are towards opposite ends of the triboelectric series, which is important for the thing to work, but teflon is even farther down.

We used some brushes from home depot as electrical brushes. Might be overkill.

We wrapped the lower roller in more pantyhose to make a bulge. Counterintuitively, this bulge is supposed to keep the belt centered?

We were getting pretty wimpy sparks until we installed to lower brush and properly grounded it. I guess that is really important. Now they are pretty beefy maybe a couple centimeters. You can definitely see them and they sting a bit.

All in all a success!

img_0231 img_5799 img_4759 img_6389

 

LQR with CVXPY

Holy crap this was easy.

And I was able to easily add a constraint on the available force. Hot damn. It is a ridiculously tiny problem I guess, but still pretty damn cool. 0.002 second runtime.

 

STM32F411 Discovery Board Getting started

Bought one of these discovery boards for 15$ from digikey. I like the built in stuff, like the 9axis mems device. I don’t enjoy wiring up little boards particularly and it makes every project so ephemeral.

http://www.st.com/content/st_com/en/products/evaluation-tools/product-evaluation-tools/mcu-eval-tools/stm32-mcu-eval-tools/stm32-mcu-discovery-kits/32f411ediscovery.html

I am concerned that I should have gotten the older board.

User manual has pin connectors

http://www.st.com/content/ccc/resource/technical/document/user_manual/e9/d2/00/5e/15/46/44/0e/DM00148985.pdf/files/DM00148985.pdf/jcr:content/translations/en.DM00148985.pdf

It’s in platform.io as

disco_f411ve

Huh it only supports STM32Cube? I kind of wanted to try libOpenCM3

start a project with

platformio init –board disco_f411ve

platformio run

The examples are invaluable

https://github.com/platformio/platform-ststm32/tree/develop/examples

ok. There is a blink for the Hardware abstraction layer (HAL) and Low level (LL)

Hmm. Neither blink examples work off the bat. SYS_CLOCK is undefined for Low level

and board not supported in the HAL.

 

 

Alright let’s try libopencm3

https://github.com/libopencm3/libopencm3-examples

make in top directory first

follow directions to init submodule

alirght I guess I need to download the arm toolchain

https://developer.arm.com/open-source/gnu-toolchain/gnu-rm/downloads

I put them on my path

export PATH=~/Downloads/gcc-arm-none-eabi-7-2017-q4-major/bin:$PATH

Then ran make

also need to apt install gawk

editted the Makefile of miniblink

https://github.com/libopencm3/libopencm3/blob/master/ld/devices.data

stm32f411re stm32f4 ROM=512K RAM=128K

that’s the right config even though re is wrong

Okay was getting weird error

sudo make flash V=1

the V=1 gives verbose

I may have had to apt install openocd?

Need to run under sudo. Go fig.

Alright got some blinking! WOOO

Ah I see. PD12 is PortD12. Makes sense.

 

Trying Platform.io

PlatformIO and Visual Studio Take over the World

http://platformio.org/

Somehow I was not aware of this thing. It is a build tool for microcontrollers

Seems like people basically like it. 1000+ stars on github

python -m pip install -U platformio

 

make a folder

platformio init –board icestick

Holy crap. Is this thing going to download and setup the tools? THAT. IS. AWESOME. If it works.

Better yet clone this bad boy

https://github.com/platformio/platformio-examples

https://github.com/platformio/platformio-examples/tree/develop/lattice_ice40/lattice_ice40-leds

go to the blink folder.

platformio run

platformio run –target upload

Holy. Hell. It worked. THAT IS NUTS.

The commands it ran to compile

Hmm. I’m puzzled. Where did this come from? How did it know counter.v?

 

Mecrisp has an icestick version. Intriguing (Mecrisp is a forth implementation)

 

 

https://github.com/platformio/platform-lattice_ice40/tree/develop/examples/leds

had to sudo apt install libreadline6 and gtkwave to run simulation

I had to follow these instructions to get the FTDI device to work

https://stackoverflow.com/questions/36633819/iceprog-cant-find-ice-ftdi-usb-device-linux-permission-issue

and change the platformio.ini file to say icestick instead of icezum. Actually i don’t think that is necessary.

 

 

 

Cart Pole using Lyapunov and LQR control, OpenAI gym

We’re having a lot of trouble hacking together a reinforcement learning version of this, so we are taking an alternative approacg, inspired by wtaching the MIT underactuated robotics course.

http://underactuated.csail.mit.edu/underactuated.html?chapter=acrobot

It took some pen and paper to get the equations of motion (which are maybe right?).

openai gym has

We switch over to LQR when the y position of the pole is above a certain height

https://en.wikipedia.org/wiki/Linear%E2%80%93quadratic_regulator

This scipy function solves the algebriac ricatti equation in the ocntinous time infite horizon section

https://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.linalg.solve_continuous_are.html

Things that helped: Trying to balance pole first from upright position then from downright.

Tuning weights for theta and thetadot. Thetadot was too small made it unstable

Hacked in the LQR control by adjusting force_mag variable. Nasty.

 

Put it some slight compensation for a delayed observation, which reflects our actual sensor system

 

 

 

 

 

Linksprite CNC machine

I’m impressed with the package in the box. Well organanized.

Putting it together took maybe 4 hours, half watching The Wire.

The x-axis screw is not fitting. I’m hoping the end is just ground incorrectly.

Nope. This has been a huge pain. They sent me the wrong screw. There are 8mm 4mm and 2mm pitch T8 rods. They have 1 2 and 4 starts to their threading. I have bought the maximal number of incorrect rods. I now have in posession the 4mm pitch rod I need. Figuring this out has set me back a month and another 30$. I am annoyed.

There is a crack in the z-axis printed part. Epoxy should fix it.

open up arduino serial monitor

115200 baud both NL & CR

 

Hmm. LinuxCNC is more complicated than I thought. It seems installing it on my regular ubuntu 14.04 is not an option without a lot of dangerous futzing. Uses a real-time special kernel.

http://linuxcnc.org/docs/devel/html/code/building-linuxcnc.html

whenever it didn’t work i apt-get installed whatever was missing.

I also had to add a non redistributable when that error came up.

make

 

 

 

Two Control Guys. One in python one in java

https://github.com/vlachoudis/bCNC

https://github.com/winder/Universal-G-Code-Sender

 

Pycam and Blendercam do 3d models. Pycam appears to be defunct

http://pycam.sourceforge.net/

go to folder and make.

The website seems wrong. The links aren’t.

Build error

https://sourceforge.net/p/pycam/mailman/pycam-devel/

 

 

http://blendercam.blogspot.com/

 

Pcb

https://github.com/pcb2gcode/pcb2gcode

 

Inkscape gcodetools.

https://github.com/cnc-club/gcodetools#gcodetools

An Intro to G-code and How to Generate It Using Inkscape

 

Openscam (now called camotics?) is a path simulator.

 

http://jscut.org/

 

S1000 sets the spindle speed

M3 turns spindle on

M5 turns off

even 0.2 depth is a little deep

home. reset

turn on the spindle