ai 0.1.8

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 55

Library for creating AI with Dart #

Created under a MIT-style license.

Overview #

This library represent an simple way to create neural network.

There are 2 types of neural network that can be created:

  • MLP(multilayer and single-layer perceptron).
  • AE(autoencoder).

API #

MLP #

Perseptron is designed due to Rosenblatt's perseptron.

The main class is the MLP which can contains Layers. Each layer consist of one or many Neurons. First layer always must consist of input neurons where each of them take one input value and have weight equal to 1. All neurons of previous layer have contacts with each neurons of next layer.

In learning is used backpropagation algorithm.

AE #

Architecture of AE is the same as MLP, except that first is used for encoding data.

Memory #

Neural network have long-time and short-time memory. All information (knowledge - weights of synapces) of neural network during studying pass through short-time memory. When studying finished and knowledge is structured, then it pass to long-time memory. Knowledge is saved in JSON file knowledge.json in resources directory in the root of your library. For next time network take knowledge from file and initialize with proper weights.

Structure #

Neural network can be created from predefined structure difined in structure.json file. You can place it anywhere you want, but default and preffered way is placing it in resources directory in the root of your library.

Every structure.json must have type property that corresspond to neural network's name. Other properties such as:

- activation: specifies which function are used for neuron activation (default sigmoid)
- momentum: describe step of gradient descent (default 1)
- bias: is limit of neuron's choice. Used only in `step` function
- hyperparameter: is used in `PReLU`, `RReLU` and `ELU`. For `RReLU` it is a random number sampled from a uniform distribution `𝑈(𝑙, u)`, for `PReLU` it is a random value and for `ELU` it is random value that is equal or greater than zero

are optional. Specific properties for each structure type should be provided, except there are optional ones.

Structure of MLP:

{
  "type": "MLP",
  "activation": "relu",
  "input": 15,
  "hiddens": [3],
  "output": 3
}

Where input - count of input neurons, hiddens - array length shows count of hidden Layers and values are count of Neurons of each layer, output - count of output Neurons.

Structure of AE:

{
  "type": "AE",
  "input": 15,
  "hiddens": [3],
  "encoded": 3
}

Where input - count of input neurons, hiddens - array length shows count of hidden Layers and values are count of Neurons of each layer for encoded and decoded parts, decoded - count of decoded data Neurons.

Activation functions #

Supported 9 activation functions:

1. step
2. sigmoid (default)
3. tanh
4. relu
5. srelu (smooth relu or softplus)
6. lrelu (leaky relu)
7. prelu (parametric relu)
8. rrelu (randomized relu)
9. elu (exponential linear units)

Visualization #

Visualization of process of training network is available. Implemented only Mean Squared Error (MSE). If visualise paramenter of train() method is true then MSE sends to console.

Sample #

This testing network recognize number 5 from numbers in range from 0 to 9. Also she detects distorted numbers of 5.

Numbers

import 'package:ai/ai.dart';

void main() {

  final l1 = Layer(<Neuron>[
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true)
  ]);
  final l2 = Layer(<Neuron>[
    Neuron(15),
    Neuron(15),
    Neuron(15),
    Neuron(15),
    Neuron(15)
  ]);
  final l3 = Layer(<Neuron>[
    Neuron(5)
  ]);

  final n = MLP.from(Structure());

  // Expected results according to learning data (10)
  final expected = <List<double>>[
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.99], // 5
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.01]
  ];

  // Цифры (Обучающая выборка)
  final trainInput = <List<double>>[
    '111101101101111'.split('').map(double.parse).toList(),
    '001001001001001'.split('').map(double.parse).toList(),
    '111001111100111'.split('').map(double.parse).toList(),
    '111001111001111'.split('').map(double.parse).toList(),
    '101101111001001'.split('').map(double.parse).toList(),
    '111100111001111'.split('').map(double.parse).toList(), // 5
    '111100111101111'.split('').map(double.parse).toList(),
    '111001001001001'.split('').map(double.parse).toList(),
    '111101111101111'.split('').map(double.parse).toList(),
    '111101111001111'.split('').map(double.parse).toList()
  ];

  // Виды цифры 5 (Тестовая выборка)
  final testInput = <List<double>>[
    '111100111000111'.split('').map(double.parse).toList(),
    '111100010001111'.split('').map(double.parse).toList(),
    '111100011001111'.split('').map(double.parse).toList(),
    '110100111001111'.split('').map(double.parse).toList(),
    '110100111001011'.split('').map(double.parse).toList(),
    '111100101001111'.split('').map(double.parse).toList()
  ];

  final num5 = '111100111001111'.split('').map(double.parse).toList();

  n.train(input: trainInput, expected: expected, learningRate: 0.1, epoch: 5000, visualize: true);

  print('Узнал 5? - ${n.predict(num5)}');
  for (var item in testInput) {
    print('Узнал искаженную 5? - ${n.predict(item)[0]}');
  }
  print('А 0? - ${n.predict(trainInput[0])}');
  print('А 8? - ${n.predict(trainInput[8])}');
  print('А 3? - ${n.predict(trainInput[3])}');

}

Features and bugs #

Please file feature requests and bugs at the issue tracker.

With ❤️ to AI

0.1.8 #

  • Remove InputNeuron and NeuronBase.
  • Change signature of Layer class.
  • Add 4 properties to Structure class.
  • Change MLP and AE classes to set up with more properties.
  • Complement README.

0.1.7 #

  • Add visualization (MSE) to network's training.

0.1.6 #

  • Change MLP.withLayers() constructor to MLP().
  • Add Layer.construct constructor.
  • Add AE neural network.
  • Edit README.
  • Change examples.

0.1.5 #

  • Remove NetworkBase class.
  • Add Structure, MlpStructure entities.
  • Rename MultilayerPerceptron to MLP.
  • Add MLP.fromStructure() constructor and create MLP.withLayers() constructor.
  • Fix train() method of MLP class.
  • Implroved README.
  • Fix bug in example.

0.1.4 #

  • Downgrade required Dart SDK to 2.1.0-dev.9.4.

0.1.3 #

  • Fix internal createFile() method for ...Memory class.
  • Change docs in ShortMemory class.

0.1.2 #

  • Fix expected parameter of train() method of MultilayerPerceptron to accept list of expected output.

0.1.1 #

  • Add MemoryBase class.
  • Add description to package.

0.1.0 #

  • Initial release.
  • Implement MultilayerPerseptron class.

example/README.md

Examples #

MLP #

import 'package:ai/ai.dart';

void main() {
  
  final l1 = Layer(<Neuron>[
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true),
    Neuron(0, isInput: true)
  ]);
  final l2 = Layer(<Neuron>[
    Neuron(15),
    Neuron(15),
    Neuron(15),
    Neuron(15),
    Neuron(15)
  ]);
  final l3 = Layer(<Neuron>[
    Neuron(5)
  ]);

  final n = MLP.from(Structure());

  // Expected results according to learning data (10)
  final expected = <List<double>>[
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.99], // 5
    <double>[0.01],
    <double>[0.01],
    <double>[0.01],
    <double>[0.01]
  ];

  // Цифры (Обучающая выборка)
  final trainInput = <List<double>>[
    '111101101101111'.split('').map(double.parse).toList(),
    '001001001001001'.split('').map(double.parse).toList(),
    '111001111100111'.split('').map(double.parse).toList(),
    '111001111001111'.split('').map(double.parse).toList(),
    '101101111001001'.split('').map(double.parse).toList(),
    '111100111001111'.split('').map(double.parse).toList(), // 5
    '111100111101111'.split('').map(double.parse).toList(),
    '111001001001001'.split('').map(double.parse).toList(),
    '111101111101111'.split('').map(double.parse).toList(),
    '111101111001111'.split('').map(double.parse).toList()
  ];

  // Виды цифры 5 (Тестовая выборка)
  final testInput = <List<double>>[
    '111100111000111'.split('').map(double.parse).toList(),
    '111100010001111'.split('').map(double.parse).toList(),
    '111100011001111'.split('').map(double.parse).toList(),
    '110100111001111'.split('').map(double.parse).toList(),
    '110100111001011'.split('').map(double.parse).toList(),
    '111100101001111'.split('').map(double.parse).toList()
  ];

  final num5 = '111100111001111'.split('').map(double.parse).toList();

  n.train(input: trainInput, expected: expected, learningRate: 0.1, epoch: 5000, visualize: true);

  print('Узнал 5? - ${n.predict(num5)}');
  for (var item in testInput) {
    print('Узнал искаженную 5? - ${n.predict(item)[0]}');
  }
  print('А 0? - ${n.predict(trainInput[0])}');
  print('А 8? - ${n.predict(trainInput[8])}');
  print('А 3? - ${n.predict(trainInput[3])}');

}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  ai: ^0.1.8

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:ai/ai.dart';
  
Version Uploaded Documentation Archive
0.1.10 May 10, 2019 Go to the documentation of ai 0.1.10 Download ai 0.1.10 archive
0.1.9 Mar 14, 2019 Go to the documentation of ai 0.1.9 Download ai 0.1.9 archive
0.1.8+1 Mar 3, 2019 Go to the documentation of ai 0.1.8+1 Download ai 0.1.8+1 archive
0.1.8 Feb 14, 2019 Go to the documentation of ai 0.1.8 Download ai 0.1.8 archive
0.1.7 Feb 1, 2019 Go to the documentation of ai 0.1.7 Download ai 0.1.7 archive
0.1.6 Jan 26, 2019 Go to the documentation of ai 0.1.6 Download ai 0.1.6 archive
0.1.5 Jan 25, 2019 Go to the documentation of ai 0.1.5 Download ai 0.1.5 archive
0.1.4 Jan 17, 2019 Go to the documentation of ai 0.1.4 Download ai 0.1.4 archive
0.1.3 Jan 14, 2019 Go to the documentation of ai 0.1.3 Download ai 0.1.3 archive
0.1.2 Jan 13, 2019 Go to the documentation of ai 0.1.2 Download ai 0.1.2 archive

All 11 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
44
Health:
Code health derived from static analysis. [more]
42
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
55
Learn more about scoring.

We analyzed this package on May 8, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.3.0
  • pana: 0.12.15

Platforms

Detected platforms: Flutter, other

Primary library: package:ai/ai.dart with components: io.

Health issues and suggestions

Fix lib/src/neuron.dart. (-43.75 points)

Analysis of lib/src/neuron.dart failed with 2 errors:

line 50 col 34: Too many positional arguments: 0 expected, but 1 found.

line 100 col 37: The method 'dotProduct' isn't defined for the class 'Vector'.

Fix lib/src/visualization/visualization.dart. (-25 points)

Analysis of lib/src/visualization/visualization.dart failed with 1 error:

line 23 col 20: The method 'Mean' isn't defined for the class 'Visualization'.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0-dev.9.4 <3.0.0
extended_math ^0.0.20 0.0.28
json_annotation ^2.0.0 2.2.0
meta ^1.1.7 1.1.7
Transitive dependencies
matcher 0.12.5
path 1.6.2
quiver 2.0.3
stack_trace 1.9.3
Dev dependencies
build_runner ^1.0.0
json_serializable ^2.0.1
test ^1.5.3