Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package zadanie2;
  6.  
  7. import Warstwy.WielowarstwowyPerceptron;
  8. import java.text.NumberFormat;
  9. import java.util.ArrayList;
  10.  
  11. /**
  12.  *
  13.  * @author wespit
  14.  */
  15. public class Main {
  16.  
  17.     /**
  18.      * @param args the command line arguments
  19.      */
  20.     public static void main(String[] args) {
  21.  
  22.         float krokNauki = 0.7f; // krok nauki
  23.         float momentum = 0.5f; // momentu - jak nie  ma być używane to dać na 0.0f
  24.         boolean bias = false; // czy stosować bias
  25.         float blad = 0;
  26.  
  27.         int[] liczbaNeuronkow = new int[]{4, 2, 4}; // liczby neurownow w poszczegolnych warstwach
  28.  
  29.         ArrayList<Float> wejscie = new ArrayList<Float>(); // dane wejsciowe
  30.         wejscie.add(1.0f);
  31.         wejscie.add(0.0f);
  32.         wejscie.add(0.0f);
  33.         wejscie.add(0.0f);
  34.  
  35.         ArrayList<Float> wyjscie = new ArrayList<Float>(); // dane wyjsciowe
  36.         wyjscie.add(1.0f);
  37.         wyjscie.add(0.0f);
  38.         wyjscie.add(0.0f);
  39.         wyjscie.add(0.0f);
  40.  
  41.         // liczba warstw, tablica z liczba neronow w kazdej warswie , czy  stosowac bias
  42.         WielowarstwowyPerceptron perceptron = new WielowarstwowyPerceptron(3, liczbaNeuronkow, false);
  43.  
  44.         float[] wyjscieV = null;
  45.  
  46.  
  47.         for (int i = 0; i < 40; i++) {
  48.             perceptron.perceptronWejscie(wejscie); // wrzucenie danych wejsciowych na perceptron
  49.  
  50.             perceptron.liczSumy(); // liczy sumy kazdej warstwy
  51.  
  52.             perceptron.liczDelty(wyjscie); // propaguje blad wstecz i liczy delty
  53.  
  54.             wyjscieV = perceptron.liczWyjscia(); // liczy wyjscie
  55.  
  56.             perceptron.zmienWagi(krokNauki, momentum); // zmienia wagi po propagacji wstecznej
  57.  
  58.         }
  59.  
  60.         for (int k = 0; k < wyjscieV.length; k++) {
  61.             blad = blad + (wyjscieV[k] - wyjscie.get(k)) * (wyjscieV[k] - wyjscie.get(k));
  62.         }
  63.  
  64.         NumberFormat nf = NumberFormat.getInstance();
  65.         nf.setMinimumFractionDigits(3);
  66.  
  67.         System.out.println("########################");
  68.         for (int i = 1; i < perceptron.warstwy.size(); i++) {
  69.             System.out.println("Warstwa " + (i + 1));
  70.             System.out.println("########################");
  71.             for (int j = 0; j < perceptron.warstwy.get(i).neuronki.size(); j++) {
  72.                 System.out.println("Otrzymane wagi dla neuronu " + (j + 1));
  73.                 for (int m = 0; m < perceptron.warstwy.get(i).neuronki.get(j).wagi.length; m++) {
  74.                     System.out.print(String.valueOf(nf.format(perceptron.warstwy.get(i).neuronki.get(j).wagi[m])) + " ");
  75.                 }
  76.                 System.out.println();
  77.             }
  78.             System.out.println("########################");
  79.         }
  80.  
  81.         for (int i = 0; i < wyjscieV.length; i++) {
  82.             System.out.println("wyjscie " + i + " : " + wyjscieV[i]);
  83.         }
  84.  
  85.         blad = blad * (float) (1.0 / 2.0);
  86.         System.out.println("blad: " + nf.format(blad));
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement