rubygrad 1.2.2 → 1.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/nn.rb +9 -0
  3. data/mlp_example.rb +2 -5
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f05df7e74616deb2db9a7c7340e105e0997268d9d2938d07dbabf51395684fe5
4
- data.tar.gz: 6179a5c6a57c37411784195a9ca3a15f73debdd06aaeae46a754ff5857e60770
3
+ metadata.gz: d02ad8e3dfa9fb89c29c21d12f17488ca1ddb8dbf2cbfdfdd61f110a84cc7b02
4
+ data.tar.gz: 2cb1da6e863aef973b12c69162723021c6bf6f07d9609628a54e889fdca1e362
5
5
  SHA512:
6
- metadata.gz: 2187327cf7c8b2a8e323b90138c49b7bd6cb5c84ed0e784a5c288556525cf0f8c619dd2aef024b6afaa27aad5b4f2a104457e67dd7158a121f41e9b5dfa631c3
7
- data.tar.gz: ebb19b5217fb756fa9072dcf9e2b39ff503f93ac94f782cc45e2ffdc73ee85e6f5c9e471d7995d1017aa0697b97437cf5eb56be1cee34d79e30b0aaf4d5818b9
6
+ metadata.gz: 0bf4727a51a73b307686f219ce5633880c9ee80348ca1834ae9f4b357a5d20bef9c572f7ac6b999a4053b9cc2229336de5ddcc9e2e6aa475bb246bbad5dae4fa
7
+ data.tar.gz: 0d08776331371d9cc65b87d099443af44ae8be5cb8b85f0596a680ec40205bf3514cf2a15e45e4433004a2a6a87b0341a7b52fb61d48cef3c491bad647833071
data/lib/nn.rb CHANGED
@@ -50,6 +50,8 @@ class Neuron
50
50
  sum.relu
51
51
  elsif @activation_function == :sigmoid
52
52
  sum.sigmoid
53
+ elsif @activation_function == :none
54
+ sum
53
55
  else
54
56
  raise "Unsupported activation function: #{activation_function}"
55
57
  end
@@ -210,4 +212,11 @@ class MLP
210
212
  end
211
213
  out.size == 1 ? out[0] : out # for convenience
212
214
  end
215
+
216
+ def print_pass(learning_rate, loss, pass, passes, learning_rate_precision = 2, loss_precision = 10)
217
+ passes_format = "%#{passes.digits.length}d"
218
+ learning_rate_format = "%.#{learning_rate_precision}f"
219
+ loss_format = "%.#{loss_precision}f"
220
+ puts "Pass #{passes_format % (pass + 1)} => Learning rate: #{learning_rate_format % learning_rate} => Loss: #{loss_format % loss.value}"
221
+ end
213
222
  end
data/mlp_example.rb CHANGED
@@ -27,10 +27,6 @@ y_expected = [1.0, -1.0, -1.0, 1.0]
27
27
  passes = 2000
28
28
  learning_rate = 0.2
29
29
 
30
- _loss_precision = 10
31
- _passes_format = "%#{passes.digits.length}d"
32
- _loss_format = "%.#{_loss_precision}f"
33
-
34
30
  (0...passes).each do |pass|
35
31
 
36
32
  # forward pass (calculate output)
@@ -47,7 +43,8 @@ _loss_format = "%.#{_loss_precision}f"
47
43
  # improve neural net (update weights and biases)
48
44
  nn.parameters.each { |p| p.value -= learning_rate * p.grad }
49
45
 
50
- puts "Pass #{_passes_format % (pass + 1)} => Learning rate: #{"%.10f" % learning_rate} => Loss: #{_loss_format % loss.value}" if (pass + 1) % 100 == 0 or pass == 0
46
+ # print some info about our progress from time to time
47
+ nn.print_pass(learning_rate, loss, pass, passes) if (pass + 1) % (passes / 20) == 0 or pass == 0
51
48
 
52
49
  break if loss.value == 0 # just for fun and just in case
53
50
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubygrad
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergio Oliveira Jr
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-22 00:00:00.000000000 Z
11
+ date: 2023-03-23 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: sergio.oliveira.jr@gmail.com