ニューラルネットワークにおけるパーセプトロンアルゴリズムと活性化関数

        パーセプトロンは英語でパーセプトロンと呼ばれます. ニューラルネットワークでは、パーセプトロン機能はANDゲート、NOTゲート、ORゲートを実現できますが、XORゲートは実現できません. XORゲートの実現には、以下に示す他の3つのゲート回路の助けが必要です。

import numpy as np


def AND(x1, x2):
    w1, w2, theta = 0.5, 0.5, 0.7
    tmp = w1 * x1 + w2 * x2
    if tmp <= theta:
        return 0
    elif tmp > theta:
        return 1


def AND2(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1


def NAND(x1, x2):
    x = np.array([x1, x2])
    w = np.array([-0.5, -0.5])
    b = 0.7
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1


def OR(x1, x2):
    x = np.array([x1, x2])
    w = np.array([0.5, 0.5])
    b = -0.2
    tmp = np.sum(w * x) + b
    if tmp <= 0:
        return 0
    else:
        return 1


def XOR(x1, x2):
    s1 = NAND(x1, x2)
    s2 = OR(x1, x2)
    return AND(s1, s2)


if __name__ == "__main__":
    print('AND(1,1)', AND(1, 1))
    print('AND(1,0)', AND(1, 0))
    print('AND(0,1)', AND(0, 1))
    print('AND(0,0)', AND(0, 0))
    print('---------')
    print('AND2(1,1)', AND2(1, 1))
    print('AND2(1,0)', AND2(1, 0))
    print('AND2(0,1)', AND2(0, 1))
    print('AND2(0,0)', AND2(0, 0))
    print('----------')
    print('NAND(1,1)', NAND(1, 1))
    print('NAND(1,0)', NAND(1, 0))
    print('NAND(0,1)', NAND(0, 1))
    print('NAND(0,0)', NAND(0, 0))
    print('----------')
    print('OR(1,1)', OR(1, 1))
    print('OR(1,0)', OR(1, 0))
    print('OR(0,1)', OR(0, 1))
    print('OR(0,0)', OR(0, 0))
    print('----------')
    print('XOR(1,1)', XOR(1, 1))
    print('XOR(1,0)', XOR(1, 0))
    print('XOR(0,1)', XOR(0, 1))
    print('XOR(0,0)', XOR(0, 0))

    アルゴリズムの結果は次のとおりです。

AND(1,1) 1
AND(1,0) 0
AND(0,1) 0
AND(0,0) 0
---------
AND2(1,1) 1
AND2(1,0) 0
AND2(0,1) 0
AND2(0,0) 0
----------
NAND(1,1) 0
NAND(1,0) 1
NAND(0,1) 1
NAND(0,0) 1
----------
OR(1,1) 1
OR(1,0) 1
OR(0,1) 1
OR(0,0) 0
----------
XOR(1,1) 0
XOR(1,0) 1
XOR(0,1) 1
XOR(0,0) 0

    パーセプトロンが活性化関数を持たない場合、それは線形重ね合わせ器です。ネットワークが何層であっても、最終結果は線形になります。活性化関数を使用すると、結果は非線形になる可能性があり、ターゲットとターゲット間の関係は、変数は曲線にすることができます。

   一般的に使用される活性化関数は次のとおりです: sigmoid、tanh、relu、softmax。以下では、pytorch を使用してこれらの活性化関数の特性を示します。

import torch
import torch.nn.functional as F
from torch.autograd import Variable
import matplotlib.pyplot as plt

x = torch.linspace(-5, 5, 200)
x = Variable(x)
x_np = x.data.numpy()

y_relu = torch.relu(x).data.numpy()
y_sigmoid = torch.sigmoid(x).data.numpy()
y_tanh = torch.tanh(x).data.numpy()
y_softplus = F.softplus(x).data.numpy()

plt.figure(1, figsize=(8, 6))
plt.subplot(221)
plt.plot(x_np, y_relu, c='red', label='relu')
plt.ylim((-1, 5))
plt.legend(loc='best')

plt.subplot(222)
plt.plot(x_np, y_sigmoid, c='red', label='sigmoid')
plt.ylim((-0.2,1.2))
plt.legend(loc='best')

plt.subplot(223)
plt.plot(x_np, y_tanh, c='red', label='tanh')
plt.ylim((-1.2, 1.2))
plt.legend(loc='best')

plt.subplot(224)
plt.plot(x_np, y_softplus, c='red', label='softplus')
plt.ylim((-0.2, 6))
plt.legend(loc='upper left')

plt.show()

    プログラムを実行すると、次のようなグラフが表示されます。

 

    ニューラル ネットワークで最も一般的に使用されるのは relu 活性化関数で、正式名は Rectified Linear Units で、関数式は y = max(0,x) です。x<0 の場合、値 0 がそのまま採用されます。 

おすすめ

転載: blog.csdn.net/feinifi/article/details/128927170