Make Ruby Differentiable
(Automatic Differentiation for Ruby)
@nagachika
2019-04-19 RubyKaigi 2019
Make Ruby Differentiable
This LT is homage for the episode #206 of Rebuild.fm
Make Ruby Differentiable
There are two LT talks for ‘Automatic Differentiation’!!
Swift for TensorFlow
https://github.com/tensorflow/swift/blob/master/docs/AutomaticDifferentiation.md
Automatic Differentiation
Berland at English Wikipedia [Public domain],

via Wikimedia Commons
Differentiation Gem
https://rubygems.org/gems/differentiation
https://github.com/nagachika/differentiation
differential def func(x, y)
(x + 1.0) * (y - 2.0)
end
result = func(1.0, 2.0)
result
=> 0.0
result.gradients
=> { x: 0.0, y: 2.0 }
result.gradients(:x, :y)
=> [ 0.0, 2.0 ]
Make method differentiable
l = ->(x, y){ (x + 1.0) * (y - 2.0) }
differentiable_l = differential l
result = differentiable_l.call(1.0, 2.0)
result
=> 0.0
result.gradients
=> { x: 0.0, y: 2.0 }
result.gradients(:x, :y)
=> [ 0.0, 2.0 ]
Make Proc differentiable
x = 1.0.to_dual_number
y = 2.0.to_dual_number
result = (x + 1.0) * (y - 2.0)
result
=> 0.0
result.gradients(x, y)
=> [ 0.0, 2.0 ]
Make Numeric Differentiable
mat = Matrix[
[ 0.0, 1.0],
[ 2.0, 3.0]
]
differential def matmul_reduce(m)
(m * m).sum
end
result = matmul_reduce(mat)
=> 22.0
result.gradients(:m)
=> [Matrix[[3.0, 7.0], [5.0, 9.0]]]
Make Matrix Differentiable
class DualNumber
…
def coerce(other)
if Differentiation.differentiable?(other)
[ Differentiation.convert_to_dual_number(other), self]
else
super
end
end
end
Numeric#coerce
Differentiable DEMO
Multiple Layered Perceptron solving XOR
x1 x2 y
0 0 0
0 1 1
1 0 1
1 1 0
x1
x2
y
Differentiable DEMO
Multiple Layered Perceptron solving XOR

with Matrix and differentiation.gem
Let’s Make Ruby Differentiable

Make Ruby Differentiable