2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

paiza.ioでelixirAdvent Calendar 2022

Day 15

paiza.ioでelixir その15

Last updated at Posted at 2022-10-27

概要

paiza.ioでelixirやってみた。
ニューラルネットワークやってみた。
xorを推定してみた。

準備

tensorflow.jsでxorを学習して、ウェイトとバイアスを取り出した。
活性化関数は、tanhとsigmoid

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

真理値表 xor

in in out
0 0 0
0 1 1
1 0 1
1 1 0

サンプルコード

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 = [-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
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.sigmoid([y])
    y = Enum.at(y, 0)
    IO.puts "#{i} #{j} #{y}"
end




実行結果

0 0 0.009060141614711496
0 1 0.9836318400047009
1 0 0.9831985157008993
1 1 0.015668832466682565

成果物

以上。

2
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?