0
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でpython その3

Posted at

概要

paiza.ioでpythonやってみた。
練習問題やってみた。

練習問題

ニューラルネットワークでxorを回帰せよ。

方針

  • sigmoid使う。
  • tanh使う。

サンプルコード

# coding: utf-8
import math

def tanh(x):
   a = math.exp(x)
   b = math.exp(-x)
   return (a - b) / (a + b)

def sigmoid(x):
   return 1.0 / (1.0 + math.exp(-x))

def main():
   w1 = [[-3.4681022, 0.379121, -1.4711456, 2.2247136, 2.3135107, 2.7624693, -1.83576, -1.1545312], [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 in range(4):
      x = [0.0, 0.0]
      y = [0.0]
      h = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
      if (i == 0):
         x = [1.0, 0.0]
         print("1, 0 => ")
      if (i == 1):
         x = [0.0, 1.0]
         print("0, 1 => ")
      if (i == 2):
         x = [1.0, 1.0]
         print("1, 1 => ")
      if (i == 3):
         x = [0.0, 0.0]
         print("0, 0 => ")
      for k in range(2):
         for l in range(8):
            h[l] += x[k] * w1[k][l]
      for l in range(8):
         h[l] += b1[l]
         h[l] = tanh(h[l])
      for k in range(8):
         for l in range(1):
            y[l] += h[k] * w2[k][l]
      for k in range(1):
         y[k] += b2[k]
         y[k] = sigmoid(y[k])
      print(y[0])

main()




実行結果

1, 0 => 
0.9831985157008993
0, 1 => 
0.9836318400047009
1, 1 => 
0.015668832466682565
0, 0 => 
0.009060141614711496

成果物

以上。

0
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
0
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?