rubyzero 0.1.0
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 +7 -0
- data/.DS_Store +0 -0
- data/.gitignore +9 -0
- data/Gemfile +11 -0
- data/LICENSE +21 -0
- data/README.md +35 -0
- data/Rakefile +4 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/changelog.md +0 -0
- data/lib/rubyzero/core/cast.rb +25 -0
- data/lib/rubyzero/core/core.rb +15 -0
- data/lib/rubyzero/core/device.rb +32 -0
- data/lib/rubyzero/core/dtypes.rb +115 -0
- data/lib/rubyzero/core/exceptions.rb +7 -0
- data/lib/rubyzero/core/functions/activations.rb +20 -0
- data/lib/rubyzero/core/functions/elementary_functions.rb +29 -0
- data/lib/rubyzero/core/functions/function.rb +29 -0
- data/lib/rubyzero/core/functions/functions.rb +10 -0
- data/lib/rubyzero/core/functions/operators.rb +112 -0
- data/lib/rubyzero/core/functions/tensor_functions.rb +123 -0
- data/lib/rubyzero/core/tensor.rb +56 -0
- data/lib/rubyzero/core/tensor_backward.rb +17 -0
- data/lib/rubyzero/core/tensor_initialize_methods.rb +78 -0
- data/lib/rubyzero/core/tensor_operators.rb +27 -0
- data/lib/rubyzero/data/data.rb +9 -0
- data/lib/rubyzero/data/dataloader.rb +34 -0
- data/lib/rubyzero/data/dataset.rb +19 -0
- data/lib/rubyzero/data/presets/presets.rb +7 -0
- data/lib/rubyzero/data/presets/xor.rb +24 -0
- data/lib/rubyzero/nn/functional.rb +21 -0
- data/lib/rubyzero/nn/layers/affine.rb +21 -0
- data/lib/rubyzero/nn/layers/embedding.rb +7 -0
- data/lib/rubyzero/nn/layers/layer.rb +5 -0
- data/lib/rubyzero/nn/layers/layers.rb +44 -0
- data/lib/rubyzero/nn/layers/modellist.rb +40 -0
- data/lib/rubyzero/nn/layers/modelstack.rb +20 -0
- data/lib/rubyzero/nn/layers/multi_layer_perceptron.rb +26 -0
- data/lib/rubyzero/nn/layers/relu.rb +10 -0
- data/lib/rubyzero/nn/load.rb +5 -0
- data/lib/rubyzero/nn/losses/loss.rb +5 -0
- data/lib/rubyzero/nn/losses/losses.rb +8 -0
- data/lib/rubyzero/nn/losses/mse.rb +13 -0
- data/lib/rubyzero/nn/model.rb +75 -0
- data/lib/rubyzero/nn/nn.rb +11 -0
- data/lib/rubyzero/nn/optimizers/momentum.rb +22 -0
- data/lib/rubyzero/nn/optimizers/optimizer.rb +16 -0
- data/lib/rubyzero/nn/optimizers/optimizers.rb +9 -0
- data/lib/rubyzero/nn/optimizers/sgd.rb +14 -0
- data/lib/rubyzero/nn/parameters.rb +36 -0
- data/lib/rubyzero/utils/hyper_parameter_optimizer.rb +0 -0
- data/lib/rubyzero/utils/trainer.rb +49 -0
- data/lib/rubyzero/utils/utils.rb +8 -0
- data/lib/rubyzero/version.rb +5 -0
- data/lib/rubyzero.rb +7 -0
- data/note.txt +29 -0
- data/rubyzero.gemspec +36 -0
- metadata +101 -0
@@ -0,0 +1,36 @@
|
|
1
|
+
module RubyZero::NN
|
2
|
+
class Parameters
|
3
|
+
attr_accessor :elements
|
4
|
+
def initialize(elems)
|
5
|
+
@elements = elems
|
6
|
+
end
|
7
|
+
def each(&block)
|
8
|
+
@elements.each(&block)
|
9
|
+
end
|
10
|
+
def <<(element)
|
11
|
+
@elements << element
|
12
|
+
end
|
13
|
+
def size
|
14
|
+
sz = 0
|
15
|
+
@elements.each do |e|
|
16
|
+
sz += e.shape.inject(:*)
|
17
|
+
end
|
18
|
+
return sz
|
19
|
+
end
|
20
|
+
def to_marshal()
|
21
|
+
return Marshal.dump(@elements.map{|e| e.data})
|
22
|
+
end
|
23
|
+
def self.from_marshal(marshal)
|
24
|
+
return Parameters.new(Marshal.load(marshal).map{|d| RubyZero::Core::Tensor.new(d)})
|
25
|
+
end
|
26
|
+
def save(path)
|
27
|
+
File.open(path, 'wb') do |f|
|
28
|
+
f.write(to_marshal)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
def self.load(path)
|
32
|
+
return from_marshal(File.read(path))
|
33
|
+
end
|
34
|
+
include Enumerable
|
35
|
+
end
|
36
|
+
end
|
File without changes
|
@@ -0,0 +1,49 @@
|
|
1
|
+
module RubyZero::Utils
|
2
|
+
class Trainer
|
3
|
+
def initialize(model, loss_function: nil, optimizer: nil)
|
4
|
+
@model = model
|
5
|
+
@optimizer = optimizer || RubyZero::NN::Optimizers::Momentum.new(@model.parameters())
|
6
|
+
@loss_function = loss_function || RubyZero::NN::Losses::MSE.new()
|
7
|
+
end
|
8
|
+
|
9
|
+
def train(train_data, test_data, num_epochs:1, batch_size:1, shuffle:true, show_graph:false, show_graph_finish:true)
|
10
|
+
train_loader = RubyZero::Data::DataLoader.new(train_data, batch_size:batch_size, shuffle:shuffle)
|
11
|
+
test_loader = RubyZero::Data::DataLoader.new(test_data, batch_size:batch_size, shuffle:shuffle)
|
12
|
+
|
13
|
+
losses_train = []
|
14
|
+
losses_test = []
|
15
|
+
num_epochs.times do |epoch|
|
16
|
+
losses_train_b = []
|
17
|
+
losses_test_b = []
|
18
|
+
train_loader.each do |input, target|
|
19
|
+
@optimizer.zero_grad
|
20
|
+
loss = @loss_function.call(@model.call(input), target)
|
21
|
+
loss.backward()
|
22
|
+
@optimizer.step()
|
23
|
+
losses_train_b << loss.data[0]
|
24
|
+
end
|
25
|
+
test_loader.each do |input, target|
|
26
|
+
loss = @loss_function.call(@model.call(input), target)
|
27
|
+
losses_test_b << loss.data[0]
|
28
|
+
end
|
29
|
+
avg_loss_train_b = losses_train_b.reduce(:+) / losses_train_b.size
|
30
|
+
avg_loss_test_b = losses_test_b.reduce(:+) / losses_test_b.size
|
31
|
+
losses_train << avg_loss_train_b
|
32
|
+
losses_test << avg_loss_test_b
|
33
|
+
if show_graph or (show_graph_finish and epoch == num_epochs-1)
|
34
|
+
clear_console()
|
35
|
+
plot = UnicodePlot.lineplot((0..epoch).to_a, losses_train, name:"train loss")
|
36
|
+
UnicodePlot.lineplot!(plot, (0..epoch).to_a, losses_test, name:"test loss")
|
37
|
+
plot.render()
|
38
|
+
puts "train loss:#{avg_loss_train_b}\n test loss:#{avg_loss_test_b}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
return losses_train[-1]
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
def clear_console()
|
46
|
+
puts "\e[H\e[2J"
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/rubyzero.rb
ADDED
data/note.txt
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
module RubyZero
|
2
|
+
module Core
|
3
|
+
class Tensor
|
4
|
+
class Function
|
5
|
+
class Device
|
6
|
+
module Functions
|
7
|
+
module Functional
|
8
|
+
module NN
|
9
|
+
class Model
|
10
|
+
module Layers
|
11
|
+
Affine/Linear
|
12
|
+
Embedding
|
13
|
+
Convolutions
|
14
|
+
ConvTranspose
|
15
|
+
Attention
|
16
|
+
RNN/LSTM/GRU
|
17
|
+
module Data
|
18
|
+
class Dataset
|
19
|
+
class DataLoader
|
20
|
+
module Presets
|
21
|
+
class XOR
|
22
|
+
class MNIST
|
23
|
+
class CIFAR10
|
24
|
+
module Utils
|
25
|
+
class Trainer
|
26
|
+
module Text
|
27
|
+
class Tokenizer
|
28
|
+
class WordEmbedding < Embedding
|
29
|
+
|
data/rubyzero.gemspec
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/rubyzero/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rubyzero"
|
7
|
+
spec.version = Rubyzero::VERSION
|
8
|
+
spec.authors = ["uthree"]
|
9
|
+
spec.email = ["33905956+uthree@users.noreply.github.com"]
|
10
|
+
|
11
|
+
spec.summary = "A simple deep learning library for Ruby"
|
12
|
+
spec.description = "(work in progress)"
|
13
|
+
spec.homepage = "https://github.com/uthree/RubyZero"
|
14
|
+
spec.required_ruby_version = ">= 2.4.0"
|
15
|
+
|
16
|
+
#spec.metadata["allowed_push_host"] = "TODO: Set to 'https://mygemserver.com'"
|
17
|
+
|
18
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
19
|
+
spec.metadata["source_code_uri"] = "https://github.com/uthree/RubyZero"
|
20
|
+
spec.metadata["changelog_uri"] = "https://github.com/uthree/RubyZero/blob/main/changelog.md"
|
21
|
+
|
22
|
+
# Specify which files should be added to the gem when it is released.
|
23
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
24
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
25
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{\A(?:test|spec|features)/}) }
|
26
|
+
end
|
27
|
+
spec.bindir = "exe"
|
28
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
29
|
+
spec.require_paths = ["lib"]
|
30
|
+
|
31
|
+
# Uncomment to register a new dependency of your gem
|
32
|
+
# spec.add_dependency "example-gem", "~> 1.0"
|
33
|
+
|
34
|
+
# For more information and examples about making a new gem, checkout our
|
35
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
36
|
+
end
|
metadata
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rubyzero
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- uthree
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-10-09 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: "(work in progress)"
|
14
|
+
email:
|
15
|
+
- 33905956+uthree@users.noreply.github.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".DS_Store"
|
21
|
+
- ".gitignore"
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- bin/console
|
27
|
+
- bin/setup
|
28
|
+
- changelog.md
|
29
|
+
- lib/rubyzero.rb
|
30
|
+
- lib/rubyzero/core/cast.rb
|
31
|
+
- lib/rubyzero/core/core.rb
|
32
|
+
- lib/rubyzero/core/device.rb
|
33
|
+
- lib/rubyzero/core/dtypes.rb
|
34
|
+
- lib/rubyzero/core/exceptions.rb
|
35
|
+
- lib/rubyzero/core/functions/activations.rb
|
36
|
+
- lib/rubyzero/core/functions/elementary_functions.rb
|
37
|
+
- lib/rubyzero/core/functions/function.rb
|
38
|
+
- lib/rubyzero/core/functions/functions.rb
|
39
|
+
- lib/rubyzero/core/functions/operators.rb
|
40
|
+
- lib/rubyzero/core/functions/tensor_functions.rb
|
41
|
+
- lib/rubyzero/core/tensor.rb
|
42
|
+
- lib/rubyzero/core/tensor_backward.rb
|
43
|
+
- lib/rubyzero/core/tensor_initialize_methods.rb
|
44
|
+
- lib/rubyzero/core/tensor_operators.rb
|
45
|
+
- lib/rubyzero/data/data.rb
|
46
|
+
- lib/rubyzero/data/dataloader.rb
|
47
|
+
- lib/rubyzero/data/dataset.rb
|
48
|
+
- lib/rubyzero/data/presets/presets.rb
|
49
|
+
- lib/rubyzero/data/presets/xor.rb
|
50
|
+
- lib/rubyzero/nn/functional.rb
|
51
|
+
- lib/rubyzero/nn/layers/affine.rb
|
52
|
+
- lib/rubyzero/nn/layers/embedding.rb
|
53
|
+
- lib/rubyzero/nn/layers/layer.rb
|
54
|
+
- lib/rubyzero/nn/layers/layers.rb
|
55
|
+
- lib/rubyzero/nn/layers/modellist.rb
|
56
|
+
- lib/rubyzero/nn/layers/modelstack.rb
|
57
|
+
- lib/rubyzero/nn/layers/multi_layer_perceptron.rb
|
58
|
+
- lib/rubyzero/nn/layers/relu.rb
|
59
|
+
- lib/rubyzero/nn/load.rb
|
60
|
+
- lib/rubyzero/nn/losses/loss.rb
|
61
|
+
- lib/rubyzero/nn/losses/losses.rb
|
62
|
+
- lib/rubyzero/nn/losses/mse.rb
|
63
|
+
- lib/rubyzero/nn/model.rb
|
64
|
+
- lib/rubyzero/nn/nn.rb
|
65
|
+
- lib/rubyzero/nn/optimizers/momentum.rb
|
66
|
+
- lib/rubyzero/nn/optimizers/optimizer.rb
|
67
|
+
- lib/rubyzero/nn/optimizers/optimizers.rb
|
68
|
+
- lib/rubyzero/nn/optimizers/sgd.rb
|
69
|
+
- lib/rubyzero/nn/parameters.rb
|
70
|
+
- lib/rubyzero/utils/hyper_parameter_optimizer.rb
|
71
|
+
- lib/rubyzero/utils/trainer.rb
|
72
|
+
- lib/rubyzero/utils/utils.rb
|
73
|
+
- lib/rubyzero/version.rb
|
74
|
+
- note.txt
|
75
|
+
- rubyzero.gemspec
|
76
|
+
homepage: https://github.com/uthree/RubyZero
|
77
|
+
licenses: []
|
78
|
+
metadata:
|
79
|
+
homepage_uri: https://github.com/uthree/RubyZero
|
80
|
+
source_code_uri: https://github.com/uthree/RubyZero
|
81
|
+
changelog_uri: https://github.com/uthree/RubyZero/blob/main/changelog.md
|
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: 2.4.0
|
91
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - ">="
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
requirements: []
|
97
|
+
rubygems_version: 3.1.4
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: A simple deep learning library for Ruby
|
101
|
+
test_files: []
|