【问题标题】:Perceptron with unigram features, how does it learn and what are the next steps?具有 unigram 特征的感知器,它是如何学习的,下一步是什么?
【发布时间】:2017-05-30 17:36:27
【问题描述】:

我正在学习基本的 NLP 算法,特别是具有 unigram 特征的简单感知器。

我从thisthis 中查看了感知器的基础知识。

这是我的简单 Haskell 感知器,改编自 this 来源:

type Inputs = [Float] 
type Weights = [Float]
type Threshold = Float
type LearnRate = Float
type Expected = Float
type Actual = Float

neuronOutput :: (Num a, Ord a) => Inputs -> Weights -> Threshold -> a 
--neuronOutput :: (Num a, Ord a) => [a] -> [a] -> a -> a --Parametic polymorphic 
neuronOutput inputs weights thresh
| total - thresh >= 0                       = 1
| otherwise                                 = 0
where
    total = foldl (+) 0 $ zipWith (*) inputs weights


adjustWeights :: Inputs -> Weights -> Expected -> Actual -> LearnRate -> Weights --Adjust the weights 
--adjustWeights :: (Num a) => [a] -> [a] -> a -> a -> a -> [a] --Parametic polymorphic 
adjustWeights inputs orgiWeights expected actual learn = map delta $ zip inputs orgiWeights
where 
    delta (i, w) = w + (learn * i * e)
    e = expected - actual


singleIteration :: Inputs -> Weights -> Expected -> LearnRate -> Threshold -> Weights   --Return adjusted weights based on neuronOutput
--singleIteration :: (Num a, Ord a) => [a] -> [a] -> a -> a -> a -> [a] --Parametic polymorphic 
singleIteration inputs weights expectd learn thresh = adjustWeights inputs weights expectd output learn
  where 
     output = neuronOutput inputs weights thresh



implementIterations :: [(Inputs, Expected)] -> Weights -> LearnRate -> Threshold -> (Inputs, Expected) --Applies singleIteration to each input set
implementIterations allInputs weights learnR thresH = (newWeights, delta)
 where
    newWeights = foldl iterate weights allInputs
    iterate w (i, e) = singleIteration i w e learnR thresH
    delta = (foldl (+) 0 $ map abs $ zipWith (-) newWeights weights) / (fromIntegral $ length weights) --Func composition here to make better?


runLearning :: [(Inputs, Expected)] -> LearnRate -> Threshold -> Weights -> Int -> (Inputs, Int)
runLearning allInputs learnR thresH weights epochNb
  | delta == 0              = (newWeights, epochNb)
  | otherwise               = runLearning allInputs learnR thresH newWeights (epochNb + 1) --Recusive changing weights each time
  where
    (newWeights, delta) = implementIterations allInputs weights learnR thresH


main = do
  let inputs = [([1, 1, 1], 1), ([0, 0, 0], -1)]
  let weights = [1, 1, 1, 0, 0, -4]
  print  $ runLearning inputs 0.1 0.2 weights 1

我的问题是:

1) 这段代码的哪一部分“学习”了?我知道每个特征都有一个相关的权重(正或负),但是我看不到每次迭代如何从先验结果中学习?

2) NLP 算法的“下一阶段”是什么? IE。我知道单层感知器非常简单,我应该寻找哪些其他神经网络结构和/或不同的算法来获得更准确的分类器?

【问题讨论】:

    标签: haskell nlp neural-network perceptron


    【解决方案1】:

    1) 这段代码的哪一部分“学习”了?

    通过调整权重来实现学习,使感知器在训练数据上的输出更接近期望的输出。

    2) NLP 算法的“下一阶段”是什么?

    我对 NLP 算法一无所知,但感知器是神经网络的构建块。我认为你接下来要看的是前馈反向传播神经网络。它们由连接的感知器层组成。复习线性代数和多变量微积分,因为调整权重有点复杂!

    【讨论】:

      猜你喜欢
      • 2017-04-01
      • 2014-03-11
      • 1970-01-01
      • 2014-07-03
      • 2012-01-31
      • 1970-01-01
      • 2011-06-04
      • 1970-01-01
      • 2018-05-30
      相关资源
      最近更新 更多