perceptron 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 60ceb2ef63de81f5bc44349f12f94786a35ac98e
4
+ data.tar.gz: aa059cd75f7cdcd44340653012beec6e1aedd339
5
+ SHA512:
6
+ metadata.gz: a520b10e37f1d3e703e33af3a46e7a8338692da776ccea918ba709643c4eed8fb24d0e58146f45b6b2612c2c4965c129af162c9353861f0b5c4c990b4e0a850d
7
+ data.tar.gz: c56e29eb4ebf58888bd356d78b64feb18be01efe7594d747ed974ef09f6a1fd1afc1f178e080597b59135ab23a6fbb4d00dc6f4c60d2febcb9c18ce6600a2d38
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in perceptron.gemspec
6
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 terminalobject
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,42 @@
1
+ # Perceptron
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/perceptron`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'perceptron'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install perceptron
22
+
23
+ ## Usage
24
+
25
+ Instantiate a new Perceptron with the desired number of features to track. The perceptron accepts an array of hashes of the form {:vector => Vector[...], :expected => number} as training input.
26
+ ```ruby
27
+ Perceptron.create(features_number)
28
+ ```
29
+
30
+ ## Development
31
+
32
+ After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
33
+
34
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
35
+
36
+ ## Contributing
37
+
38
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/perceptron.
39
+
40
+ ## License
41
+
42
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "perceptron"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/lib/perceptron.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'matrix'
2
+
3
+ class Perceptron
4
+ def self.create(features_number)
5
+ @unit = Unit.new(features_number, Weight.new(features_number))
6
+ end
7
+ end
8
+
9
+ require_relative './perceptron/unit'
10
+ require_relative './perceptron/weight'
11
+
@@ -0,0 +1,36 @@
1
+ require 'matrix'
2
+ class Perceptron::Unit
3
+
4
+ attr_reader :weight
5
+
6
+ def initialize(features_number, weight = Weight.new(features_number))
7
+ @weight = weight
8
+ end
9
+
10
+ def predict(vector)
11
+ scalar_product(@weight.vector, vector) > 0 ? 1 : 0
12
+ end
13
+
14
+ def train(hash)
15
+ learn(hash) unless calculate_error(hash).zero?
16
+ end
17
+
18
+ private
19
+
20
+ def scalar_product(vector_1, vector_2)
21
+ vector_1.inner_product vector_2
22
+ end
23
+
24
+ def calculate_error(hash)
25
+ hash[:expected] - predict(Vector[*hash[:vector]])
26
+ end
27
+
28
+ def learn(hash)
29
+ @weight.update(updated_weight_arr(hash))
30
+ end
31
+
32
+ def updated_weight_arr(hash)
33
+ (@weight.vector + Vector[*hash[:vector]] * calculate_error(hash)).to_a
34
+ end
35
+ end
36
+
@@ -0,0 +1,3 @@
1
+ module Perceptron
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,31 @@
1
+ require 'matrix'
2
+ class Perceptron::Weight
3
+
4
+ attr_reader :vector
5
+
6
+ INITIAL_BIAS = 0
7
+
8
+ def initialize(features_number)
9
+ @values = Array.new(features_number, 0)
10
+ @vector = vector_init(@values)
11
+ end
12
+
13
+ def create(features_number)
14
+ @weight = Weight.new(features_number)
15
+ end
16
+
17
+ def update(values)
18
+ @vector = Vector[*values]
19
+ end
20
+
21
+ private
22
+
23
+ def vector_init(array)
24
+ prepend_bias(array)
25
+ Vector[*array]
26
+ end
27
+
28
+ def prepend_bias(array)
29
+ array.unshift(INITIAL_BIAS)
30
+ end
31
+ end
@@ -0,0 +1,37 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "perceptron/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "perceptron"
8
+ spec.version = "0.1.1"
9
+ spec.authors = ["Alessandro Noiato", "Alistair Kung", "Corina Gheorghe", "Jini Coroneo", "Tom Scanlon"]
10
+ spec.email = ["doctorboredom@hotmail.com"]
11
+
12
+ spec.summary = %q{A reusable perceptron implementation written in ruby from scratch.}
13
+ spec.homepage = "https://github.com/terminalobject/Perceptron"
14
+ spec.license = "MIT"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ #if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "http://mygemserver.com'"
20
+ #else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ #end
24
+
25
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
26
+ f.match(%r{^(test|spec|features)/})
27
+ end
28
+ spec.files << ["lib/perceptron.rb", "lib/perceptron/unit.rb", "lib/perceptron/weight.rb"]
29
+ spec.files.flatten
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_development_dependency "bundler", "~> 1.15"
35
+ spec.add_development_dependency "rake", "~> 10.0"
36
+ spec.add_development_dependency "rspec"
37
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: perceptron
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Alessandro Noiato
8
+ - Alistair Kung
9
+ - Corina Gheorghe
10
+ - Jini Coroneo
11
+ - Tom Scanlon
12
+ autorequire:
13
+ bindir: exe
14
+ cert_chain: []
15
+ date: 2017-08-23 00:00:00.000000000 Z
16
+ dependencies:
17
+ - !ruby/object:Gem::Dependency
18
+ name: bundler
19
+ requirement: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - "~>"
22
+ - !ruby/object:Gem::Version
23
+ version: '1.15'
24
+ type: :development
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '1.15'
31
+ - !ruby/object:Gem::Dependency
32
+ name: rake
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - "~>"
36
+ - !ruby/object:Gem::Version
37
+ version: '10.0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ requirements:
42
+ - - "~>"
43
+ - !ruby/object:Gem::Version
44
+ version: '10.0'
45
+ - !ruby/object:Gem::Dependency
46
+ name: rspec
47
+ requirement: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: '0'
52
+ type: :development
53
+ prerelease: false
54
+ version_requirements: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ description:
60
+ email:
61
+ - doctorboredom@hotmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files: []
65
+ files:
66
+ - ".gitignore"
67
+ - Gemfile
68
+ - LICENSE.txt
69
+ - README.md
70
+ - Rakefile
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/perceptron.rb
74
+ - lib/perceptron/unit.rb
75
+ - lib/perceptron/version.rb
76
+ - lib/perceptron/weight.rb
77
+ - perceptron.gemspec
78
+ homepage: https://github.com/terminalobject/Perceptron
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.6.12
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: A reusable perceptron implementation written in ruby from scratch.
102
+ test_files: []