概要
wslのtensorflowでkerasのbackendを試す。
練習問題、やってみた。
練習問題
kerasのbackendでxorを学習したウェイトとバイアスを使って、elixirで再現せよ。
サンプルコード
defmodule Nn do
def mul(a, b) do
Enum.map(a, fn i ->
i * b
end)
end
def add(a, b) do
for x <- 0..7 do
Enum.at(a, x) + Enum.at(b, x)
end
end
def sum(a, b) do
for x <- 0..7 do
Enum.at(a, x) * Enum.at(b, x)
end
|> Enum.sum
end
def tanh(v) do
Enum.map(v, fn i ->
a = :math.exp(i)
b = :math.exp(-1 * i)
(a - b) / (a + b)
end)
end
def sigmoid(a) do
Enum.map(a, fn i ->
(1 / (1 + :math.exp(-1 * i)))
end)
end
end
w10 = [ 1.6427742, 1.292738, 0.13679211, 0.09276549, 0.31930393, 0.18661255, 1.0932094, -0.7801719 ]
w11 = [ 1.4525393, 1.1745324, 0.20021456, 0.497589, 0.11109181, 0.33417672, -0.44360426, -1.1322144 ]
b1 = [-0.20589589, 0.09548836, 0.54812616, 0.60974604, 0.15801114, 0.30214155, 0.38613093, 1.4007448 ]
w2 = [ 1.6341141, 1.1821091, -0.24378781, -0.2940346, -0.22311434, -0.34191525, -0.79992425, 1.717806 ]
b2 = -0.5830822
for i <- 0..1, j <- 0..1 do
h = Nn.mul(w10, i)
l = Nn.mul(w11, j)
h = Nn.add(h, l)
h = Nn.add(h, b1)
h = Nn.tanh(h)
y = Nn.sum(h, w2)
y = y + b2
y = Nn.tanh([y])
y = Enum.at(y, 0)
IO.puts "#{i} #{j} #{y}"
end
実行結果
0 0 0.007566404874442216
0 1 0.9309703232706648
1 0 0.9168728513830984
1 1 0.011421405940028202
成果物
以上。