Advertisement
incogn1too

nn

Jan 16th, 2012
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scheme 8.64 KB | None | 0 0
  1. (define (n-mk lst) ;make new neuron
  2.  
  3.  (list 'n lst))
  4.  
  5.  
  6.  
  7.  (define (l-mk lst) ;make new layer
  8.  
  9.  (list 'l lst))
  10.  
  11.  
  12.  
  13.  (define (n-inp-lst x) ;return neuron wheights
  14.  
  15.  (cadr x))
  16.  
  17.  
  18.  
  19.  (define (n? x) ;check if x is neuron
  20.  
  21.  (if (= (car x) 'n) #t #f))
  22.  
  23.  
  24.  
  25.  (define (l-neuron-lst x) ;returns list of layers neurons
  26.  
  27.  (cadr x))
  28.  
  29.  
  30.  
  31.  (define (l? x) ;checks if x is a layer
  32.  
  33.  (if (= (car x) 'l) #t #f))
  34.  
  35.  
  36.  
  37.  (define (l-inp-wh l) ;shows all layer neurns input wheights
  38.  
  39.  (map (lambda(x)(n-inp-lst x)) (l-neuron-lst l)))
  40.  
  41.  
  42.  
  43.  (define (l-out-wh l) ;returns all layer neuron output wheights
  44.  
  45.  (transpose (map (lambda(x)(n-inp-lst x)) (l-neuron-lst l)) '() ))
  46.  
  47.  
  48.  
  49.  (define (net-inp-wh net) ;return all network input wheights
  50.  
  51.  (map (lambda(x)(l-inp-wh x)) net))
  52.  
  53.  
  54.  
  55.  (define (net-out-wh net) ;returns all network output wheights
  56.  
  57.  (map (lambda(x)(l-out-wh x)) net))
  58.  
  59.  
  60.  
  61.  (define (net-mk-data lst) ;make network from data list
  62.  
  63.  (map (lambda(x)(l-mk-data x)) lst))
  64.  
  65.  
  66.  
  67.  (define (l-mk-data lst) ;make layer from list
  68.  
  69.  (l-mk (map (lambda(x)(n-mk x)) lst)))
  70.  
  71.  
  72.  
  73.  (define (lst-push x lst) ;push
  74.  
  75.  (cons x lst))
  76.  
  77.  
  78.  
  79.  (define (lst-push-b x lst) ;push from the end
  80.  
  81.  (append lst (list x)))
  82.  
  83.  
  84.  
  85.  (define (randomize n lst) ;randomize returns n random numbers
  86.  
  87.  (if (= n 0)
  88.  
  89.  lst
  90.  
  91.  (randomize (- n 1) (append (list (+ (random 9) 1) ) lst) )))
  92.  
  93.  
  94.  
  95.  (define (empty? lst) ;checks if the list is empty
  96.  
  97.  (if (= (length lst) 0) #t #f))
  98.  
  99.  
  100.  
  101.  (define (net-make lay) ;makes new network. lay consists of number of neuron for each layer
  102.  
  103.  (net-mk-new (car lay) (cdr lay) '()))
  104.  
  105.  
  106.  
  107.  (define (net-mk-new input n-lst lst) ;input — ammount of input neurons; n-lsts — list of neuron ammount for each layer
  108.  
  109.  (let ( (lst1 (lst-push-b (l-mk-new input (car n-lst) '()) lst)) )
  110.  
  111.  (if (= (length n-lst) 1) lst1 (net-mk-new (car n-lst) (cdr n-lst) lst1))))
  112.  
  113.  
  114.  
  115.  (define (l-mk-new input n lst) ;makes new layer
  116.  
  117.  (let ( (lst1 (append lst (list (n-mk (lst-rand input))))) ); seit kluda
  118.  
  119.  (if (= n 1) (l-mk lst1) (l-mk-new input (- n 1) lst1))))
  120.  
  121.  
  122.  
  123.  (define (lst-rand n) ;random returns n random numbers divided by 10
  124.  
  125.  (map (lambda(x)(/ x 10)) (randomize n '()) ))
  126.  
  127.  
  128.  
  129.  (define (n-akt x param) ;neuron activation function
  130.  
  131.  (/ x (+ (abs x) param)))
  132.  
  133.  
  134.  
  135.  (define (lst-sum lst);sums list
  136.  
  137.  (apply + lst))
  138.  
  139.  
  140.  
  141.  (define (n-proc neuron input) ;process single neuron
  142.  
  143.  (n-akt (lst-sum (lst-mul (n-inp-lst neuron) input )) 4))
  144.  
  145.  
  146.  
  147.  (define (lst-mul a b) ;multiply each element of list a with same element of list b
  148.  
  149.  (map (lambda(x y)(* x y)) a b))
  150.  
  151.  
  152.  
  153.  (define (lst-if a b) ;if one of the lists consists of only one element it returns true
  154.  
  155.  (if (or (= (length a) 1) (= (length b) 1)) #t #f))
  156.  
  157.  
  158.  
  159.  (define (l-proc l input) ;proceses a layer
  160.  
  161.  (map (lambda(x)(n-proc x input)) (n-inp-lst l)))
  162.  
  163.  
  164.  
  165.  (define (net-proc net input) ;proceses a network
  166.  
  167.  (net-proc2 net (l-check input)))
  168.  
  169.  
  170.  
  171.  (define (net-proc2 net input)
  172.  
  173.  (let ( (l (l-proc (car net) input)) )
  174.  
  175.  (if (= 1 (length net)) l
  176.  
  177.  (net-proc2 (cdr net) l))))
  178.  
  179.  
  180.  
  181.  (define (net-proc-res net input) ;returns a list of results for each layer
  182.  
  183.  (net-proc-res2 net (l-check input) '() ))
  184.  
  185.  
  186.  
  187.  (define (net-proc-res2 net input res)
  188.  
  189.  (let ( (l (l-proc (car net) input)) )
  190.  
  191.  (let ( (res1 (lst-push-b l res)) )
  192.  
  193.  (if (= 1 (length net)) res1
  194.  
  195.  (net-proc-res2 (cdr net) l res1)))))
  196.  
  197.  
  198.  
  199.  (define (l-err out err wh) ;counts error for layer
  200.  
  201.  (lst-mul out (l-err*wh err wh)))
  202.  
  203.  
  204.  
  205.  (define (l-err*wh err wh) ;next l error * output wheights
  206.  
  207.  (map (lambda(y)(lst-sum y)) (map (lambda(x)(lst-mul err x)) wh)))
  208.  
  209.  
  210.  
  211.  (define (lst-2-mul a b) ;multiply 2 lists
  212.  
  213.  (map (lambda(x y)(* x y)) a b))
  214.  
  215.  
  216.  
  217.  (define (lst-lst-mul a b); miltiply list a with every list from b
  218.  
  219.  (map (lambda(x)(lst-mul-x x a)) b))
  220.  
  221.  
  222.  
  223.  (define (net-wh*err wh err) ;miltiply each wheight with error
  224.  
  225.  (map (lambda(x y)(lst-lst-mul x y)) wh err))
  226.  
  227.  
  228.  
  229.  (define (net-err+wh err wh) ;add error to wheghts
  230.  
  231.  (map (lambda(x y)(l-err+wh x y)) err wh))
  232.  
  233.  
  234.  
  235.  (define (lst-2-sum a b); adds to lists
  236.  
  237.  (map (lambda(x y)(+ x y)) a b))
  238.  
  239.  
  240.  
  241.  (define (l-err+wh err wh) ;adds error to wheight for layer
  242.  
  243.  (map (lambda(x y)(lst-2-sum x y)) err wh))
  244.  
  245.  
  246.  
  247.  (define (lst-put-num lst) ;ads number to each el. of list ex. (1 a)
  248.  
  249.  (lst-zip (sequence 0 (- (length lst) 1) '() ) lst ))
  250.  
  251.  
  252.  
  253.  (define (sequence from to res) ;creates sequency of numbers from to
  254.  
  255.  (let ( (nr (append res (list from))))
  256.  
  257.  (if (= from to) nr (sequence (+ from 1) to nr ) )))
  258.  
  259.  
  260.  
  261.  (define (lst-order-max lst) ;sort el.
  262.  
  263.  (hsort (lst-put-num lst)))
  264.  
  265.  
  266.  
  267.  (define (lst-zip a b); zip 2 lists
  268.  
  269.  (map (lambda(x y)(list x y)) a b))
  270.  
  271.  
  272.  
  273.  (define (hsort lst) ;sorts
  274.  
  275.  (if (empty? lst) '()
  276.  
  277.  (append
  278.  
  279.  (hsort (filter (lambda(x) (> (cadr x) (cadr (car lst)))) (cdr lst)))
  280.  
  281.  (list (car lst))
  282.  
  283.  (hsort (filter (lambda(x) (<= (cadr x) (cadr (car lst)))) (cdr lst))))))
  284.  
  285.  
  286.  
  287.  (define (ssort lst) ;sort
  288.  
  289.  (if (empty? lst) '()
  290.  
  291.  (append
  292.  
  293.  (ssort (filter (lambda(x) (> x (car lst))) (cdr lst)))
  294.  
  295.  (list (car lst))
  296.  
  297.  (ssort (filter (lambda(x) (<= x (car lst))) (cdr lst))))))
  298.  
  299.  
  300.  
  301.  (define (net-study net lst spd) ;studys net for each example from the list smpl ((inp)(out))
  302.  
  303.  (if (net-check-lst net lst) net
  304.  
  305.  (net-study (net-study-lst net lst spd) lst spd)))
  306.  
  307.  
  308.  
  309.  (define (net-proc-num net inp) ;process network and returns output number
  310.  
  311.  (caar (lst-order-max (net-proc net inp))))
  312.  
  313.  
  314.  
  315.  (define (net-check-lst net lst) ;parbauda sarakstu ar paraugiem
  316.  
  317.  (lst-and (map (lambda(x)(net-check-smpl net x)) lst)))
  318.  
  319.  
  320.  
  321.  (define (lst-and lst) ;accumulates list
  322.  
  323.  (lst-and2 (car lst) (cdr lst)))
  324.  
  325.  
  326.  
  327.  (define (lst-and2 a lst)
  328.  
  329.  (if (empty? lst) a (lst-and2 (and a (car lst)) (cdr lst))))
  330.  
  331.  
  332.  
  333.  (define (net-check-smpl net x) ;checks one sample (input output)
  334.  
  335.  (let ( (inp (car x))
  336.  
  337.  (out (cadr x)) )
  338.  
  339.  (if (= (caar (lst-order-max out)) (net-proc-num net inp)) #t #f)))
  340.  
  341.  
  342.  
  343.  (define (net-study-lst net lst spd); studies network for smaples from lst. Smpl (input output)
  344.  
  345.  (let ( (x (net-study1 net (caar lst) (cadar lst) spd)) )
  346.  
  347.  (if (= 1 (length lst)) x
  348.  
  349.  (net-study-lst x (cdr lst) spd))))
  350.  
  351.  
  352.  
  353.  (define (net-study1 net inp need spd)
  354.  
  355.  (net-study2 net (l-check inp) need spd))
  356.  
  357.  
  358.  
  359.  (define (net-study2 net inp need spd)
  360.  
  361.  (let ( (x (net-study3 net inp need spd)))
  362.  
  363.  (if (= (caar (lst-order-max need))(caar (lst-order-max (net-proc x inp))))
  364.  
  365.  x
  366.  
  367.  (net-study2 x inp need spd))))
  368.  
  369.  
  370.  
  371.  (define (slice lst from to)
  372.  
  373.  (let ( (lng (length lst)) )
  374.  
  375.  (take (drop lst from) (+ (- lng from) to))))
  376.  
  377.  
  378.  
  379.  (define (net-study3 net inp need spd)
  380.  
  381.  (let ((err (net-spd*err spd (slice (net-err net inp need) 1 0)))
  382.  
  383.  (wh (net-inp-wh net))
  384.  
  385.  (out (slice (net-proc-res-out net inp) 0 -1)))
  386.  
  387.  (net-mk-data (net-err+wh wh (map (lambda(x y)(lst-lst-mul x y)) out err)))))
  388.  
  389.  
  390.  
  391.  (define (net-err2 out-lst err-lst wh-lst)
  392.  
  393.  (let ( (err-lst1 (lst-push (l-err (car out-lst) (car err-lst) (car wh-lst)) err-lst)) )
  394.  
  395.  (if (lst-if out-lst wh-lst) err-lst1 (net-err2 (cdr out-lst) err-lst1 (cdr wh-lst)))))
  396.  
  397.  
  398.  
  399.  (define (net-err net inp need) ;networks error list for each neuron
  400.  
  401.  (let ( (wh-lst (reverse (net-out-wh net)))
  402.  
  403.  (err-lst (list (net-out-err net inp need)))
  404.  
  405.  (out-lst (reverse (slice (net-proc-res-out net inp) 0 -1))))
  406.  
  407.  (net-err2 out-lst err-lst wh-lst)))
  408.  
  409.  (define (net-spd*err spd err) ;multiply error with speed
  410.  
  411.  (map (lambda(x)(lst-mul-x spd x)) err))
  412.  
  413.  
  414.  
  415.  (define (lst-mul-x s lst) ;multiply list with x
  416.  
  417.  (map (lambda(x)(* s x)) lst))
  418.  
  419.  
  420.  
  421.  (define (net-proc-res-out net inp) ;x*(1 — x) for each neuron output
  422.  
  423.  (lst-push inp (map (lambda(x)(l-proc-res-out x)) (net-proc-res net inp))))
  424.  
  425.  
  426.  
  427.  (define (l-proc-res-out lst)
  428.  
  429.  (map (lambda(x)(* x (- 1 x))) lst))
  430.  
  431.  
  432.  
  433.  (define (l-out-err need fact) ;error of otput layer
  434.  
  435.  (map (lambda(x y)(n-out-err x y)) need fact))
  436.  
  437.  
  438.  
  439.  (define (n-out-err need fact) ;error of output neuron
  440.  
  441.  (* fact (- 1 fact)(- need fact)))
  442.  
  443.  
  444.  
  445.  (define (net-out-err net inp need) ;networks error lists
  446.  
  447.  (l-out-err need (net-proc net inp)))
  448.  
  449.  
  450.  
  451.  (define (lst-print lst);prints list
  452.  
  453.  (map (lambda(x)(and (newline)(display x))) lst)(newline))
  454.  
  455.  
  456.  
  457.  (define (lst-print2 lst)
  458.  
  459.  (map (lambda(x)(lst-print x)) lst)(newline))
  460.  
  461.  
  462.  
  463.  (define (l-check lst)
  464.  
  465.  lst)
  466.  
  467.  
  468.  
  469.  (define (transpose lst res)
  470.  
  471.  (if (empty? (car lst)) res
  472.  
  473.  (transpose (lst-cdr lst) (append res (lst-car lst)))))
  474.  
  475.  
  476.  
  477.  (define (lst-cdr lst) ;cdr of all list elements
  478.  
  479.  (map (lambda(x)(cdr x)) lst))
  480.  
  481.  
  482.  
  483.  (define (lst-car lst) ;car of all list elements
  484.  
  485.  (list (map (lambda(x)(car x)) lst)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement