📜 ⬆️ ⬇️

Fast neural network for everyone

This article will demonstrate the ability to easily write your neural network in Java. In order not to reinvent the wheel, take the already well-developed library of the Fast Artificial Neural Network. Using neural networks in their Java projects is real. You can often hear accusations against Java regarding execution speed. Although the difference is not so great - you can learn more about this in the publication “C ++ vs. Performance. Java vs. PHP vs. Python. Test "in the forehead." We will use a wrapper around the FANN library.

Task


You need to write a system that can make decisions for a character who can meet one or more enemies. The system may be aware of:

The answer should be in the form of one of the actions:

For learning we will make a table of "lessons":
HealthpistolThe enemiesAct
50%oneoneAttack
90%one2Attack
80%0oneAttack
thirty%oneoneHide
60%one2Hide
40%0oneHide
90%one7To run
60%onefourTo run
ten%0oneTo run
60%one0Nothing
100%00Nothing

Training


The first thing to do is build and install libfann .
Then download fannj and jna .

Make a file that will contain a set of "lessons":

11 3 4 0.5 1 1 1 0 0 0 0.9 1 2 1 0 0 0 0.8 0 1 1 0 0 0 0.3 1 1 0 1 0 0 0.6 1 2 0 1 0 0 0.4 0 1 0 1 0 0 0.9 1 7 0 0 1 0 0.5 1 4 0 0 1 0 0.1 0 1 0 0 1 0 0.6 1 0 0 0 0 1 1.0 0 0 0 0 0 1 

Now we will train our ANN and save it to a file:
')
  public static void main(String[] args) { //        List<Layer> layerList = new ArrayList<Layer>(); layerList.add(Layer.create(3, ActivationFunction.FANN_SIGMOID_SYMMETRIC, 0.01f)); layerList.add(Layer.create(16, ActivationFunction.FANN_SIGMOID_SYMMETRIC, 0.01f)); layerList.add(Layer.create(4, ActivationFunction.FANN_SIGMOID_SYMMETRIC, 0.01f)); Fann fann = new Fann(layerList); //      Trainer trainer = new Trainer(fann); trainer.setTrainingAlgorithm(TrainingAlgorithm.FANN_TRAIN_RPROP); /*      ,     100000,    100      0.0001 */ trainer.train(new File("train.data").getAbsolutePath(), 100000, 100, 0.0001f); fann.save("ann"); } 

Explanation


Layer


ANN consists of layers of neurons. The first layer is the “receptor” neurons or input neurons. The last layer of neuron output. All the others are hidden layers. In our case, the first layer has 3 neurons:

health level (0.1-1.0);
the presence of weapons (1-there, 0-no);
number of enemies.

Fann


The object of the class Fann is the neural network, which is created on the basis of the layers created earlier.

Trainer


The class object trainer encapsulates the neural network learning algorithms transmitted during the creation of the trainer. After learning, do not forget to save it to a file.

Checking results


To test our training, we use the following code:

  public static void main(String[] args) { Fann fann = new Fann("ann"); float[][] tests = { {1.0f, 0, 1}, {0.9f, 1, 3}, {0.3f, 0, 8}, {1, 1, 8}, {0.1f, 0, 0}, }; for (float[] test:tests){ System.out.println(getAction(fann.run(test))); } } private static String getAction(float[] out){ int i = 0; for (int j = 1; j < 4; j++) { if(out[i]<out[j]){ i = j; } } switch (i){ case 0:return ""; case 1:return ""; case 2:return ""; case 3:return "  "; } return ""; } 

I got the following results:
HealthpistolThe enemiesAct
100%NotoneAttack
90%there is3Hide
thirty%noteightTo run
100%there iseightTo run
ten%Not0Nothing to do

I will be glad to hear constructive criticism.

Source: https://habr.com/ru/post/254921/


All Articles