-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRakefile
96 lines (74 loc) · 1.75 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'rspec/core/rake_task'
require 'synaptical'
RSpec::Core::RakeTask.new(:spec)
task default: :spec
def create_network
input_layer = Synaptical::Layer.new(2)
hidden_layer = Synaptical::Layer.new(3)
output_layer = Synaptical::Layer.new(1)
input_layer.project(hidden_layer)
hidden_layer.project(output_layer)
Synaptical::Network.new(
input: input_layer, hidden: [hidden_layer], output: output_layer
)
end
def train(network)
learning_rate = 0.3
network.activate([0, 0])
network.propagate(learning_rate, [0])
network.activate([0, 1])
network.propagate(learning_rate, [1])
network.activate([1, 0])
network.propagate(learning_rate, [1])
network.activate([1, 1])
network.propagate(learning_rate, [0])
end
def activate(network)
network.activate([rand(2), rand(2)])
end
task :benchmark do
require 'benchmark/ips'
require 'benchmark/memory'
network = create_network
train = lambda do |x|
x.report('training') { train(network) }
x.compare!
end
activate = lambda do |x|
x.report('activate') { activate(network) }
x.compare!
end
Benchmark.ips(&train)
Benchmark.memory(&train)
Benchmark.ips(&activate)
Benchmark.memory(&activate)
end
namespace :profile do
require 'hotch'
require 'hotch/memory'
task :training do
network = create_network
Hotch() do
10_000.times do
train(network)
end
end
Hotch.memory do
10_000.times do
train(network)
end
end
end
task :activate do
network = create_network
10_000.times { train(network) }
Hotch() do
10_000.times { activate(network) }
end
Hotch.memory do
10_000.times { activate(network) }
end
end
end