wslのtensorflowでkerasのbackend その10

# 概要
wslのtensorflowでkerasのbackendを試す。
練習問題、やってみた。

# 練習問題
kerasのbackendでxorを学習したウェイトとバイアスを量子化せよ。

# 方針
- 量子化を用いて、変数を浮動小数点から、-128から127のint8に変更する。
- 活性化関数、tanhとsigmoidをint8のテーブルに変更する。
- 量子化は、以下の式でやる。
weight * x + bias
具体的には、以下を選択した。
10 * x + 0

# サンプルコード

```
defmodule Nn do
	def tanh(v) do
		Enum.map(v, fn i -> 
		    a = :math.exp(i / 10)
            b = :math.exp(-1 * i / 10)
            floor((a - b) / (a + b) * 10)
		end)
	end
	def sigmoid(a) do
		Enum.map(a, fn i -> 
		    floor((1 / (1 + :math.exp(-1 * i / 10))) * 10)
		end)
	end
end

for i <- -125..126 do
    j = Nn.tanh([i])
    j = Enum.at(j, 0)
    IO.write "#{j}, "
end
IO.puts ""
for i <- -125..126 do
    k = Nn.sigmoid([i])
    k = Enum.at(k, 0)
    IO.write "#{k}, "
end

w10 = [-3.4681022, 0.379121, -1.4711456, 2.2247136, 2.3135107, 2.7624693, -1.83576, -1.1545312]
w11 = [2.1978209, 0.2895245, 2.0881913, -1.3450832, -1.4154571, 1.9255028, 3.2867477, 1.956522]
b1 = [-0.6591252, 0.3864409, 0.4982838, 0.3707547, 0.3982254, -0.3612466, 0.4284867, 0.2701645]
w2 = [1.4324994, 0.7911373, -2.3011343, -1.7614117, -2.4442003, 3.9701838, -1.837263, -1.3641325]
b2 = 0.9285715

Enum.map(w10, fn x ->
    floor(10 * x)
end)
|> IO.inspect
Enum.map(w11, fn x ->
    floor(10 * x)
end)
|> IO.inspect
Enum.map(b1, fn x ->
    floor(10 * x)
end)
|> IO.inspect
Enum.map(w2, fn x ->
    floor(10 * x)
end)
|> IO.inspect
floor(10 * b2)
|> IO.inspect


```

# 実行結果

```
-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -9, -9, -9, -9, -8, -8, -7, -7, -6, -5, -4, -3, -2, -1, 0, 0, 1, 2, 3, 4, 5, 6, 6, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3,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