ai 0.1.10

  • Readme
  • Changelog
  • Example
  • Installing
  • 49

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).



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.


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


Neural network have long-time and short-time memory. All information (knowledge - weights of synapses) 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.


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

Every structure.json must have type property that correspond 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 of process of training network is available. Implemented only Mean Squared Error (MSE). If visualize parameter of train() method is true then MSE sends to console.

Sample #

Available here.

Features and bugs #

Please file feature requests and bugs at the issue tracker.

With ❤️ to AI

0.1.10 #

  • Add resources directory with structure.json file (for example).
  • Fix typos in README.

0.1.9 #

  • Small fixes in classes.
  • Fix problems related to upgrade of extended_math package.

0.1.8+1 #

  • Fix problems related to upgrade of extended_math package.

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.


Examples #

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



import 'package:ai/ai.dart';

void main() {
  final n = MLP.from(Structure());

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

  // Digits (Learning set)
  final trainInput = <List<double>>[
    '111100111001111'.split('').map(double.parse).toList(), // 5

  // Digits 5 (Test set)
  final testInput = <List<double>>[

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

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

  print('Recognize 5? - ${n.predict(num5)}');
  for (var item in testInput) {
    print('Recognize distorted 5? - ${n.predict(item)[0]}');
  print('Аnd 0? - ${n.predict(trainInput[0])}');
  print('Аnd 8? - ${n.predict(trainInput[8])}');
  print('Аnd 3? - ${n.predict(trainInput[3])}');


Use this package as a library

1. Depend on it

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

  ai: ^0.1.10

2. Install it

You can install packages from the command line:

with pub:

$ pub get

Alternatively, your editor might support 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';
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Apr 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health issues and suggestions

Fix lib/src/network/cnn.dart. (-44.03 points)

Analysis of lib/src/network/cnn.dart failed with 2 errors, 1 hint:

line 19 col 37: Too many positional arguments: 3 expected, but 4 found.

line 20 col 65: The getter 'RGB' isn't defined for the class 'Image'.

line 26 col 7: The declaration '_fixOutOfRangeRGBValues' isn't referenced.

Maintenance issues and suggestions

No valid SDK. (-20 points)

The analysis could not detect a valid SDK that can use this package.

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (json_annotation).

The package description is too short. (-1 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0-dev.9.4 <3.0.0
extended_math ^0.0.26 0.0.29+1
image ^2.0.7 2.1.12
json_annotation ^2.0.0 2.4.0 3.0.1
meta ^1.1.6 1.1.8
Transitive dependencies
archive 2.0.13
args 1.6.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
matcher 0.12.6
path 1.6.4
petitparser 3.0.2
quiver 2.1.3
stack_trace 1.9.3
typed_data 1.1.6
xml 3.7.0 4.1.0
Dev dependencies
build_runner ^1.0.0
json_serializable ^2.0.1
test ^1.5.3