Python 中的單神經元神經網路


神經網路是深度學習非常重要的核心;它在許多不同領域都有許多實際應用。如今,這些網路被用於影像分類、語音識別、目標檢測等。

讓我們來了解一下它是什麼以及它是如何工作的?

這個網路有不同的元件。它們如下所示 -

  • 輸入層,x
  • 任意數量的隱藏層
  • 輸出層,ŷ
  • 每層之間的一組權重和偏差,由 W 和 b 定義
  • 接下來是為每個隱藏層選擇啟用函式,σ。

在此圖中,展示了 2 層神經網路(在計算神經網路中的層數時,通常不包括輸入層)

Neuron Neural Network

在此圖中,圓圈代表神經元,線條代表突觸。突觸用於將輸入和權重相乘。我們將權重視為神經元之間連線的“強度”。權重定義了神經網路的輸出。

以下是簡單的前饋神經網路工作原理的簡要概述 -

當我們使用前饋神經網路時,我們必須遵循一些步驟。

  • 首先將輸入作為矩陣(數字的二維陣列)

  • 接下來是將輸入乘以一組權重。

  • 接下來應用啟用函式。

  • 返回輸出。

  • 接下來計算誤差,它是資料期望輸出與預測輸出之間的差值。

  • 並且權重會根據誤差稍作調整。

  • 為了訓練,這個過程會重複 1,000+ 次,並且訓練的資料越多,我們的輸出就會越準確。

學習時間,睡眠時間(輸入)測試分數(輸出)

2, 992
1, 586
3, 689
4, 8?

示例程式碼

from numpy import exp, array, random, dot, tanh
class my_network():
   def __init__(self):
      random.seed(1)
      # 3x1 Weight matrix
      self.weight_matrix = 2 * random.random((3, 1)) - 1
      defmy_tanh(self, x):
      return tanh(x)
      defmy_tanh_derivative(self, x):
   return 1.0 - tanh(x) ** 2
      # forward propagation
      defmy_forward_propagation(self, inputs):
   return self.my_tanh(dot(inputs, self.weight_matrix))
      # training the neural network.
      deftrain(self, train_inputs, train_outputs,
      num_train_iterations):
   for iteration in range(num_train_iterations):
      output = self.my_forward_propagation(train_inputs)
      # Calculate the error in the output.
      error = train_outputs - output
      adjustment = dot(train_inputs.T, error *self.my_tanh_derivative(output))
      # Adjust the weight matrix
      self.weight_matrix += adjustment
      # Driver Code
   if __name__ == "__main__":
      my_neural = my_network()
      print ('Random weights when training has started')
   print (my_neural.weight_matrix)
      train_inputs = array([[0, 0, 1], [1, 1, 1], [1, 0, 1], [0, 1, 1]])
      train_outputs = array([[0, 1, 1, 0]]).T
      my_neural.train(train_inputs, train_outputs, 10000)
   print ('Displaying new weights after training')
      print (my_neural.weight_matrix)
         # Test the neural network with a new situation.
         print ("Testing network on new examples ->")
print (my_neural.my_forward_propagation(array([1, 0, 0])))

輸出

Random weights when training has started
[[-0.16595599]
[ 0.44064899]
[-0.99977125]]
Displaying new weights after training
[[5.39428067]
[0.19482422]
[0.34317086]]
Testing network on new examples ->
[0.99995873]

更新於: 2020年6月26日

675 次檢視

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告