自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

從程序員的角度設計一個基于Java的神經(jīng)網(wǎng)絡

人工智能 深度學習 后端
人工神經(jīng)網(wǎng)絡(ANN)或連接系統(tǒng)是受生物神經(jīng)網(wǎng)絡啟發(fā)構成生物大腦的計算系統(tǒng)。這樣的系統(tǒng)通過考慮例子來學習(逐步提高性能)來完成任務,通常沒有任務特定的編程。

人工神經(jīng)網(wǎng)絡(ANN)或連接系統(tǒng)是受生物神經(jīng)網(wǎng)絡啟發(fā)構成生物大腦的計算系統(tǒng)。這樣的系統(tǒng)通過考慮例子來學習(逐步提高性能)來完成任務,通常沒有任務特定的編程。 

用Java或任何其他編程語言設計神經(jīng)網(wǎng)絡我們需要理解人工神經(jīng)網(wǎng)絡的結構和功能。

人工神經(jīng)網(wǎng)絡執(zhí)行的任務比如有模式識別、從數(shù)據(jù)中學習以及像專家一樣預測趨勢,而不像傳統(tǒng)的算法方法那樣需要執(zhí)行一組步驟來實現(xiàn)所定義的目標。人工神經(jīng)網(wǎng)絡由于其高度交互的網(wǎng)絡結構,可以學習如何自己解決一些任務。

人造神經(jīng)元具有與人腦神經(jīng)元相似的結構。一個天然的神經(jīng)元是由核,樹突和軸突組成的。軸突延伸到幾個分支形成突觸與其他神經(jīng)元的樹突。

到目前為止,我們已經(jīng)區(qū)分了神經(jīng)元的結構和相連神經(jīng)元的網(wǎng)絡。另一個重要方面是分別與單個神經(jīng)元相關的神經(jīng)網(wǎng)絡的處理或計算。自然神經(jīng)元是信號處理器 - 它們在樹突中接收可以觸發(fā)軸突信號的微信號。有一個潛在的閾值,到達的時候,刺激軸突,并傳播信號到其他神經(jīng)元。因此,我們可以將人造神經(jīng)元視為一個在輸入中具有信號接收器、在輸出中具有激活單元的東西,其可以發(fā)送的信號將被轉發(fā)到與圖中所示類似的其他神經(jīng)元上: 

此外,神經(jīng)元之間的連接具有相應可以修改信號的權重,從而影響神經(jīng)元的輸出。由于權重是神經(jīng)網(wǎng)絡的內部因素并影響其輸出,所以可以認為它們是神經(jīng)網(wǎng)絡的內部學科,調節(jié)描述神經(jīng)元與其他神經(jīng)元或外部世界的連接的權重將反映神經(jīng)網(wǎng)絡的能力。

正如Bioinfo Publications所述:

人造神經(jīng)元接收一個或多個輸入(代表樹突)并將它們相加以產(chǎn)生輸出/ 激活  (代表神經(jīng)元的軸突)。一般來說每個節(jié)點的總和被加權,總和通過激活函數(shù)或傳遞函數(shù)傳遞。

這個組件為神經(jīng)網(wǎng)絡處理增加了非線性,這是因為自然神經(jīng)元具有非線性行為。在一些特殊情況下,它可以是一個線性函數(shù)。 

維基百科提及到說:

一個標準的計算機芯片電路可以看作是一個激活功能的數(shù)字網(wǎng)絡,取決于輸入的是“ON”(1)還是“OFF”(0)。這與神經(jīng)網(wǎng)絡中的線性感知器的行為類似。然而,  非線性  激活函數(shù)允許這樣的網(wǎng)絡僅使用少量的節(jié)點來計算特殊問題。使用的流行的激活函數(shù)的例子是Sigmoid、雙曲正切、硬極限閾值和純線性。

將這些知識轉化為Java代碼,我們將有一個如下的神經(jīng)元類:

  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3. import edu.neuralnet.core.activation.ActivationFunction; 
  4. import edu.neuralnet.core.input.InputSummingFunction; 
  5. /** 
  6.  * Represents a neuron model comprised of(以下內容組成的神經(jīng)元模型): </br> 
  7.  * <ul> 
  8.  * <li>Summing part(求和部分)  - input summing function(輸入求和函數(shù) )</li> 
  9.  * <li>Activation function(激活函數(shù))</li> 
  10.  * <li>Input connections(輸入連接)</li> 
  11.  * <li>Output connections(輸出連接)</li> 
  12.  * </ul> 
  13.  */ 
  14. public class Neuron { 
  15.  /** 
  16.   * Neuron's identifier 
  17.   * 神經(jīng)元標識符 
  18.   */ 
  19.  private String id; 
  20.  /** 
  21.   * Collection of neuron's input connections (connections to this neuron) 
  22.   * 神經(jīng)元輸入連接的集合(與此神經(jīng)元的連接)  
  23.   */ 
  24.  protected List < Connection > inputConnections; 
  25.  /** 
  26.   * Collection of neuron's output connections (connections from this to other 
  27.   * neurons) 
  28.   * 神經(jīng)元輸出連接的集合(從這個到其他神經(jīng)元的連接)  
  29.   */ 
  30.  protected List < Connection > outputConnections; 
  31.  /** 
  32.   * Input summing function for this neuron 
  33.   * 該神經(jīng)元的輸入和函數(shù) 
  34.   */ 
  35.  protected InputSummingFunction inputSummingFunction; 
  36.  /** 
  37.   * Activation function for this neuron 
  38.   * 這個神經(jīng)元的激活函數(shù) 
  39.   */ 
  40.  protected ActivationFunction activationFunction; 
  41.  /** 
  42.   * Default constructor 
  43.   * 默認構造方法 
  44.   */ 
  45.  public Neuron() { 
  46.   this.inputConnections = new ArrayList < > (); 
  47.   this.outputConnections = new ArrayList < > (); 
  48.  } 
  49.  /** 
  50.   * Calculates the neuron's output 
  51.   * 計算神經(jīng)元輸出 
  52.   */ 
  53.  public double calculateOutput() { 
  54.    double totalInput = inputSummingFunction.getOutput(inputConnections); 
  55.    return activationFunction.getOutput(totalInput); 
  56.   } 
  57.   ... 

神經(jīng)元有輸入和輸出連接、輸入求和值和激活函數(shù),那輸入權重在哪里呢?它們包含在連接本身中,如下所示:

  1. /** 
  2.  * Represents a connection between two neurons an the associated weight. 
  3.  * 表示兩個神經(jīng)元之間的連接以及相關的權重 
  4.  */ 
  5. public class NeuronsConnection { 
  6. /** 
  7.  * From neuron for this connection (source neuron). This connection is 
  8.  * output connection for from neuron. 
  9.  * 從神經(jīng)元中獲取這個連接(源神經(jīng)元)。此連接是來自神經(jīng)元的輸出連接 
  10.  */ 
  11. protected Neuron fromNeuron; 
  12. /** 
  13.  * To neuron for this connection (target, destination neuron) This 
  14.  * connection is input connection for to neuron. 
  15.  * 對于用于此連接的神經(jīng)元(目標,目標神經(jīng)元),此連接是神經(jīng)元的輸入連接 
  16.  */ 
  17. protected Neuron toNeuron; 
  18. /** 
  19.  * Connection weight 
  20.  * 連接權重 
  21.  */ 
  22. protected double weight; 
  23. /** 
  24.  * Creates a new connection between specified neurons with random weight. 
  25.  * 在具有隨機權重的指定神經(jīng)元之間創(chuàng)建一個新的連接 
  26.  * @param fromNeuron 
  27.  *            neuron to connect from 
  28.  * @param toNeuron 
  29.  *            neuron to connect to 
  30.  */ 
  31. public NeuronsConnection(Neuron fromNeuron, Neuron toNeuron) { 
  32. this.fromNeuron = fromNeuron; 
  33. this.toNeuron = toNeuron; 
  34. this.weight = Math.random(); 
  35. /** 
  36.  * Creates a new connection to specified neuron with specified weight object 
  37.  * 創(chuàng)建與指定權重對象的指定神經(jīng)元的新連接 
  38.  * 
  39.  * @param fromNeuron 
  40.  *            neuron to connect from 
  41.  * @param toNeuron 
  42.  *            neuron to connect to 
  43.  * @param weight 
  44.  *            weight for this connection 
  45.  */ 
  46. public NeuronsConnection(Neuron fromNeuron, Neuron toNeuron, double weight) { 
  47. this(fromNeuron, toNeuron); 
  48. this.weight = weight; 
  49. /** 
  50.  * Returns weight for this connection 
  51.  * 返回此連接的權重 
  52.  * @return weight for this connection 
  53.  */ 
  54. public double getWeight() { 
  55. return weight; 
  56. /** 
  57.  * Set the weight of the connection
  58.  * 設置連接的權值 
  59.  * @param weight 
  60.  *            The new weight of the connection to be set 
  61.  */ 
  62. public void setWeight(double weight) { 
  63. this.weight = weight; 
  64. /** 
  65.  * Returns input of this connection - the activation function result 
  66.  * calculated in the input neuron of this connection
  67.  * 返回此連接的輸入 - 在此連接輸入神經(jīng)元中激活函數(shù)計算的結果 
  68.  * @return input received through this connection 
  69.  */ 
  70. public double getInput() { 
  71. return fromNeuron.calculateOutput(); 
  72. /** 
  73.  * Returns the weighted input of this connection 
  74.  * 返回此連接的權值輸入 
  75.  * @return weighted input of the connection 
  76.  */ 
  77. public double getWeightedInput() { 
  78. return fromNeuron.calculateOutput() * weight; 
  79. /** 
  80.  * Gets from neuron for this connection 
  81.  * 從神經(jīng)元獲取此連接 
  82.  * @return from neuron for this connection 
  83.  */ 
  84. public Neuron getFromNeuron() { 
  85. return fromNeuron; 
  86. /** 
  87.  * Gets to neuron for this connection 
  88.  * 獲取用于此連接的神經(jīng)元 
  89.  * @return neuron to set as to neuron 
  90.  */ 
  91. public Neuron getToNeuron() { 
  92. return toNeuron; 
  93. ... 

連接對象提供權重并負責計算輸入的權值。

求和函數(shù)被定義為接口,以便能夠替換神經(jīng)元的計算策略:

  1. import java.util.List; 
  2. import edu.neuralnet.core.Connection
  3. /** 
  4.  * Represents the inputs summing part of a neuron also called signal collector. 
  5.  * 神經(jīng)元的求和部分,也可以稱為信號收集器 
  6.  */ 
  7. public interface InputSummingFunction { 
  8. /** 
  9.  * Performs calculations based on the output values of the input neurons. 
  10.  * 根據(jù)輸入神經(jīng)元的輸出值執(zhí)行計算 
  11.  * @param inputConnections 
  12.  *            neuron's input connections 
  13.  * @return total input for the neuron having the input connections 
  14.  *         總輸入,具有輸入連接的神經(jīng)元 
  15.  */ 
  16. double collectOutput(List<Connection> inputConnections); 

分別實現(xiàn)為:

  1. import java.util.List; 
  2. import edu.neuralnet.core.Connection
  3. /** 
  4.  * Calculates the weighted sums of the input neurons' outputs. 
  5.  * 計算輸入神經(jīng)元輸出的加權和 
  6.  */ 
  7. public final class WeightedSumFunction implements InputSummingFunction { 
  8. /** 
  9.  * {@inheritDoc} 
  10.  */ 
  11. @Override 
  12. public double collectOutput(List<Connection> inputConnections) { 
  13. double weightedSum = 0d; 
  14. for (Connection connection : inputConnections) { 
  15. weightedSum += connection.getWeightedInput(); 
  16. return weightedSum; 

激活函數(shù)的接口可以定義如下::

  1. /** 
  2.  * Neural networks activation function interface. 
  3.  * 神經(jīng)網(wǎng)絡激活函數(shù)的接口 
  4.  */ 
  5. public interface ActivationFunction { 
  6. /** 
  7.  * Performs calculation based on the sum of input neurons output
  8.  * 基于輸入神經(jīng)元輸出的和來進行計算 
  9.  * @param summedInput 
  10.  *            neuron's sum of outputs respectively inputs for the connected 
  11.  *            neuron 
  12.  *  
  13.  * @return Output's calculation based on the sum of inputs 
  14.  *         基于輸入和來計算輸出 
  15.  */ 
  16. double calculateOutput(double summedInput); 

開始編寫代碼之前需要注意的***一個問題是神經(jīng)網(wǎng)絡層。神經(jīng)網(wǎng)絡由幾個鏈接層組成,形成所謂的多層網(wǎng)絡。神經(jīng)層可以分為三類:

  1. 輸入層
  2. 隱藏層
  3. 輸出層

在實踐中,額外的神經(jīng)層增加了另一個抽象層次的外部刺激,增強了神經(jīng)網(wǎng)絡認知更復雜知識的能力。

一個圖層類可以被定義為一個有連接的神經(jīng)元列表:

  1. import java.util.ArrayList; 
  2. import java.util.List; 
  3. /** 
  4.  * Neural networks can be composed of several linked layers, forming the 
  5.  * so-called multilayer networks. A layer can be defined as a set of neurons 
  6.  * comprising a single neural net's layer. 
  7.  * 神經(jīng)網(wǎng)絡可以由多個連接層組成,形成所謂的多層網(wǎng)絡, 
  8.  * 一層可以定義為一組包含神經(jīng)網(wǎng)絡層的神經(jīng)元 
  9.  */ 
  10. public class NeuralNetLayer { 
  11. /** 
  12.  * Layer's identifier 
  13.  * 層次標識符  
  14.  */ 
  15. private String id; 
  16. /** 
  17.  * Collection of neurons in this layer 
  18.  * 該層神經(jīng)元的集合 
  19.  */ 
  20. protected List<Neuron> neurons; 
  21. /** 
  22.  * Creates an empty layer with an id. 
  23.  * 用ID創(chuàng)建一個空層 
  24.  * @param id 
  25.  *            layer's identifier 
  26.  */ 
  27. public NeuralNetLayer(String id) { 
  28. this.id = id; 
  29. neurons = new ArrayList<>(); 
  30. /** 
  31.  * Creates a layer with a list of neurons and an id. 
  32.  * 創(chuàng)建一個包含神經(jīng)元列表和id的層 
  33.  * @param id 
  34.  *            layer's identifier 層次標識符  
  35.  * @param neurons 
  36.  *            list of neurons to be added to the layer 添加到該層的神經(jīng)元列表 
  37.  */ 
  38. public NeuralNetLayer(String id, List<Neuron> neurons) { 
  39. this.id = id; 
  40. this.neurons = neurons; 
  41. ... 

***,用Java創(chuàng)建一個簡單的神經(jīng)網(wǎng)絡:

  1. /** 
  2.  * Represents an artificial neural network with layers containing neurons. 
  3.  * 含有神經(jīng)元層的人工神經(jīng)網(wǎng)絡 
  4.  */ 
  5. public class NeuralNet { 
  6. /** 
  7.  * Neural network id 
  8.  * 神經(jīng)網(wǎng)絡ID 
  9.  */ 
  10. private String id; 
  11. /** 
  12.  * Neural network input layer 
  13.  * 神經(jīng)網(wǎng)絡的輸入層 
  14.  */ 
  15. private NeuralNetLayer inputLayer; 
  16. /** 
  17.  * Neural network hidden layers 
  18.  * 神經(jīng)網(wǎng)絡隱藏的層 
  19.  */ 
  20. private List<NeuralNetLayer> hiddenLayers; 
  21. /** 
  22.  * Neural network output layer 
  23.  * 神經(jīng)網(wǎng)絡的輸出層 
  24.  */ 
  25. private NeuralNetLayer outputLayer; 
  26. /** 
  27.  * Constructs a neural net with all layers present. 
  28.  * 構造一個具有所有層的神經(jīng)網(wǎng)絡 
  29.  * @param id 
  30.  *            Neural network id to be set 設置神經(jīng)網(wǎng)絡標識 
  31.  * @param inputLayer 
  32.  *            Neural network input layer to be set 設置神經(jīng)網(wǎng)絡的輸入層  
  33.  * @param hiddenLayers 
  34.  *            Neural network hidden layers to be set 設置神經(jīng)網(wǎng)絡隱藏的層 
  35.  * @param outputLayer 
  36.  *            Neural network output layer to be set 設置神經(jīng)網(wǎng)絡的輸出層 
  37.  */ 
  38. public NeuralNet(String id, NeuralNetLayer inputLayer, List<NeuralNetLayer> hiddenLayers, 
  39. NeuralNetLayer outputLayer) { 
  40. this.id = id; 
  41. this.inputLayer = inputLayer; 
  42. this.hiddenLayers = hiddenLayers; 
  43. this.outputLayer = outputLayer; 
  44. /** 
  45.  * Constructs a neural net without hidden layers. 
  46.  * 構造一個沒有隱藏層的神經(jīng)網(wǎng)絡 
  47.  * @param id 
  48.  *            Neural network id to be set 設置神經(jīng)網(wǎng)絡標識 
  49.  * @param inputLayer 
  50.  *            Neural network input layer to be set 設置神經(jīng)網(wǎng)絡的輸入層  
  51.  * @param outputLayer 
  52.  *            Neural network output layer to be set 設置神經(jīng)網(wǎng)絡隱藏的層 
  53.  */ 
  54. public NeuralNet(String id, NeuralNetLayer inputLayer, NeuralNetLayer outputLayer) { 
  55. this.id = id; 
  56. this.inputLayer = inputLayer; 
  57. this.outputLayer = outputLayer; 
  58. ... 

我們所得到的是一個基于Java的神經(jīng)網(wǎng)絡層次、神經(jīng)元和連接的結構定義。我們也談到了一些關于激活函數(shù)的內容,并為它們定義了一個接口。為簡單起見,我們省略了各種激活函數(shù)的實現(xiàn)以及學習神經(jīng)網(wǎng)絡的基礎知識。這兩個主題將在本系列的后續(xù)文章中介紹。

原文鏈接:https://cloud.tencent.com/developer/article/1038393

作者:Daniela Kolarova

【本文是51CTO專欄作者“云加社區(qū)”的原創(chuàng)稿件,轉載請通過51CTO聯(lián)系原作者獲取授權】

戳這里,看該作者更多好文

責任編輯:武曉燕 來源: 51CTO專欄
相關推薦

2020-02-22 21:51:43

程序員Microsoft SServerSQL

2025-02-25 14:13:31

2016-11-04 10:30:17

微信小程序

2014-01-06 09:33:32

程序員管理

2019-11-11 09:02:51

MySQL數(shù)據(jù)庫索引

2018-10-26 15:30:49

程序員MySQL數(shù)據(jù)庫

2015-04-20 09:50:58

程序員

2009-06-14 18:43:57

LinuxWindows對比

2011-02-14 13:05:17

PythonWeb

2015-06-08 10:48:39

程序員程序員自白

2015-03-19 14:50:27

編程拖拽編程合格程序員

2020-07-10 09:55:15

程序員技能開發(fā)者

2015-06-16 10:31:36

程序員

2015-04-14 11:15:18

程序員創(chuàng)業(yè)程序員談創(chuàng)業(yè)

2011-11-24 14:20:24

Java

2016-09-27 17:29:23

騰訊云小程序微信

2014-04-16 11:39:52

2025-02-19 18:00:00

神經(jīng)網(wǎng)絡模型AI

2017-05-05 08:57:06

卷積神經(jīng)網(wǎng)絡機制

2017-05-03 08:52:13

卷積神經(jīng)網(wǎng)絡神經(jīng)網(wǎng)絡非線性激活函數(shù)
點贊
收藏

51CTO技術棧公眾號