Skip to content

Instantly share code, notes, and snippets.

@KatagiriSo
Created June 12, 2017 09:38
Show Gist options
  • Save KatagiriSo/0a935ed0e2f42eaad126d7b9cde516d9 to your computer and use it in GitHub Desktop.
Save KatagiriSo/0a935ed0e2f42eaad126d7b9cde516d9 to your computer and use it in GitHub Desktop.
main = do
print "Hello, World!"
-- training data とはfeature vector とラベルの組である。
-- feature vector
data FeatureVector = FeatureVector { x :: Double} deriving Show
-- 入力
type InputSpace = [FeatureVector]
-- newtype InputSpace = InputSpace [FeatureVector] deriving Show
inputSpace = map (FeatureVector) [1,2,3,4,5,6,7,8,9,10]
-- ラベル
type Label = Double
-- 出力
type OutputSpace = [Label]
-- newtype OutputSpace = OutputSpace [Label] deriving Show
outputSpace::OutputSpace
outputSpace = [2,4,6,8,10,12,14,16,18,20]
type TrainingExample = (FeatureVector, Label)
--newtype TrainingExample = TrainingExample (FeatureVector, Label) deriving Show
-- data TrainingExample = TrainingExample {featureVector :: FeatureVector, label :: Label} deriving Show
type TrainingExamples = [TrainingExample]
-- newtype TrainingExamples = TrainingExamples [TrainingExample] deriving Show
trainingExamples = zip inputSpace outputSpace
-- g:: InputSpace -> OutputSpace
-- gは仮説空間Gの元
type Hypothesis = FeatureVector -> Label
type HypothesisSpace = [Hypothesis]
-- newtype HypothesisSpace = HypothesisSpace [InputSpace -> OutputSpace]
-- 損失関数
-- 絶対値はどうやるの?
-- costFunctionはlossFunctionの別名
lossFunction:: Hypothesis -> TrainingExample -> Double
-- 損失2乗誤差
lossFunction f (x, y) = (y - f(x))^2
-- 標準誤差(sample error) 経験誤算、経験損失
sampleError:: Hypothesis -> TrainingExamples -> Double
sampleError f [] = 0
sampleError f dataList = sum(map (lossFunction f) dataList) / realToFrac(length(dataList))
-- map (\(x,y) -> (x,2*y)) [(1,3),(2,4)]
type Parameter = (Double, Double)
makeHypothsis :: Parameter -> Hypothesis
makeHypothsis (a,b) = \fv -> a * x(fv) + b
errorFunc::Parameter -> TrainingExamples -> Double
errorFunc = lossFunction makeHypothsis
-- errorFunc::Parameter -> TrainingExamples -> Double
-- derrorFunc_a ((a,b), t) = errorFunc(a
parameters::[Parameter]
parameters = [(1,0),(1.5,0),(2.0,0),(2.5,0)]
gs :: HypothesisSpace
gs = map makeHypothsis parameters
reslist = [sampleError g trainingExamples | g <- gs]
res = reslist !! 0
res2 = reslist !! 1
hypothesis_0::Hypothesis
hypothesis_0 = makeHypothsis (3,2)
result0 = sampleError hypothesis_0 trainingExamples
type LearningCoefficent
grade :: Parameter -> LearningCoefficent -> Error -> Parameter
grade ((a,b), e, ) = ()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment