楼主: Scalachen
1427 0

Artificial Neural Networks using Julia [推广有奖]

  • 0关注
  • 0粉丝

本科生

56%

还不是VIP/贵宾

-

TA的文库  其他...

Haskell NewOccidental

Splunk NewOccidental

Apache Storm NewOccidental

威望
0
论坛币
5149 个
通用积分
0
学术水平
9 点
热心指数
11 点
信用等级
9 点
经验
1156 点
帖子
24
精华
1
在线时间
0 小时
注册时间
2015-3-29
最后登录
2017-8-22

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
  1. type NeuralLayer
  2.     w::Matrix{Float64}   # weights
  3.     b::Vector{Float64}   # biases
  4.     a_func::Function     # activation function
  5.     a_derv::Function     # activation funciton derivative

  6.     # The following must be tracked for back propagation
  7.     hx::Vector{Float64}  # input values
  8.     pa::Vector{Float64}  # pre activation values
  9.     pr::Vector{Float64}  # predictions (activation values)
  10.     # Gradients
  11.     wgr::Matrix{Float64} # weight gradient
  12.     bgr::Vector{Float64} # bias gradient
  13. end


  14. # sigmoidal activation function
  15. function sigm(a::Vector{Float64})
  16.     1. ./ (1. + exp(-a))
  17. end

  18. # sigmoid derivative
  19. function sigm_derv(a::Vector{Float64})
  20.     s_a = sigm(a)
  21.     s_a .* (1. - s_a)
  22. end

  23. function NeuralLayer(in_dim::Integer,out_dim::Integer)
  24.     # Glorot & Bengio, 2010
  25.     b = sqrt(6) / sqrt(in_dim + out_dim)
  26.     NeuralLayer(2b * rand(out_dim,in_dim) - b,
  27.                 zeros(out_dim),
  28.                 sigm,
  29.                 sigm_derv,

  30.                 zeros(in_dim),
  31.                 zeros(out_dim),
  32.                 zeros(out_dim),

  33.                 zeros(out_dim,in_dim),
  34.                 zeros(out_dim)
  35.     )
  36. end

  37. type ArtificialNeuralNetwork
  38.     layers::Vector{NeuralLayer}
  39.     hidden_sizes::Vector{Int64} # Number of nodes in each hidden level
  40.     classes::Vector{Int64}
  41. end

  42. function softmax(ann_output::Vector{Float64})
  43.     # Takes the output of a neural network and produces a valid
  44.     # probability distribution
  45.     ann_output = exp(ann_output)
  46.     ann_output / sum(ann_output)
  47. end

  48. function ArtificialNeuralNetwork(n_hidden_units::Integer)
  49.     ann = ArtificialNeuralNetwork(Array(NeuralLayer,0),
  50.                                  [n_hidden_units],
  51.                                  Array(Int64,0))
  52. end

  53. function forward_propagate(nl::NeuralLayer,x::Vector{Float64})
  54.     nl.hx = x
  55.     nl.pa = nl.b + nl.w * x
  56.     nl.pr = nl.a_func(nl.pa)
  57. end

  58. function back_propagate(nl::NeuralLayer,output_gradient::Vector{Float64})
  59.     nl.wgr = output_gradient * nl.hx' # compute weight gradient
  60.     nl.bgr = output_gradient # compute bias gradient
  61.     nl.w' * output_gradient # return gradient of level below
  62. end

  63. function init!(ann::ArtificialNeuralNetwork,
  64.                      x::Matrix{Float64},
  65.                      y::Vector{Int64})
  66.     layers = Array(NeuralLayer,length(ann.hidden_sizes) + 1)
  67.     ann.classes = unique(y)
  68.     sort!(ann.classes)
  69.     input_dim = size(x)[2]
  70.     for i = 1:length(ann.hidden_sizes)
  71.         out_dim = ann.hidden_sizes[i]
  72.         layers[i] = NeuralLayer(input_dim,out_dim)
  73.         input_dim = out_dim
  74.     end
  75.     layers[length(layers)] = NeuralLayer(input_dim,length(ann.classes))
  76.     layers[length(layers)].a_func = softmax
  77.     ann.layers = layers
  78.     ann
  79. end

  80. function fit!(ann::ArtificialNeuralNetwork,
  81.               x::Matrix{Float64},
  82.               y::Vector{Int64};
  83.               epochs::Int64 = 5,
  84.               alpha::Float64 = 0.1,
  85.               lambda::Float64 = 0.1)
  86.     init!(ann,x,y)
  87.     n_obs, n_feats = size(x)
  88.     layers = ann.layers
  89.     n_layers = length(layers)
  90.     for _ = 1:epochs
  91.         for i = 1:n_obs
  92.             y_hat = zeros(length(ann.classes))
  93.             y_hat[findfirst(ann.classes,y[i])] = 1.

  94.             y_pred = predict(ann,x[i,:][:])
  95.             output_gradient = -(y_hat - y_pred)
  96.             for j = n_layers:-1:2
  97.                 # This returns the gradient of the j-1 layer
  98.                 next_layer_gr = back_propagate(layers[j],output_gradient)
  99.                 next_layer = layers[j-1]
  100.                 output_gradient = next_layer_gr .* next_layer.a_derv(next_layer.pa)
  101.             end
  102.             back_propagate(layers[1],output_gradient)

  103.             # Compute delta and step
  104.             for j = 1:n_layers
  105.                 nl = layers[j]
  106.                 # Computer L2 weight penatly
  107.                 weight_delta = (-nl.wgr) - (lambda * (2 * nl.w))
  108.                 nl.w = alpha * weight_delta + nl.w
  109.                 nl.b = alpha * (-nl.bgr) + nl.b
  110.             end
  111.         end
  112.     end
  113. end


  114. # Predict class probabilities for a given observation
  115. function predict(ann::ArtificialNeuralNetwork,x::Vector{Float64})
  116.     for i in 1:length(ann.layers)
  117.         x = forward_propagate(ann.layers[i],x)
  118.     end
  119.     x
  120. end

  121. # Handle a Matrix input
  122. function predict(ann::ArtificialNeuralNetwork,x::Matrix{Float64})
  123.     n_obs,n_feats = size(x)
  124.     y_proba = zeros((n_obs,length(ann.classes)))
  125.     for i = 1:n_obs
  126.         y_proba[i,:] = predict(ann,x[i,:][:])
  127.     end
  128.     y_proba
  129. end
复制代码

In machine learning and cognitive science, artificial neural networks (ANNs) are a family of statistical learning algorithms inspired by biological neural networks (the central nervous systems of animals, in particular thebrain) and are used to estimate or approximate functions that can depend on a large number of inputs and are generally unknown. Artificial neural networks are generally presented as systems of interconnected "neurons" which can compute values from inputs, and are capable of machine learning as well as pattern recognition thanks to their adaptive nature.

For example, a neural network for handwriting recognition is defined by a set of input neurons which may be activated by the pixels of an input image. After being weighted and transformed by a function[disambiguation needed](determined by the network's designer), the activations of these neurons are then passed on to other neurons. This process is repeated until finally, an output neuron is activated. This determines which character was read.

Like other machine learning methods - systems that learn from data - neural networks have been used to solve a wide variety of tasks that are hard to solve using ordinary rule-based programming, including computer vision and speech recognition.


Perhaps the greatest advantage of ANNs is their ability to be used as an arbitrary function approximation mechanism that 'learns' from observed data. However, using them is not so straightforward, and a relatively good understanding of the underlying theory is essential.

  • Choice of model: This will depend on the data representation and the application. Overly complex models tend to lead to problems with learning.
  • Learning algorithm: There are numerous trade-offs between learning algorithms. Almost any algorithm will work well with the correct hyperparameters for training on a particular fixed data set. However, selecting and tuning an algorithm for training on unseen data requires a significant amount of experimentation.
  • Robustness: If the model, cost function and learning algorithm are selected appropriately the resulting ANN can be extremely robust.

With the correct implementation, ANNs can be used naturally in online learning and large data set applications. Their simple implementation and the existence of mostly local dependencies exhibited in the structure allows for fast, parallel implementations in hardware.



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:Artificial Networks network Neural Works following function gradient Vector Matrix

本帖被以下文库推荐

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注jltj
拉您入交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-1 21:40