ruby-dnn 0.8.5 → 0.8.6

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3b897e7f51a4bf649e3bc9b0207dd3dbea30fc35d50ca4cf5ff08c7f8daacb4c
4
- data.tar.gz: '078b1fa114f3abe04cc40e2e01211f3a563c2796bc9da78b904478ad0a5e83ac'
3
+ metadata.gz: 63b7cd17090ec6fd4e1f23e6b40f3c41e8a31ad0fa887945bda962a6df305c1b
4
+ data.tar.gz: b7e173f392c73e8151bde633d61913e8ce1645aaf9acc3a48788a950890d6ca4
5
5
  SHA512:
6
- metadata.gz: fe348f3964752cdc520d2f571da843a26073795de1033a823757e388f506e800fd310ffe6063d3cb415d55502c5c9576162cb1404a925088dced844b143aa129
7
- data.tar.gz: d3347bb4ed8c6b2ad9945252947aa44c44eda188ec64cb5aa4ac371d7f3a426241f9c76c67eb12de42854ceebf4aa796a2762b43b7395a09a75e234e403b6619
6
+ metadata.gz: c35a8a90216f42fcd8f88b4d4ee6f98fa517c5c2c8aaa6e61eeb8c260b176966c979e0e25311c6c4d0237e89f5e9c0f39eb056f1fad37f5fbaeeff7d0a4e1739
7
+ data.tar.gz: d2905e0b21c2dd2911cd985e490425be1909fed26e66e63b357aff7d657c2c86099653b1cdff8910171a7f1302029b2aa1f5f63f850b2a3f3d398e2325982475
data/API-Reference.ja.md CHANGED
@@ -2,7 +2,7 @@
2
2
  ruby-dnnのAPIリファレンスです。このリファレンスでは、APIを利用するうえで必要となるクラスとメソッドしか記載していません。
3
3
  そのため、プログラムの詳細が必要な場合は、ソースコードを参照してください。
4
4
 
5
- 最終更新バージョン:0.8.5
5
+ 最終更新バージョン:0.8.6
6
6
 
7
7
  # module DNN
8
8
  ruby-dnnの名前空間をなすモジュールです。
@@ -706,6 +706,16 @@ Float alpha
706
706
  パラメータを0で初期化します。
707
707
 
708
708
 
709
+ # class Const < Initializer
710
+ パラメータを指定の定数で初期化します。
711
+
712
+ ## 【Instance methods】
713
+ ## def initialize(const)
714
+ ### arguments
715
+ * Float const
716
+ 初期化する定数
717
+
718
+
709
719
  # class RandomNormal < Initializer
710
720
  パラメータを正規分布による乱数で初期化します。
711
721
 
@@ -1,6 +1,6 @@
1
1
  # LIB-APIリファレンス
2
2
  ruby-dnnの付属ライブラリのリファレンスです。
3
- 最終更新バージョン:0.5.10
3
+ 最終更新バージョン:0.8.6
4
4
 
5
5
 
6
6
  # dnn/lib/mnist
@@ -71,11 +71,11 @@ Array
71
71
  Numo::UInt8の[10000]の形式
72
72
 
73
73
 
74
- # dnn/lib/image_io
75
- 画像のin/outを行うライブラリです。内部でstb_image.hとstb_image_write.hを使用しています。
74
+ # dnn/lib/image
75
+ 画像のread/writeを行うライブラリです。内部でstb_image.hとstb_image_write.hを使用しています。
76
76
 
77
77
 
78
- # module ImageIO
78
+ # module Image
79
79
 
80
80
  ## def self.read(file_name)
81
81
  画像をNumo::UInt8形式で読み込みます。
@@ -271,12 +271,12 @@ module DNN
271
271
 
272
272
  def backward(dout)
273
273
  unpool_h, unpool_w = @unpool_size
274
- dout = dout.reshape(dout.shape[0], @x_shape[0], unpool_h, @x_shape[1], unpool_w, @num_channel)
274
+ dout = dout.reshape(dout.shape[0], @x_shape[1], unpool_h, @x_shape[2], unpool_w, @num_channel)
275
275
  dout[true, true, 0, true, 0, true].clone
276
276
  end
277
277
 
278
278
  def shape
279
- [@out_width, @out_height, @num_channel]
279
+ [*@out_size, @num_channel]
280
280
  end
281
281
 
282
282
  def to_hash
@@ -20,6 +20,25 @@ module DNN
20
20
  param.data = param.data.fill(0)
21
21
  end
22
22
  end
23
+
24
+
25
+ class Const < Initializer
26
+ def self.load_hash(hash)
27
+ self.new(hash[:const])
28
+ end
29
+
30
+ def initialize(const)
31
+ @const = const
32
+ end
33
+
34
+ def init_param(layer, param)
35
+ param.data = param.data.fill(@const)
36
+ end
37
+
38
+ def to_hash
39
+ super({const: @const})
40
+ end
41
+ end
23
42
 
24
43
 
25
44
  class RandomNormal < Initializer
@@ -121,25 +121,27 @@ module DNN
121
121
 
122
122
 
123
123
  class SimpleRNN_Dense
124
- def initialize(rnn)
125
- @rnn = rnn
126
- @activation = rnn.activation.clone
124
+ def initialize(weight, weight2, bias, activation)
125
+ @weight = weight
126
+ @weight2 = weight2
127
+ @bias = bias
128
+ @activation = activation.clone
127
129
  end
128
130
 
129
131
  def forward(x, h)
130
132
  @x = x
131
133
  @h = h
132
- h2 = x.dot(@rnn.weight.data) + h.dot(@rnn.weight2.data) + @rnn.bias.data
134
+ h2 = x.dot(@weight.data) + h.dot(@weight2.data) + @bias.data
133
135
  @activation.forward(h2)
134
136
  end
135
137
 
136
138
  def backward(dh2)
137
139
  dh2 = @activation.backward(dh2)
138
- @rnn.weight.grad += @x.transpose.dot(dh2)
139
- @rnn.weight2.grad += @h.transpose.dot(dh2)
140
- @rnn.bias.grad += dh2.sum(0)
141
- dx = dh2.dot(@rnn.weight.data.transpose)
142
- dh = dh2.dot(@rnn.weight2.data.transpose)
140
+ @weight.grad += @x.transpose.dot(dh2)
141
+ @weight2.grad += @h.transpose.dot(dh2)
142
+ @bias.grad += dh2.sum(0)
143
+ dx = dh2.dot(@weight.data.transpose)
144
+ dh = dh2.dot(@weight2.data.transpose)
143
145
  [dx, dh]
144
146
  end
145
147
  end
@@ -194,15 +196,17 @@ module DNN
194
196
  @weight_initializer.init_param(self, @weight2)
195
197
  @bias_initializer.init_param(self, @bias)
196
198
  @time_length.times do |t|
197
- @layers << SimpleRNN_Dense.new(self)
199
+ @layers << SimpleRNN_Dense.new(@weight, @weight2, @bias)
198
200
  end
199
201
  end
200
202
  end
201
203
 
202
204
 
203
205
  class LSTM_Dense
204
- def initialize(rnn)
205
- @rnn = rnn
206
+ def initialize(weight, weight2, bias)
207
+ @weight = weight
208
+ @weight2 = weight2
209
+ @bias = bias
206
210
  @tanh = Tanh.new
207
211
  @g_tanh = Tanh.new
208
212
  @forget_sigmoid = Sigmoid.new
@@ -215,7 +219,7 @@ module DNN
215
219
  @h = h
216
220
  @c = c
217
221
  num_nodes = h.shape[1]
218
- a = x.dot(@rnn.weight.data) + h.dot(@rnn.weight2.data) + @rnn.bias.data
222
+ a = x.dot(@weight.data) + h.dot(@weight2.data) + @bias.data
219
223
 
220
224
  @forget = @forget_sigmoid.forward(a[true, 0...num_nodes])
221
225
  @g = @g_tanh.forward(a[true, num_nodes...(num_nodes * 2)])
@@ -239,11 +243,11 @@ module DNN
239
243
 
240
244
  da = Xumo::SFloat.hstack([dforget, dg, din, dout])
241
245
 
242
- @rnn.weight.grad += @x.transpose.dot(da)
243
- @rnn.weight2.grad += @h.transpose.dot(da)
244
- @rnn.bias.grad += da.sum(0)
245
- dx = da.dot(@rnn.weight.data.transpose)
246
- dh = da.dot(@rnn.weight2.data.transpose)
246
+ @weight.grad += @x.transpose.dot(da)
247
+ @weight2.grad += @h.transpose.dot(da)
248
+ @bias.grad += da.sum(0)
249
+ dx = da.dot(@weight.data.transpose)
250
+ dh = da.dot(@weight2.data.transpose)
247
251
  dc = dc2_tmp * @forget
248
252
  [dx, dh, dc]
249
253
  end
@@ -331,15 +335,17 @@ module DNN
331
335
  @weight_initializer.init_param(self, @weight2)
332
336
  @bias_initializer.init_param(self, @bias)
333
337
  @time_length.times do |t|
334
- @layers << LSTM_Dense.new(self)
338
+ @layers << LSTM_Dense.new(@weight, @weight2, @bias)
335
339
  end
336
340
  end
337
341
  end
338
342
 
339
343
 
340
344
  class GRU_Dense
341
- def initialize(rnn)
342
- @rnn = rnn
345
+ def initialize(weight, weight2, bias)
346
+ @weight = weight
347
+ @weight2 = weight2
348
+ @bias = bias
343
349
  @update_sigmoid = Sigmoid.new
344
350
  @reset_sigmoid = Sigmoid.new
345
351
  @tanh = Tanh.new
@@ -349,16 +355,16 @@ module DNN
349
355
  @x = x
350
356
  @h = h
351
357
  num_nodes = h.shape[1]
352
- @weight_a = @rnn.weight.data[true, 0...(num_nodes * 2)]
353
- @weight2_a = @rnn.weight2.data[true, 0...(num_nodes * 2)]
354
- bias_a = @rnn.bias.data[0...(num_nodes * 2)]
358
+ @weight_a = @weight.data[true, 0...(num_nodes * 2)]
359
+ @weight2_a = @weight2.data[true, 0...(num_nodes * 2)]
360
+ bias_a = @bias.data[0...(num_nodes * 2)]
355
361
  a = x.dot(@weight_a) + h.dot(@weight2_a) + bias_a
356
362
  @update = @update_sigmoid.forward(a[true, 0...num_nodes])
357
363
  @reset = @reset_sigmoid.forward(a[true, num_nodes..-1])
358
364
 
359
- @weight_h = @rnn.weight.data[true, (num_nodes * 2)..-1]
360
- @weight2_h = @rnn.weight2.data[true, (num_nodes * 2)..-1]
361
- bias_h = @rnn.bias.data[(num_nodes * 2)..-1]
365
+ @weight_h = @weight.data[true, (num_nodes * 2)..-1]
366
+ @weight2_h = @weight2.data[true, (num_nodes * 2)..-1]
367
+ bias_h = @bias.data[(num_nodes * 2)..-1]
362
368
  @tanh_h = @tanh.forward(x.dot(@weight_h) + (h * @reset).dot(@weight2_h) + bias_h)
363
369
  h2 = (1 - @update) * h + @update * @tanh_h
364
370
  h2
@@ -383,9 +389,9 @@ module DNN
383
389
  dh += da.dot(@weight2_a.transpose)
384
390
  dbias_a = da.sum(0)
385
391
 
386
- @rnn.weight.grad += Xumo::SFloat.hstack([dweight_a, dweight_h])
387
- @rnn.weight2.grad += Xumo::SFloat.hstack([dweight2_a, dweight2_h])
388
- @rnn.bias.grad += Xumo::SFloat.hstack([dbias_a, dbias_h])
392
+ @weight.grad += Xumo::SFloat.hstack([dweight_a, dweight_h])
393
+ @weight2.grad += Xumo::SFloat.hstack([dweight2_a, dweight2_h])
394
+ @bias.grad += Xumo::SFloat.hstack([dbias_a, dbias_h])
389
395
  [dx, dh]
390
396
  end
391
397
  end
@@ -425,7 +431,7 @@ module DNN
425
431
  @weight_initializer.init_param(self, @weight2)
426
432
  @bias_initializer.init_param(self, @bias)
427
433
  @time_length.times do |t|
428
- @layers << GRU_Dense.new(self)
434
+ @layers << GRU_Dense.new(@weight, @weight2, @bias)
429
435
  end
430
436
  end
431
437
  end
@@ -2,9 +2,9 @@ require "numo/narray"
2
2
  require_relative "../ext/rb_stb_image/rb_stb_image"
3
3
 
4
4
  module DNN
5
- module ImageIO
5
+ module Image
6
6
  def self.read(file_name)
7
- raise ImageIO::ReadError.new("#{file_name} is not found.") unless File.exist?(file_name)
7
+ raise Image::ReadError.new("#{file_name} is not found.") unless File.exist?(file_name)
8
8
  bin, w, h, n = Stb.stbi_load(file_name, 3)
9
9
  img = Numo::UInt8.from_binary(bin)
10
10
  img.reshape(h, w, 3)
@@ -29,13 +29,13 @@ module DNN
29
29
  Stb.stbi_write_jpg(file_name, w, h, ch, bin, quality)
30
30
  end
31
31
  rescue => ex
32
- raise ImageIO::WriteError.new(ex.message)
32
+ raise Image::WriteError.new(ex.message)
33
33
  end
34
34
  end
35
35
 
36
- class ImageIO::Error < StandardError; end
36
+ class Image::Error < StandardError; end
37
37
 
38
- class ImageIO::ReadError < ImageIO::Error; end
38
+ class Image::ReadError < Image::Error; end
39
39
 
40
- class ImageIO::WriteError < ImageIO::Error; end
40
+ class Image::WriteError < Image::Error; end
41
41
  end
data/lib/dnn/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module DNN
2
- VERSION = "0.8.5"
2
+ VERSION = "0.8.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-dnn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.5
4
+ version: 0.8.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - unagiootoro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-02-22 00:00:00.000000000 Z
11
+ date: 2019-02-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: numo-narray
@@ -123,7 +123,7 @@ files:
123
123
  - lib/dnn/ext/rb_stb_image/stb_image.h
124
124
  - lib/dnn/ext/rb_stb_image/stb_image_write.h
125
125
  - lib/dnn/lib/cifar10.rb
126
- - lib/dnn/lib/image_io.rb
126
+ - lib/dnn/lib/image.rb
127
127
  - lib/dnn/lib/mnist.rb
128
128
  - lib/dnn/version.rb
129
129
  - ruby-dnn.gemspec