-- END stolen things

{-
data Neuron = Neuron {
  inputs :: [Neuron],
  weights :: [Double],
  activation :: Double -> Double
}

e = exp 1

sigmoidActivation :: Double -> Double
sigmoidActivation v = 1 / (1 + e ** (- v))

-- Get the output value of a neuron
output :: Neuron -> Double
output neuron = activation neuron $ sum inputValues where
  inputValues = map output (inputs neuron)

testNet = Neuron { inputs = [], weights = [], activation = const 0 }
-}

main :: IO ()
main = do
  trainingImages <- imagesFromIDXFile "./data/t10k-images-idx3-ubyte"
  trainingLabels <- labelsFromIDXFile "./data/t10k-labels-idx1-ubyte"
  testImages <- imagesFromIDXFile "./data/train-images-idx3-ubyte"
  testLabels <- labelsFromIDXFile "./data/train-labels-idx1-ubyte"

  b <- newBrain [784, 30, 10]
  n <- (`mod` (10000 :: Int)) <$> randomIO

  let
    example = getX testImages n
    bs = scanl (foldl' (\b n -> learn (getX trainingImages n) (getY trainingLabels n) b)) b [
     [   0.. 999],
     [1000..2999],
     [3000..5999],
     [6000..9999]]
    smart = last bs
    cute d score = show d ++ ": " ++ replicate (round $ 70 * min 1 score) '+'
    bestOf = fst . maximumBy (comparing snd) . zip [0..]

  forM_ bs $ putStrLn . unlines . zipWith cute [0..9] . feed example

  putStrLn $ "best guess: " ++ show (bestOf $ feed example smart)

  let answers = getLabel testLabels <$> [0..9999]

  --putStrLn $ show (sum $ fromEnum <$> zipWith (==) guesses answers) ++
   -- " / 10000"

  --let classifier = fromJust $ trainClassifier trainingImages trainingLabels
  let guesses = bestOf . (\n -> feed (getX testImages n) smart) <$> [0..9999]
  let numberOfCorrectGuesses = length (filter (uncurry (==)) (zip answers guesses))
  let ratioCorrect = realToFrac numberOfCorrectGuesses / realToFrac (length answers)
  let outputMessage = show (100.0 * ratioCorrect) ++ "% of labels on test images guessed correctly"
    in putStrLn outputMessage
main :: IO ()
main = do
  trainingImages <- imagesFromIDXFile "./data/t10k-images-idx3-ubyte"
  trainingLabels <- labelsFromIDXFile "./data/t10k-labels-idx1-ubyte"
  testImages <- imagesFromIDXFile "./data/train-images-idx3-ubyte"
  testLabels <- labelsFromIDXFile "./data/train-labels-idx1-ubyte"

  b <- newBrain [784, 30, 10]
  n <- (`mod` (10000 :: Int)) <$> randomIO

  let
    example = getX testImages n
    bs = scanl (foldl' (\b n -> learn (getX trainingImages n) (getY trainingLabels n) b)) b [
     [   0.. 999],
     [1000..2999],
     [3000..5999],
     [6000..9999]]
    smart = last bs
    cute d score = show d ++ ": " ++ replicate (round $ 70 * min 1 score) '+'
    bestOf = fst . maximumBy (comparing snd) . zip [0..]

  forM_ bs $ putStrLn . unlines . zipWith cute [0..9] . feed example

  putStrLn $ "best guess: " ++ show (bestOf $ feed example smart)

  let answers = getLabel testLabels <$> [0..9999]

  --putStrLn $ show (sum $ fromEnum <$> zipWith (==) guesses answers) ++
   -- " / 10000"

  --let classifier = fromJust $ trainClassifier trainingImages trainingLabels
  let guesses = bestOf . (\n -> feed (getX testImages n) smart) <$> [0..9999]
  let numberOfCorrectGuesses = length (filter (uncurry (==)) (zip answers guesses))
  let ratioCorrect = realToFrac numberOfCorrectGuesses / realToFrac (length answers)
  let outputMessage = show (100.0 * ratioCorrect) ++ "% of labels on test images guessed correctly"
    in putStrLn outputMessage
main = do
  trainingImages <- imagesFromIDXFile "./data/t10k-images-idx3-ubyte"
  trainingLabels <- labelsFromIDXFile "./data/t10k-labels-idx1-ubyte"
  testImages <- imagesFromIDXFile "./data/train-images-idx3-ubyte"
  testLabels <- labelsFromIDXFile "./data/train-labels-idx1-ubyte"

  b <- newBrain [784, 30, 10]
  n <- (`mod` (10000 :: Int)) <$> randomIO

  let
    example = getX testImages n
    bs = scanl (foldl' (\b n -> learn (getX trainingImages n) (getY trainingLabels n) b)) b [
     [   0.. 999],
     [1000..2999],
     [3000..5999],
     [6000..9999]]
    smart = last bs
    cute d score = show d ++ ": " ++ replicate (round $ 70 * min 1 score) '+'
    bestOf = fst . maximumBy (comparing snd) . zip [0..]

  forM_ bs $ putStrLn . unlines . zipWith cute [0..9] . feed example

  putStrLn $ "best guess: " ++ show (bestOf $ feed example smart)

  let answers = getLabel testLabels <$> [0..9999]

  --putStrLn $ show (sum $ fromEnum <$> zipWith (==) guesses answers) ++
   -- " / 10000"

  --let classifier = fromJust $ trainClassifier trainingImages trainingLabels
  let guesses = bestOf . (\n -> feed (getX testImages n) smart) <$> [0..9999]
  let numberOfCorrectGuesses = length (filter (uncurry (==)) (zip answers guesses))
  let ratioCorrect = realToFrac numberOfCorrectGuesses / realToFrac (length answers)
  let outputMessage = show (100.0 * ratioCorrect) ++ "% of labels on test images guessed correctly"
    in putStrLn outputMessage