• Stars
    star
    178
  • Rank 207,710 (Top 5 %)
  • Language
    Crystal
  • License
    MIT License
  • Created over 6 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

SHAInet - a pure Crystal machine learning library

shainet

Crystal CI

SHAInet - stands for Super Human Artificial Intelligence network a neural network in pure Crystal

This is a free-time project, happily hosted by NeuraLegion that was created as part of some internal research. We started it with research in mind, rather than production, and just kept going, also thanks to members of the community.

We wanted to try and implement some inspiration from the biological world into this project. In addition to that, we wanted to try an approach for NNs using object-oriented modeling instead of matrices. The main reason behind that was, to try new types of neurons aiming for more robust learning (if possible) or at least have more fine-tuned control over the manipulation of each neuron (which is difficult using a matrix-driven approach).

At the Roadmap you can see what we plan to add to the network as the project will progress.

Installation

Add this to your application's shard.yml:

dependencies:
  shainet:
    github: NeuraLegion/shainet

Usage

More usage examples can be found in the specs

Standard training on XOR example

require "shainet"

training_data = [
  [[0, 0], [0]],
  [[1, 0], [1]],
  [[0, 1], [1]],
  [[1, 1], [0]],
]
# Initialize a new network
xor = SHAInet::Network.new
# Add a new layer of the input type with 2 neurons and classic neuron type (memory)
xor.add_layer(:input, 2, :memory, SHAInet.sigmoid)
# Add a new layer of the hidden type with 2 neurons and classic neuron type (memory)
xor.add_layer(:hidden, 2, :memory, SHAInet.sigmoid)
# Add a new layer of the output type with 1 neurons and classic neuron type (memory)
xor.add_layer(:output, 1, :memory, SHAInet.sigmoid)
# Fully connect the network layers
xor.fully_connect

# Adjust network parameters
xor.learning_rate = 0.7
xor.momentum = 0.3

# data, training_type, cost_function, activation_function, epochs, error_threshold (sum of errors), learning_rate, momentum)
xor.train(
      data: training_data,
      training_type: :sgdm,
      cost_function: :mse,
      epochs: 5000,
      error_threshold: 0.000001,
      log_each: 1000)

# Run the trained network
xor.run([0, 0])

Batch training on the iris dataset using adam

# Create a new Data object based on a CSV
data = SHAInet::Data.new_with_csv_input_target("iris.csv", 0..3, 4)

# Split the data in a training set and a test set
training_set, test_set = data.split(0.67)

# Initiate a new network
iris = SHAInet::Network.new

# Add layers
iris.add_layer(:input, 4, :memory, SHAInet.sigmoid)
iris.add_layer(:hidden, 5, :memory, SHAInet.sigmoid)
iris.add_layer(:output, 3, :memory, SHAInet.sigmoid)
iris.fully_connect

# Adjust network parameters
xor.learning_rate = 0.7
xor.momentum = 0.3

# Train the network
iris.train_batch(
      data: normalized.data.shuffle,
      training_type: :adam,
      cost_function: :mse,
      epochs: 20000,
      error_threshold: 0.000001,
      log_each: 1000)

# Test the network's performance
iris.test(test_set)

Using convolutional network

# Load training data (partial dataset)
raw_data = Array(Array(Float64)).new
csv = CSV.new(File.read(__DIR__ + "/test_data/mnist_train.csv"))
10000.times do
  # CSV.each_row(File.read(__DIR__ + "/test_data/mnist_train.csv")) do |row|
  csv.next
  new_row = Array(Float64).new
  csv.row.to_a.each { |value| new_row << value.to_f64 }
  raw_data << new_row
end
raw_input_data = Array(Array(Float64)).new
raw_output_data = Array(Array(Float64)).new

raw_data.each do |row|
  raw_input_data << row[1..-1]
  raw_output_data << [row[0]]
end

training_data = SHAInet::CNNData.new(raw_input_data, raw_output_data)
training_data.for_mnist_conv
training_data.data_pairs.shuffle!

# Load test data (partial dataset)
raw_data = Array(Array(Float64)).new
csv = CSV.new(File.read(__DIR__ + "/test_data/mnist_test.csv"))
1000.times do
  csv.next
  new_row = Array(Float64).new
  csv.row.to_a.each { |value| new_row << value.to_f64 }
  raw_data << new_row
end

raw_input_data = Array(Array(Float64)).new
raw_output_data = Array(Array(Float64)).new

raw_data.each do |row|
  raw_input_data << row[1..-1]
  raw_output_data << [row[0]]
end

# Load data to a CNNData helper class
test_data = SHAInet::CNNData.new(raw_input_data, raw_output_data)
test_data.for_mnist_conv # Normalize and make labels into 'one-hot' vectors

# Initialize Covnolutional network
cnn = SHAInet::CNN.new

# Add layers to the model
cnn.add_input([height = 28, width = 28, channels = 1]) # Output shape = 28x28x1
cnn.add_conv(
  filters_num: 20,
  window_size: 5,
  stride: 1,
  padding: 2,
  activation_function: SHAInet.none)  # Output shape = 28x28x20
cnn.add_relu(0.01)                    # Output shape = 28x28x20
cnn.add_maxpool(pool: = 2, stride = 2) # Output shape = 14x14x20
cnn.add_conv(
  filters_num: 20,
  window_size: 5,
  stride: 1,
  padding: 2,
  activation_function: SHAInet.none)  # Output shape = 14x14x40
cnn.add_maxpool(pool:2, stride: 2)    # Output shape = 7x7x40
cnn.add_fconnect(l_size: 10, activation_function: SHAInet.sigmoid)
cnn.add_fconnect(l_size: 10, activation_function: SHAInet.sigmoid)
cnn.add_softmax

cnn.learning_rate = 0.005
cnn.momentum = 0.02

# Train the model on the training-set
cnn.train_batch(
  data: training_data.data_pairs,
  training_type: :sgdm,
  cost_function: :mse,
  epochs: 3,
  error_threshold: 0.0001,
  log_each: 1,
  mini_batch_size: 50)

# Evaluate accuracy on the test-set
correct_answers = 0
test_data.data_pairs.each do |data_point|
  result = cnn.run(data_point[:input], stealth: true)
  if (result.index(result.max) == data_point[:output].index(data_point[:output].max))
    correct_answers += 1
  end
end

# Print the layer activations
cnn.inspect("activations")
puts "We managed #{correct_answers} out of #{test_data.data_pairs.size} total"
puts "Cnn output: #{cnn.output}"

Evolutionary optimizer example:

label = {
      "setosa"     => [0.to_f64, 0.to_f64, 1.to_f64],
      "versicolor" => [0.to_f64, 1.to_f64, 0.to_f64],
      "virginica"  => [1.to_f64, 0.to_f64, 0.to_f64],
    }

    iris = SHAInet::Network.new
    iris.add_layer(:input, 4, :memory, SHAInet.sigmoid)
    iris.add_layer(:hidden, 4, :memory, SHAInet.sigmoid)
    iris.add_layer(:output, 3, :memory, SHAInet.sigmoid)
    iris.fully_connect

    # Get data from a local file
    outputs = Array(Array(Float64)).new
    inputs = Array(Array(Float64)).new
    CSV.each_row(File.read(__DIR__ + "/test_data/iris.csv")) do |row|
      row_arr = Array(Float64).new
      row[0..-2].each do |num|
        row_arr << num.to_f64
      end
      inputs << row_arr
      outputs << label[row[-1]]
    end
    data = SHAInet::TrainingData.new(inputs, outputs)
    data.normalize_min_max

    training_data, test_data = data.split(0.9)

    iris.train_es(
      data: training_data,
      pool_size: 50,
      learning_rate: 0.5,
      sigma: 0.1,
      cost_function: :c_ent,
      epochs: 500,
      mini_batch_size: 15,
      error_threshold: 0.00000001,
      log_each: 100,
      show_slice: true)

    # Test the trained model
    correct = 0
    test_data.data.each do |data_point|
      result = iris.run(data_point[0], stealth: true)
      expected = data_point[1]
      # puts "result: \t#{result.map { |x| x.round(5) }}"
      # puts "expected: \t#{expected}"
      error_sum = 0.0
      result.size.times do |i|
        error_sum += (result[i] - expected[i]).abs
      end
      correct += 1 if error_sum < 0.3
    end
    puts "Correct answers: (#{correct} / #{test_data.size})"
    (correct > 10).should eq(true)

Development

Basic Features

  • Train network
  • Save/load
  • Activation functions:
    • Sigmoid
    • Bipolar sigmoid
    • log-sigmoid
    • Tanh
    • ReLU
    • Leaky ReLU
    • Softmax
  • Cost functions:
    • Quadratic
    • Cross-entropy
  • Gradient optimizers
    • SGD + momentum
    • iRprop+
    • ADAM
    • ES (evolutionary strategy, non-backprop)
  • Autosave during training

Advanced Features

  • Support activation functions as Proc
  • Support cost functions as Proc
  • Convolutional Neural Net.
  • Add support for multiple neuron types.
  • Bind and use CUDA (GPU acceleration)
  • graphic printout of network architecture.

Possible Future Features

  • RNN (recurant neural network)
  • LSTM (long-short term memory)
  • GNG (growing neural gas).
  • SOM (self organizing maps).
  • DBM (deep belief network).

Contributing

  1. Fork it ( https://github.com/NeuraLegion/shainet/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

More Repositories

1

brokencrystals

A Broken Application - Very Vulnerable!
CSS
103
star
2

crystal-fann

FANN (Fast Artifical Neural Network) binding in Crystal
Crystal
80
star
3

cypress-har-generator

Cypress plugin for generating HTTP Archive (HAR) files captures network requests during Cypress tests.
TypeScript
75
star
4

wafalyzer

Web Application Firewall (WAF) Detector
Crystal
32
star
5

har

HAR (HTTP Archive) parser in Crystal
Crystal
22
star
6

broken_crystals

A vulnerable Application in crystal
Crystal
20
star
7

sslscanner

SSL Scanner written in Crystal
Crystal
17
star
8

bright-cli

Command Line Interface (CLI) tool for NeuraLegion's solutions.
TypeScript
16
star
9

sectester-js-demo

This is a demo project for the SecTester JS SDK framework, with some installation and usage examples.
TypeScript
16
star
10

sec-tester-cr

Bright Security scanner integration for Crystal specs
Crystal
14
star
11

tensorflow.cr

TensorFlow bindings in Crystal
Crystal
13
star
12

cvss

The Common Vulnerability Scoring System (CVSS) base score calculator and validator library written in TypeScript.
TypeScript
13
star
13

fix

FIX library for crystal
Crystal
13
star
14

blue_cr

BlueZ bluetooth binding in Crystal
Crystal
12
star
15

sslscan.cr

Crystal shard wrapping the rbsec/sslscan utility
Crystal
10
star
16

generic_actor

A generic Actor model for Crystal
Crystal
10
star
17

har-sdk

HAR SDK enables Node.js developers to easily work with HAR, convert it from OAS/Swagger and Postman collection, and more.
TypeScript
9
star
18

mt_helpers

MultiThreading helpers for crystal
Crystal
8
star
19

example-actions

8
star
20

monitor

Quick HTTP Load tester for CSE\Support
Crystal
5
star
21

sectester-js

SecTester is a new tool that integrates our enterprise-grade scan engine directly into your unit tests.
TypeScript
4
star
22

documentation

NeuraLegion's Documentation Repository
4
star
23

simple-email

Crystal
3
star
24

jfrog-example

3
star
25

changelog

NexPloit Changelog
2
star
26

run-scan

Action runs a new security scan in NeuraLegion, or reruns an existing one
TypeScript
2
star
27

AMASS-the-Legion

Python
2
star
28

crometheus

Crystal
2
star
29

oas2har

Transform you Swagger/OAS spec files to a series of HAR request objects.
JavaScript
2
star
30

socket_io

Crystal SocketIO Shard
Crystal
2
star
31

postman2har

Transform your Postman collection to a series of HAR request objects.
TypeScript
2
star
32

broken_crystals_kemal

Temporary backup of Broken Crystals written in Kemal
HTML
1
star
33

restharter

Restart a scan using a HAR
Crystal
1
star
34

sectester-net

SecTester is a new tool that integrates our enterprise-grade scan engine directly into your unit tests.
C#
1
star
35

helmcharts

Smarty
1
star