.NET 開発基盤部会 Wiki」は、「Open棟梁Project」,「OSSコンソーシアム .NET開発基盤部会」によって運営されています。

目次

概要

ここでは正確には、人口ニューロン、単純パーセプトロンについて説明する。

動作原理

(x1) --------- w1 --------> (y)
                             ↑
(x2) --------- w2 -----------┘
 入力信号  固有の重み     出力信号

-θ= bはバイアスと呼ばれ、w, θ同様に、「重み」と呼ぶことがある。

活性化関数

単純な論理回路をパーセプトロンで表現する。

機械学習では下のパラメタ(w1, w2, θ(-b))を決める作業をコンピュータに行わせる。

ANDゲート

下記のANDゲートを満たすパーセプトロン(w1, w2, θ(-b))を作る。

x1x2y
000
100
010
111

プロット

 │
 0  ①
 │
 │
─0──0─────
 │

NANDゲート

下記のNANDゲートを満たすパーセプトロン(w1, w2, θ(-b))を作る。

x1x2y
001
101
011
110

(ANDゲートのパラメタの符号を反転すればOK)

プロット

 │
 ①  0
 │
 │
─①──①─────
 │

ORゲート

下記のORゲートを満たすパーセプトロン(w1, w2, θ(-b))を作る。

x1x2y
000
101
011
111

プロット

 │
 ①  ①
 │
 │
─0──①─────
 │

XORゲート

下記のXORゲートを満たす1層のパーセプトロンを作ることはできない。

x1x2y
000
101
011
110

プロット

理由は、以下の縦軸x1、横軸x2のプロットは、
非線形領域のため、閾値の直線を引けないため。

 │
 ①  0
 │
 │
─0──①─────
 │

多層のパーセプトロン

しかし、前述のANDNADNORのゲートを
組みわせた多層のパーセプトロンであればXORゲートを作ることができる。

例えば、

を使用すると、

第0層第1層第2層
x1x2s1s2y
00100
10111
01111
11010

と多層(2層)パーセプトロンとしてXORゲートを作ることができる。

(x1) ──NAND──→ (s1)─AND┐
  └── OR ───↑┐       ↓
                 ││       (y)
  ┌───NAND──┘↓       ↑
(x2) ── OR ──→ (s2)─AND┘

パーセプトロンを実装する。

簡単な実装

ここから、VSCodeでPythonファイルに書いた。

ANDゲート

"""This is a test program."""
def and_method(x_1, x_2):
    """This is a test program."""
    w_1, w_2, theta = 0.5, 0.5, 0.7
    tmp = x_1 * w_1 + x_2 * w_2
    if tmp <= theta:
        return 0
    else:
        return 1

print(and_method(0, 0))
print(and_method(1, 0))
print(and_method(0, 1))
print(and_method(1, 1))

NANDゲート

w, θを以下のようにする。

w_1, w_2, theta = -0.5, -0.5, -0.7

ORゲート

w, θを以下のようにする。

w_1, w_2, theta = 0.5, 0.5, 0.3

バイアスを用いた実装

上記バイアスとベクトルを用いて書き直す。

ANDゲート

"""This is a test program."""

import numpy as np

def and_method(x_1, x_2):
    """This is a test program."""
    x = np.array([x_1, x_2])
    w = np.array([0.5, 0.5])
    b = -0.7
    tmp = b + np.sum(x*w)
    if tmp <= 0:
        return 0
    else:
        return 1

print(and_method(0, 0))
print(and_method(1, 0))
print(and_method(0, 1))
print(and_method(1, 1))

NANDゲート

w, bを以下のようにする。

w = np.array([-0.5, -0.5])
b = 0.7

ORゲート

w, bを以下のようにする。

w = np.array([0.5, 0.5])
b = -0.3

XORゲート

多層パーセプトロン(MLP)として以下のように実装できる。

"""This is a test program."""

import numpy as np

def and_method(x_1, x_2):
    """This is a test program."""
    x = np.array([x_1, x_2])
    w = np.array([0.5, 0.5])
    b = -0.7
    tmp = b + np.sum(x*w)
    if tmp <= 0:
        return 0
    else:
        return 1

def nand_method(x_1, x_2):
    """This is a test program."""
    x = np.array([x_1, x_2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    tmp = b + np.sum(x*w)
    if tmp <= 0:
        return 0
    else:
        return 1

def or_method(x_1, x_2):
    """This is a test program."""
    x = np.array([x_1, x_2])
    w = np.array([0.5, 0.5])
    b = -0.3
    tmp = b + np.sum(x*w)
    if tmp <= 0:
        return 0
    else:
        return 1

def xor_method(x_1, x_2):
    """This is a test program."""
    s_1 = nand_method(x_1, x_2)
    s_2 = or_method(x_1, x_2)
    return and_method(s_1, s_2)

print(xor_method(0, 0))
print(xor_method(1, 0))
print(xor_method(0, 1))
print(xor_method(1, 1))

参考


トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS