深度學習——神經網絡

目錄

線性界線:

N維界線:

感知器:

​感知器算法實現:


線性界線:

w_{1}x_{1} + w_{2}x_{2} + b = 0 ———— Wx + b =0W = (w_{1}, w_{2}), x = (x_{1}, x_{2})),W:權重,b:偏差

y = label: 0 or 1

predection:

\hat{y} = \left\{\begin{matrix} 1 & Wx + b \geqslant 0 \\ 0 & Wx + b < 0 \end{matrix}\right.

N維界線:

n維:x_{1}, x_{2}, \cdots , x_{n}

n - 1維超平面的方程:

w_{1}x_{1}, w_{2}x_{2}, \cdots , w_{n}x_{n} + b = 0

predection:

\hat{y} = \left\{\begin{matrix} 1 & Wx + b \geqslant 0 \\ 0 & Wx + b < 0 \end{matrix}\right.

感知器:

1、它是神經網絡的基礎構成組件。

2、

3、整個數據集中的每一個點都會把分類的結果提供給感知器(分類函數),並調整感知器。——這就是計算機在神經網絡算法中,找尋最優感知器的原理。

4、


感知器算法實現:

import numpy as np
# Setting the random seed, feel free to change it and see different solutions.
np.random.seed(42)

def stepFunction(t):
    if t >= 0:
        return 1
    return 0

def prediction(X, W, b):
    return stepFunction((np.matmul(X,W)+b)[0])

# TODO: Fill in the code below to implement the perceptron trick.
# The function should receive as inputs the data X, the labels y,
# the weights W (as an array), and the bias b,
# update the weights and bias W, b, according to the perceptron algorithm,
# and return W and b.
def perceptronStep(X, y, W, b, learn_rate = 0.01):
    # Fill in code
    for i in range(len(X)):
        y_hat = prediction(X[i], W, b)
        if y[i] - y_hat == 1:   #分類爲負,標籤爲正
            W[0] += X[i][0] * learn_rate
            W[1] += X[i][1] * learn_rate
            b += learn_rate
        if y[i] - y_hat == -1:
            W[0] -= X[i][0] * learn_rate
            W[1] -= X[i][1] * learn_rate
            b -= learn_rate
    return W, b
    
# This function runs the perceptron algorithm repeatedly on the dataset,
# and returns a few of the boundary lines obtained in the iterations,
# for plotting purposes.
# Feel free to play with the learning rate and the num_epochs,
# and see your results plotted below.
def trainPerceptronAlgorithm(X, y, learn_rate = 0.01, num_epochs = 25):
    x_min, x_max = min(X.T[0]), max(X.T[0])
    y_min, y_max = min(X.T[1]), max(X.T[1])
    W = np.array(np.random.rand(2,1))
    b = np.random.rand(1)[0] + x_max
    # These are the solution lines that get plotted below.
    boundary_lines = []
    for i in range(num_epochs):
        # In each epoch, we apply the perceptron step.
        W, b = perceptronStep(X, y, W, b, learn_rate)
        boundary_lines.append((-W[0]/W[1], -b/W[1]))
    return boundary_lines

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章