神經網路 線性單元

來源:互聯網
上載者:User

標籤:注意   linear   一個   來替   代碼   import   nbsp   技術分享   inpu   

本文結構:
  1. 什麼是線性單元
  2. 有什麼用
  3. 代碼實現
1. 什麼是線性單元

線性單元和感知器的區別就是在啟用函數:

感知器的f是階躍函數:

線性單元的啟用函數是線性:

所以線性模型的公式如下:

2. 有什麼用

感知器存在一個問題,就是遇到線性不可分的資料時,就可能無法收斂,所以要使用一個可導的線性函數來替代階躍函數,即線性單元,這樣就會收斂到一個最佳的近似上。

3. 代碼實現

1. 繼承Perception,初始化線性單元

class LinearUnit(Perception):    def __init__(self, input_num):        ‘‘‘初始化線性單元,設定輸入參數的個數‘‘‘        Perception.__init__(self, input_num, f)

  

2. 定義一個線性單元,調用 train_linear_unit 進行訓練

- 列印訓練獲得的權重

- 輸入參數值[3.4]測試一下預測值

if __name__ == ‘__main__‘:    ‘‘‘訓練線性單元‘‘‘    linear_unit = train_linear_unit()    # 列印訓練獲得的權重    print(linear_unit)    # 測試    print(‘Work 3.4 years, monthly salary = %.2f‘ % linear_unit.predict([3.4]))    print(‘Work 15 years, monthly salary = %.2f‘ % linear_unit.predict([15]))    print(‘Work 1.5 years, monthly salary = %.2f‘ % linear_unit.predict([1.5]))    print(‘Work 6.3 years, monthly salary = %.2f‘ % linear_unit.predict([6.3]))

  

  • 其中訓練的過程就是:
  • 獲得訓練資料
  • 設定迭代次數,學習速率等參數
  • 再返回訓練好的線性單元
def train_linear_unit():    ‘‘‘    使用資料訓練線性單元    ‘‘‘    # 建立感知器,輸入參數的特徵數為1(工作年限)    lu = LinearUnit(1)    # 訓練,迭代10輪,學習速率為0.01    input_vecs, labels = get_training_dataset()    lu.train(input_vecs, labels, 10, 0.01)    # 返回訓練好的線性單元    return lu

  

完整代碼

# -*-coding:utf-8-*-from NeuralNetwork.sensor_demo import Perception# 定義啟用函數ff = lambda x: xclass LinearUnit(Perception):    def __init__(self, input_num):        ‘‘‘初始化線性單元,設定輸入參數的個數‘‘‘        Perception.__init__(self, input_num, f)def get_training_dataset():    ‘‘‘    捏造5個人的收入資料    ‘‘‘    # 構建訓練資料    # 輸入向量列表,每一項是工作年限    input_vecs = [[5], [3], [8], [1.4], [10.1]]    # 期望的輸出資料行表,月薪,注意要與輸入一一對應    labels = [5500, 2300, 7600, 1800, 11400]    return input_vecs, labelsdef train_linear_unit():    ‘‘‘    使用資料訓練線性單元    ‘‘‘    # 建立感知器,輸入參數的特徵數為1(工作年限)    lu = LinearUnit(1)    # 訓練,迭代10輪,學習速率為0.01    input_vecs, labels = get_training_dataset()    lu.train(input_vecs, labels, 10, 0.01)    # 返回訓練好的線性單元    return luif __name__ == ‘__main__‘:    ‘‘‘訓練線性單元‘‘‘    linear_unit = train_linear_unit()    # 列印訓練獲得的權重    print(linear_unit)    # 測試    print(‘Work 3.4 years, monthly salary = %.2f‘ % linear_unit.predict([3.4]))    print(‘Work 15 years, monthly salary = %.2f‘ % linear_unit.predict([15]))    print(‘Work 1.5 years, monthly salary = %.2f‘ % linear_unit.predict([1.5]))    print(‘Work 6.3 years, monthly salary = %.2f‘ % linear_unit.predict([6.3]))

  

運行結果如下:

 

神經網路 線性單元

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.