機器學習 - 感知機



感知機是最古老和最簡單的神經網路架構之一。它由弗蘭克·羅森布拉特於 20 世紀 50 年代發明。感知機演算法是一種線性分類器,可將輸入分類為兩個可能的輸出類別之一。它是一種監督學習型別,透過提供標記的訓練資料來訓練模型。感知機演算法基於閾值函式,該函式取輸入的加權和並應用閾值以生成二進位制輸出。

感知機的架構

單層感知機由輸入層、權重層和輸出層組成。輸入層中的每個節點都連線到權重層中的每個節點,每個連線都分配了一個權重。權重層中的每個節點計算輸入的加權和,並應用閾值函式來生成輸出。

感知機中的閾值函式是海維賽德階躍函式,如果輸入大於或等於零,則返回二進位制值 1,否則返回 0。權重層中每個節點的輸出由以下公式確定:

$$y=\left\{\begin{matrix} 1; & if\: w_{0}+w_{1}x_{1}+w_{2}x_{2}+\cdot \cdot \cdot +w_{n}x_{n}\: > = 0 \\ 0; & otherwise \\ \end{matrix}\right.$$

其中“y”是輸出,x1、x2、...、xn是輸入特徵;w0、w1、w2、...、wn是相應的權重,>= 0 表示海維賽德階躍函式。

感知機的訓練

感知機演算法的訓練過程涉及迭代更新權重,直到模型收斂到一組權重,這些權重可以正確地對所有訓練示例進行分類。最初,權重設定為隨機值。對於每個訓練示例,將預測輸出與實際輸出進行比較,並相應地更新權重以最小化誤差。

感知機中的權重更新規則如下:

$$w_{i}=w_{i}+\alpha \times \left ( y-y' \right )\times x_{i}$$

其中Wi是第i個特徵的權重,$\alpha$是學習率,y是實際輸出,y′是預測輸出,xi是第i個輸入特徵。

使用Python實現感知機

使用scikit-learn庫在Python中實現了感知機演算法。scikit-learn庫提供了一個Perceptron類,可用於二元分類問題。

以下是在Python中使用scikit-learn實現感知機演算法的示例:

示例

from sklearn.linear_model import Perceptron
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

# Load the iris dataset
iris = load_iris()

# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.3, random_state=0)

# Create a Perceptron object with a learning rate of 0.1
perceptron = Perceptron(alpha=0.1)

# Train the Perceptron on the training data
perceptron.fit(X_train, y_train)

# Use the trained Perceptron to make predictions on the testing data
y_pred = perceptron.predict(X_test)

# Evaluate the accuracy of the Perceptron
accuracy = accuracy_score(y_test, y_pred)
print("Accuracy:", accuracy)

輸出

執行此程式碼時,將生成以下輸出:

Accuracy: 0.8

訓練感知機後,可將其用於對新輸入資料進行預測。給定一組輸入值,感知機計算輸入的加權和,並將啟用函式應用於該和以獲得輸出值。然後可以將此輸出值解釋為對應輸入的預測。

階躍函式在感知機訓練中的作用

感知機中使用的啟用函式可能會有所不同,但常見的選擇是階躍函式。如果輸入為正,則階躍函式返回 1;如果輸入為負或零,則返回 0。此函式很有用,因為它提供二進位制輸出,可以將其解釋為二元分類問題的預測。

以下是在Python中使用階躍函式作為啟用函式實現感知機的示例:

import numpy as np

class Perceptron:
   def __init__(self, learning_rate=0.1, epochs=100):
      self.learning_rate = learning_rate
      self.epochs = epochs
      self.weights = None
      self.bias = None

   def step_function(self, x):
      return np.where(x >= 0, 1, 0)

   def fit(self, X, y):
      n_samples, n_features = X.shape

      # initialize weights and bias to 0
      self.weights = np.zeros(n_features)
      self.bias = 0

      # iterate over epochs and update weights and bias
      for _ in range(self.epochs):
         for i in range(n_samples):
            linear_output = np.dot(self.weights, X[i]) + self.bias
            y_pred = self.step_function(linear_output)

            # update weights and bias based on error
            update = self.learning_rate * (y[i] - y_pred)
            self.weights += update * X[i]
            self.bias += update
   
   def predict(self, X):
      linear_output = np.dot(X, self.weights) + self.bias
      y_pred = self.step_function(linear_output)
      return y_pred

在此實現中,Perceptron類採用兩個引數:learning_rate和epochs。fit方法使用輸入資料X和相應的目標值y訓練感知機。predict方法採用輸入資料陣列並返回預測的輸出值。

要使用此實現,我們可以建立一個Perceptron類的例項並呼叫fit方法來訓練模型:

X = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y = np.array([0, 0, 0, 1])

perceptron = Perceptron(learning_rate=0.1, epochs=10)
perceptron.fit(X, y)

訓練模型後,我們可以使用predict方法對新輸入資料進行預測:

test_data = np.array([[1, 1], [0, 1]])
predictions = perceptron.predict(test_data)
print(predictions)

此程式碼的輸出為[1, 0],它們是輸入資料[[1, 1], [0, 1]]的預測值。

廣告