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でelixirやってみた。
ニューラルネットワークで、xor推定したけど、浮動小数点使わないで、int8でやる。

サンプルコード

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/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


w10 = [-35, 3, -15, 22, 23, 27, -19, -12]
w11 = [21, 2, 20, -14, -15, 19, 32, 19]
b1 = [-7, 3, 4, 3, 3, -4, 4, 2]
w2 = [14, 7, -24, -18, -25, 39, -19, -14]
b2 = 9
for i <- 0..1, 
    j <- 0..1 do
    h = Nn.mul(w10, i)
    #|> IO.inspect
    l = Nn.mul(w11, j)
    #|> IO.inspect
    h = Nn.add(h, l)
    #|> IO.inspect
    h = Nn.add(h, b1)
    #|> IO.inspect
    h = Nn.tanh(h)
    #|> IO.inspect
    y = Nn.sum(h, w2)
    #|> IO.inspect
    y = y + b2
    #|> IO.inspect
    y = Nn.sigmoid([y])
    #|> IO.inspect
    y = Enum.at(y, 0)
    IO.puts "#{i} #{j} #{y}"
end




実行結果

0 0 0
0 1 10
1 0 10
1 1 0

成果物

以上。

2
0
0

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?