ruby-dnn 0.2.2 → 0.3.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 +4 -4
- data/API-Reference.ja.md +32 -20
- data/LIB-API-Reference.ja.md +8 -8
- data/examples/cifar10_example.rb +5 -5
- data/examples/mnist_example2.rb +3 -3
- data/lib/dnn.rb +3 -1
- data/lib/dnn/core/activations.rb +12 -11
- data/lib/dnn/core/layers.rb +55 -54
- data/lib/dnn/core/model.rb +1 -1
- data/lib/dnn/core/optimizers.rb +1 -1
- data/lib/dnn/core/version.rb +1 -1
- data/lib/dnn/ext/dataset_loader/dataset_loader.c +4 -4
- data/lib/dnn/ext/dataset_loader/numo/narray.h +2 -2
- data/lib/dnn/ext/image_io/image_io_ext.c +1 -1
- data/lib/dnn/ext/image_io/numo/narray.h +2 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 42cb9aa824ed0c969932f6f5f17a6ef461ebc945ffe11a3eeb304a9c134bb8f2
|
4
|
+
data.tar.gz: d92e71d07b69f3ffbb6acced5cbfde129c8496c18a3a6b89f9817ac29204b79f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d0eed09e32fbab61aca4691493a3d15c47abbed7ee3cb035c3e77933d17045276cca8140eb88c9de425a040a2de4d26c245a434c4084d0054bdf4a6f0848ba6f
|
7
|
+
data.tar.gz: 81fbf5b6e8109903f0b95f03d3287c48b0443e0445ec9b27128299ab087c3b664282cbe7f9ef5b8e7d109f08eb29df13b9e1de382afee299e6f773d28c00462f
|
data/API-Reference.ja.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
ruby-dnnのAPIリファレンスです。このリファレンスでは、APIを利用するうえで必要となるクラスとメソッドしか記載していません。
|
3
3
|
そのため、プログラムの詳細が必要な場合は、ソースコードを参照してください。
|
4
4
|
|
5
|
-
|
5
|
+
最終更新バージョン:0.3.0
|
6
6
|
|
7
7
|
# module DNN
|
8
8
|
ruby-dnnの名前空間をなすモジュールです。
|
@@ -238,6 +238,7 @@ Hash
|
|
238
238
|
入力層に該当するレイヤーです。モデルの先頭レイヤーは、必ずこのクラスのインスタンスでなければなりません。
|
239
239
|
|
240
240
|
## 【Instance methods】
|
241
|
+
|
241
242
|
## def initialize(dim_or_shape)
|
242
243
|
コンストラクタ
|
243
244
|
### arguments
|
@@ -258,6 +259,7 @@ Float
|
|
258
259
|
重み減衰の係数を取得します。
|
259
260
|
|
260
261
|
## 【Instance methods】
|
262
|
+
|
261
263
|
## def initialize(num_nodes, weight_initializer: nil, bias_initializer: nil, weight_decay: 0)
|
262
264
|
コンストラクタ。
|
263
265
|
### arguments
|
@@ -277,22 +279,23 @@ nilを指定すると、Zerosイニシャライザーが使用されます。
|
|
277
279
|
畳み込みレイヤーを扱うクラスです。
|
278
280
|
|
279
281
|
## 【Instance methods】
|
280
|
-
|
282
|
+
|
283
|
+
## def initialize(num_filters, filter_size, weight_initializer: nil, bias_initializer: nil, strides: 1, padding false, weight_decay: 0)
|
281
284
|
コンストラクタ。
|
282
285
|
### arguments
|
283
286
|
* Integer num_filters
|
284
|
-
|
285
|
-
* Integer
|
286
|
-
|
287
|
-
|
288
|
-
フィルターの縦の長さ
|
287
|
+
出力するフィルターの枚数。
|
288
|
+
* Integer | Array filter_size
|
289
|
+
フィルターの横と縦の長さ。
|
290
|
+
Arrayで指定する場合、[Integer width, Integer height]の形式で指定します。
|
289
291
|
* Initializer weight_initializer: nil
|
290
292
|
重みの初期化に使用するイニシャライザーを設定します
|
291
293
|
nilを指定すると、RandomNormalイニシャライザーが使用されます。
|
292
294
|
* Initializer bias_initializer: nil
|
293
295
|
バイアスの初期化に使用するイニシャライザーを設定します。
|
294
|
-
* Array<Integer> strides:
|
295
|
-
|
296
|
+
* Array<Integer> strides: 1
|
297
|
+
畳み込みを行う際のストライドの単位を指定します。
|
298
|
+
Arrayで指定する場合、[Integer width, Integer height]の形式で指定します。
|
296
299
|
* bool padding: true
|
297
300
|
イメージに対してゼロパディングを行うか否かを設定します。trueを設定すると、出力されるイメージのサイズが入力されたイメージと同じになるように
|
298
301
|
ゼロパディングを行います。
|
@@ -304,15 +307,17 @@ nilを指定すると、RandomNormalイニシャライザーが使用されま
|
|
304
307
|
maxプーリングを行うレイヤーです。
|
305
308
|
|
306
309
|
## 【Instance methods】
|
307
|
-
|
310
|
+
|
311
|
+
## def initialize(pool_size, strides: nil, padding: false)
|
308
312
|
コンストラクタ。
|
309
313
|
### arguments
|
310
|
-
* Integer
|
311
|
-
|
312
|
-
|
313
|
-
プーリングを行う縦の長さ。
|
314
|
+
* Integer | Array pool_size
|
315
|
+
プーリングを行う横と縦の長さ。
|
316
|
+
Arrayで指定する場合、[Integer width, Integer height]の形式で指定します。
|
314
317
|
* Array<Integer> strides: nil
|
315
|
-
|
318
|
+
畳み込みを行う際のストライドの単位を指定します。
|
319
|
+
Arrayで指定する場合、[Integer width, Integer height]の形式で指定します。
|
320
|
+
なお、nilが設定された場合は、pool_sizeがstridesの値となります。
|
316
321
|
* bool padding: true
|
317
322
|
イメージに対してゼロパディングを行うか否かを設定します。trueを設定すると、出力されるイメージのサイズが入力されたイメージと同じになるように
|
318
323
|
ゼロパディングを行います。
|
@@ -322,13 +327,13 @@ maxプーリングを行うレイヤーです。
|
|
322
327
|
逆プーリングを行うレイヤーです。
|
323
328
|
|
324
329
|
## 【Instance methods】
|
325
|
-
|
330
|
+
|
331
|
+
## def initialize(unpool_size)
|
326
332
|
コンストラクタ。
|
327
333
|
### arguments
|
328
|
-
* Integer
|
329
|
-
|
330
|
-
|
331
|
-
逆プーリングを行う縦の長さ。
|
334
|
+
* Integer unpool_size
|
335
|
+
逆プーリングを行う横と縦の長さ。
|
336
|
+
Arrayで指定する場合、[Integer width, Integer height]の形式で指定します。
|
332
337
|
|
333
338
|
|
334
339
|
# class Flatten
|
@@ -339,6 +344,7 @@ N次元のデータを平坦化します。
|
|
339
344
|
データの形状を変更します。
|
340
345
|
|
341
346
|
## 【Instance methods】
|
347
|
+
|
342
348
|
## def initialize(shape)
|
343
349
|
コンストラクタ。
|
344
350
|
### arguments
|
@@ -349,6 +355,8 @@ N次元のデータを平坦化します。
|
|
349
355
|
# class OutputLayer < Layer
|
350
356
|
出力層に該当するレイヤーです。出力層の活性化関数は、全てこのクラスを継承する必要があります。
|
351
357
|
|
358
|
+
## 【Instance methods】
|
359
|
+
|
352
360
|
## abstruct def backward(y)
|
353
361
|
出力層の活性化関数と損失関数を合わせたものを微分した導関数を用いて、教師データの出力データを逆方向に伝搬します。
|
354
362
|
### arguments
|
@@ -369,6 +377,7 @@ SFloat y
|
|
369
377
|
# class Dropout
|
370
378
|
学習の際に、一部のノードを非活性化させるクラスです。
|
371
379
|
|
380
|
+
## 【Instance methods】
|
372
381
|
## def initialize(dropout_ratio)
|
373
382
|
コンストラクタ。
|
374
383
|
### arguments
|
@@ -427,6 +436,8 @@ LeakyReLU関数のレイヤーです。
|
|
427
436
|
# class Initializer
|
428
437
|
全てのInitializeクラスのスーパークラスです。
|
429
438
|
|
439
|
+
## 【Instance methods】
|
440
|
+
|
430
441
|
## def init_param(layer, param_key, param)
|
431
442
|
レイヤーの持つパラメータを更新します。
|
432
443
|
### arguments
|
@@ -445,6 +456,7 @@ LeakyReLU関数のレイヤーです。
|
|
445
456
|
# class RandomNormal < Initializer
|
446
457
|
パラメータを正規分布による乱数で初期化します。
|
447
458
|
|
459
|
+
## 【Instance methods】
|
448
460
|
## def initialize(mean = 0, std = 0.05)
|
449
461
|
### arguments
|
450
462
|
* Float mean = 0
|
data/LIB-API-Reference.ja.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# LIB-APIリファレンス
|
2
2
|
ruby-dnnの付属ライブラリのリファレンスです。
|
3
|
-
|
3
|
+
最終更新バージョン:0.3.0
|
4
4
|
|
5
5
|
|
6
6
|
# dnn/lib/mnist
|
@@ -72,26 +72,26 @@ Array
|
|
72
72
|
|
73
73
|
|
74
74
|
# dnn/lib/image_io
|
75
|
-
画像のin/outを行うライブラリです。内部でstb_imageとstb_image_writeを使用しています。
|
75
|
+
画像のin/outを行うライブラリです。内部でstb_image.hとstb_image_write.hを使用しています。
|
76
76
|
|
77
77
|
|
78
78
|
# module ImageIO
|
79
79
|
|
80
80
|
## def self.read(file_name)
|
81
|
-
画像を
|
81
|
+
画像をXumo::UInt8形式で読み込みます。
|
82
82
|
### arguments
|
83
83
|
* String file_name
|
84
84
|
読み込む画像のファイル名。
|
85
85
|
### return
|
86
|
-
|
87
|
-
[width, height, rgb]の
|
86
|
+
Xumo::UInt8
|
87
|
+
[width, height, rgb]のXumo::UInt8配列。
|
88
88
|
|
89
89
|
## def self.write(file_name, nary, quality: 100)
|
90
|
-
|
90
|
+
Xumo::UInt8形式の画像を書き込みます。
|
91
91
|
### arguments
|
92
92
|
* String file_name
|
93
93
|
書き込む画像のファイル名。
|
94
|
-
*
|
95
|
-
[width, height, rgb]の
|
94
|
+
* Xumo::UInt8
|
95
|
+
[width, height, rgb]のXumo::UInt8配列。
|
96
96
|
* Integer quality: 100
|
97
97
|
画像をJPEGで書き込む場合のクオリティ。
|
data/examples/cifar10_example.rb
CHANGED
@@ -25,21 +25,21 @@ model = Model.new
|
|
25
25
|
|
26
26
|
model << InputLayer.new([32, 32, 3])
|
27
27
|
|
28
|
-
model << Conv2D.new(16, 5,
|
28
|
+
model << Conv2D.new(16, 5, padding: true)
|
29
29
|
model << BatchNormalization.new
|
30
30
|
model << ReLU.new
|
31
31
|
|
32
|
-
model << Conv2D.new(16, 5,
|
32
|
+
model << Conv2D.new(16, 5, padding: true)
|
33
33
|
model << BatchNormalization.new
|
34
34
|
model << ReLU.new
|
35
35
|
|
36
|
-
model << MaxPool2D.new(2
|
36
|
+
model << MaxPool2D.new(2)
|
37
37
|
|
38
|
-
model << Conv2D.new(32, 5,
|
38
|
+
model << Conv2D.new(32, 5, padding: true)
|
39
39
|
model << BatchNormalization.new
|
40
40
|
model << ReLU.new
|
41
41
|
|
42
|
-
model << Conv2D.new(32, 5,
|
42
|
+
model << Conv2D.new(32, 5, padding: true)
|
43
43
|
model << BatchNormalization.new
|
44
44
|
model << ReLU.new
|
45
45
|
|
data/examples/mnist_example2.rb
CHANGED
@@ -25,13 +25,13 @@ model = Model.new
|
|
25
25
|
|
26
26
|
model << InputLayer.new([28, 28, 1])
|
27
27
|
|
28
|
-
model << Conv2D.new(16, 5
|
28
|
+
model << Conv2D.new(16, 5)
|
29
29
|
model << BatchNormalization.new
|
30
30
|
model << ReLU.new
|
31
31
|
|
32
|
-
model << MaxPool2D.new(2
|
32
|
+
model << MaxPool2D.new(2)
|
33
33
|
|
34
|
-
model << Conv2D.new(32, 5
|
34
|
+
model << Conv2D.new(32, 5)
|
35
35
|
model << BatchNormalization.new
|
36
36
|
model << ReLU.new
|
37
37
|
|
data/lib/dnn.rb
CHANGED
data/lib/dnn/core/activations.rb
CHANGED
@@ -3,16 +3,10 @@ module DNN
|
|
3
3
|
Layer = Layers::Layer
|
4
4
|
OutputLayer = Layers::OutputLayer
|
5
5
|
|
6
|
-
|
7
|
-
module SigmoidFunction
|
6
|
+
class Sigmoid < Layer
|
8
7
|
def forward(x)
|
9
8
|
@out = 1.0 / (1 + NMath.exp(-x))
|
10
9
|
end
|
11
|
-
end
|
12
|
-
|
13
|
-
|
14
|
-
class Sigmoid < Layer
|
15
|
-
include SigmoidFunction
|
16
10
|
|
17
11
|
def backward(dout)
|
18
12
|
dout * (1.0 - @out) * @out
|
@@ -21,7 +15,7 @@ module DNN
|
|
21
15
|
|
22
16
|
|
23
17
|
class Tanh < Layer
|
24
|
-
include
|
18
|
+
include Xumo
|
25
19
|
|
26
20
|
def forward(x)
|
27
21
|
@x = x
|
@@ -50,7 +44,7 @@ module DNN
|
|
50
44
|
|
51
45
|
|
52
46
|
class LeakyReLU < Layer
|
53
|
-
include
|
47
|
+
include Xumo
|
54
48
|
|
55
49
|
def initialize(alpha = 0.3)
|
56
50
|
@alpha = alpha
|
@@ -112,8 +106,15 @@ module DNN
|
|
112
106
|
|
113
107
|
|
114
108
|
class SigmoidWithLoss < OutputLayer
|
115
|
-
include
|
116
|
-
|
109
|
+
include Xumo
|
110
|
+
|
111
|
+
def initialize
|
112
|
+
@sigmoid = Sigmoid.new
|
113
|
+
end
|
114
|
+
|
115
|
+
def forward(x)
|
116
|
+
@out = @sigmoid.forward(x)
|
117
|
+
end
|
117
118
|
|
118
119
|
def backward(y)
|
119
120
|
@out - y
|
data/lib/dnn/core/layers.rb
CHANGED
@@ -3,7 +3,7 @@ module DNN
|
|
3
3
|
|
4
4
|
#Super class of all optimizer classes.
|
5
5
|
class Layer
|
6
|
-
include
|
6
|
+
include Xumo
|
7
7
|
|
8
8
|
def initialize
|
9
9
|
@built = false
|
@@ -212,8 +212,8 @@ module DNN
|
|
212
212
|
end
|
213
213
|
|
214
214
|
def out_size(prev_w, prev_h, fil_w, fil_h, strides)
|
215
|
-
out_w = (prev_w - fil_w) / strides[
|
216
|
-
out_h = (prev_h - fil_h) / strides[
|
215
|
+
out_w = (prev_w - fil_w) / strides[0] + 1
|
216
|
+
out_h = (prev_h - fil_h) / strides[1] + 1
|
217
217
|
[out_w, out_h]
|
218
218
|
end
|
219
219
|
end
|
@@ -223,25 +223,24 @@ module DNN
|
|
223
223
|
include Initializers
|
224
224
|
include Convert
|
225
225
|
|
226
|
-
def initialize(num_filters,
|
226
|
+
def initialize(num_filters, filter_size,
|
227
227
|
weight_initializer: nil,
|
228
228
|
bias_initializer: nil,
|
229
|
-
strides:
|
229
|
+
strides: 1,
|
230
230
|
padding: false,
|
231
231
|
weight_decay: 0)
|
232
232
|
super()
|
233
233
|
@num_filters = num_filters
|
234
|
-
@
|
235
|
-
@filter_height = filter_height
|
234
|
+
@filter_size = filter_size.is_a?(Integer) ? [filter_size, filter_size] : filter_size
|
236
235
|
@weight_initializer = (weight_initializer || RandomNormal.new)
|
237
236
|
@bias_initializer = (bias_initializer || Zeros.new)
|
238
|
-
@strides = strides
|
237
|
+
@strides = strides.is_a?(Integer) ? [strides, strides] : strides
|
239
238
|
@padding = padding
|
240
239
|
@weight_decay = weight_decay
|
241
240
|
end
|
242
241
|
|
243
242
|
def self.load_hash(hash)
|
244
|
-
Conv2D.new(hash[:num_filters], hash[:
|
243
|
+
Conv2D.new(hash[:num_filters], hash[:filter_size],
|
245
244
|
weight_initializer: Util.load_hash(hash[:weight_initializer]),
|
246
245
|
bias_initializer: Util.load_hash(hash[:bias_initializer]),
|
247
246
|
strides: hash[:strides],
|
@@ -251,21 +250,21 @@ module DNN
|
|
251
250
|
|
252
251
|
def build(model)
|
253
252
|
super
|
254
|
-
|
255
|
-
@
|
253
|
+
prev_w, prev_h = prev_layer.shape[0..1]
|
254
|
+
@out_size = out_size(prev_w, prev_h, *@filter_size, @strides)
|
255
|
+
out_w, out_h = @out_size
|
256
256
|
if @padding
|
257
|
-
@pad = [
|
258
|
-
@
|
259
|
-
@out_height = prev_height
|
257
|
+
@pad = [prev_w - out_w, prev_h - out_h]
|
258
|
+
@out_size = [prev_w, prev_h]
|
260
259
|
end
|
261
260
|
end
|
262
261
|
|
263
262
|
def forward(x)
|
264
263
|
x = padding(x, @pad) if @padding
|
265
264
|
@x_shape = x.shape
|
266
|
-
@col = im2col(x,
|
265
|
+
@col = im2col(x, *@out_size, *@filter_size, @strides)
|
267
266
|
out = @col.dot(@params[:weight])
|
268
|
-
out.reshape(x.shape[0],
|
267
|
+
out.reshape(x.shape[0], *@out_size, out.shape[3])
|
269
268
|
end
|
270
269
|
|
271
270
|
def backward(dout)
|
@@ -277,20 +276,19 @@ module DNN
|
|
277
276
|
end
|
278
277
|
@grads[:bias] = dout.sum(0)
|
279
278
|
dcol = dout.dot(@params[:weight].transpose)
|
280
|
-
dx = col2im(dcol, @x_shape,
|
279
|
+
dx = col2im(dcol, @x_shape, *@out_size, *@filter_size, @strides)
|
281
280
|
@padding ? back_padding(dx, @pad) : dx
|
282
281
|
end
|
283
282
|
|
284
283
|
def shape
|
285
|
-
[
|
284
|
+
[*@out_size, @num_filters]
|
286
285
|
end
|
287
286
|
|
288
287
|
def to_hash
|
289
288
|
{
|
290
289
|
name: self.class.name,
|
291
290
|
num_filters: @num_filters,
|
292
|
-
|
293
|
-
filter_height: @filter_height,
|
291
|
+
filter_size: @filter_size,
|
294
292
|
weight_initializer: @weight_initializer.to_hash,
|
295
293
|
bias_initializer: @bias_initializer.to_hash,
|
296
294
|
strides: @strides,
|
@@ -303,7 +301,7 @@ module DNN
|
|
303
301
|
|
304
302
|
def init_params
|
305
303
|
num_prev_filter = prev_layer.shape[2]
|
306
|
-
@params[:weight] = SFloat.new(num_prev_filter * @
|
304
|
+
@params[:weight] = SFloat.new(num_prev_filter * @filter_size.reduce(:*), @num_filters)
|
307
305
|
@params[:bias] = SFloat.new(@num_filters)
|
308
306
|
@weight_initializer.init_param(self, :weight)
|
309
307
|
@bias_initializer.init_param(self, :bias)
|
@@ -314,50 +312,52 @@ module DNN
|
|
314
312
|
class MaxPool2D < Layer
|
315
313
|
include Convert
|
316
314
|
|
317
|
-
def initialize(
|
315
|
+
def initialize(pool_size, strides: nil, padding: false)
|
318
316
|
super()
|
319
|
-
@
|
320
|
-
@
|
321
|
-
|
317
|
+
@pool_size = pool_size.is_a?(Integer) ? [pool_size, pool_size] : pool_size
|
318
|
+
@strides = if strides
|
319
|
+
strides.is_a?(Integer) ? [strides, strides] : strides
|
320
|
+
else
|
321
|
+
@pool_size.clone
|
322
|
+
end
|
322
323
|
@padding = padding
|
323
324
|
end
|
324
325
|
|
325
326
|
def self.load_hash(hash)
|
326
|
-
MaxPool2D.new(hash[:
|
327
|
+
MaxPool2D.new(hash[:pool_size], strides: hash[:strides], padding: hash[:padding])
|
327
328
|
end
|
328
329
|
|
329
330
|
def build(model)
|
330
331
|
super
|
331
|
-
|
332
|
+
prev_w, prev_h = prev_layer.shape[0..1]
|
332
333
|
@num_channel = prev_layer.shape[2]
|
333
|
-
@
|
334
|
+
@out_size = out_size(prev_w, prev_h, *@pool_size, @strides)
|
335
|
+
out_w, out_h = @out_size
|
334
336
|
if @padding
|
335
|
-
@pad = [
|
336
|
-
@
|
337
|
-
@out_height = prev_height
|
337
|
+
@pad = [prev_w - out_w, prev_h - out_h]
|
338
|
+
@out_size = [prev_w, prev_h]
|
338
339
|
end
|
339
340
|
end
|
340
341
|
|
341
342
|
def forward(x)
|
342
343
|
x = padding(x, @pad) if @padding
|
343
344
|
@x_shape = x.shape
|
344
|
-
col = im2col(x,
|
345
|
-
col = col.reshape(x.shape[0] * @
|
345
|
+
col = im2col(x, *@out_size, *@pool_size, @strides)
|
346
|
+
col = col.reshape(x.shape[0] * @out_size.reduce(:*) * x.shape[3], @pool_size.reduce(:*))
|
346
347
|
@max_index = col.max_index(1)
|
347
|
-
col.max(1).reshape(x.shape[0],
|
348
|
+
col.max(1).reshape(x.shape[0], *@out_size, x.shape[3])
|
348
349
|
end
|
349
350
|
|
350
351
|
def backward(dout)
|
351
|
-
|
352
|
-
dmax = SFloat.zeros(dout.size * pool_size)
|
352
|
+
dmax = SFloat.zeros(dout.size * @pool_size.reduce(:*))
|
353
353
|
dmax[@max_index] = dout.flatten
|
354
|
-
dcol = dmax.reshape(dout.shape[0..2].reduce(:*), dout.shape[3] * pool_size)
|
355
|
-
dx = col2im(dcol, @x_shape,
|
354
|
+
dcol = dmax.reshape(dout.shape[0..2].reduce(:*), dout.shape[3] * @pool_size.reduce(:*))
|
355
|
+
dx = col2im(dcol, @x_shape, *@out_size, *@pool_size, @strides)
|
356
356
|
@padding ? back_padding(dx, @pad) : dx
|
357
357
|
end
|
358
358
|
|
359
359
|
def shape
|
360
|
-
[
|
360
|
+
[*@out_size, @num_channel]
|
361
361
|
end
|
362
362
|
|
363
363
|
def to_hash
|
@@ -375,34 +375,36 @@ module DNN
|
|
375
375
|
class UnPool2D < Layer
|
376
376
|
include Convert
|
377
377
|
|
378
|
-
def initialize(
|
378
|
+
def initialize(unpool_size)
|
379
379
|
super()
|
380
|
-
@
|
381
|
-
@unpool_height = unpool_height
|
380
|
+
@unpool_size = unpool_size.is_a?(Integer) ? [unpool_size, unpool_size] : unpool_size
|
382
381
|
end
|
383
382
|
|
384
383
|
def self.load_hash(hash)
|
385
|
-
UnPool2D.new(hash[:
|
384
|
+
UnPool2D.new(hash[:unpool_size])
|
386
385
|
end
|
387
386
|
|
388
387
|
def build(model)
|
389
388
|
super
|
390
|
-
|
391
|
-
|
392
|
-
|
393
|
-
|
389
|
+
prev_w, prev_h = prev_layer.shape[0..1]
|
390
|
+
unpool_w, unpool_h = @unpool_size
|
391
|
+
out_w = prev_w * unpool_w
|
392
|
+
out_h = prev_h * unpool_h
|
393
|
+
@out_size = [out_w, out_h]
|
394
394
|
@num_channel = prev_layer.shape[2]
|
395
395
|
end
|
396
396
|
|
397
397
|
def forward(x)
|
398
|
-
|
399
|
-
|
398
|
+
@x_shape = x.shape
|
399
|
+
unpool_w, unpool_h = @unpool_size
|
400
|
+
x2 = SFloat.zeros(x.shape[0], x.shape[1], unpool_w, x.shape[2], unpool_h, @num_channel)
|
400
401
|
x2[true, true, 0, true, 0, true] = x
|
401
|
-
x2.reshape(x.shape[0],
|
402
|
+
x2.reshape(x.shape[0], *@out_size, x.shape[3])
|
402
403
|
end
|
403
404
|
|
404
405
|
def backward(dout)
|
405
|
-
|
406
|
+
unpool_w, unpool_h = @unpool_size
|
407
|
+
dout = dout.reshape(dout.shape[0], @x_shape[0], unpool_w, @x_shape[1], unpool_h, @num_channel)
|
406
408
|
dout[true, true, 0, true, 0, true].clone
|
407
409
|
end
|
408
410
|
|
@@ -413,8 +415,7 @@ module DNN
|
|
413
415
|
def to_hash
|
414
416
|
{
|
415
417
|
name: self.class.name,
|
416
|
-
|
417
|
-
unpool_height: @unpool_height,
|
418
|
+
unpool_size: @unpool_size,
|
418
419
|
}
|
419
420
|
end
|
420
421
|
end
|
@@ -500,7 +501,7 @@ module DNN
|
|
500
501
|
end
|
501
502
|
|
502
503
|
def backward(dout)
|
503
|
-
dout[@mask] = 0 if @model.training
|
504
|
+
dout[@mask] = 0 if @model.training?
|
504
505
|
dout
|
505
506
|
end
|
506
507
|
|
data/lib/dnn/core/model.rb
CHANGED
data/lib/dnn/core/optimizers.rb
CHANGED
data/lib/dnn/core/version.rb
CHANGED
@@ -17,7 +17,7 @@ VALUE mnist_load_images(VALUE self, VALUE rb_bin, VALUE rb_num_images, VALUE rb_
|
|
17
17
|
VALUE rb_na;
|
18
18
|
narray_data_t* na_data;
|
19
19
|
|
20
|
-
sprintf(script, "
|
20
|
+
sprintf(script, "Xumo::UInt8.zeros(%d, %d, %d)", num_images, cols, rows);
|
21
21
|
rb_na = rb_eval_string(&script[0]);
|
22
22
|
na_data = RNARRAY_DATA(rb_na);
|
23
23
|
|
@@ -35,7 +35,7 @@ VALUE mnist_load_labels(VALUE self, VALUE rb_bin, VALUE rb_num_labels) {
|
|
35
35
|
VALUE rb_na;
|
36
36
|
narray_data_t* na_data;
|
37
37
|
|
38
|
-
sprintf(script, "
|
38
|
+
sprintf(script, "Xumo::UInt8.zeros(%d)", num_labels);
|
39
39
|
rb_na = rb_eval_string(&script[0]);
|
40
40
|
na_data = RNARRAY_DATA(rb_na);
|
41
41
|
|
@@ -57,13 +57,13 @@ VALUE cifar10_load(VALUE self, VALUE rb_bin, VALUE rb_num_datas) {
|
|
57
57
|
int k = 0;
|
58
58
|
int size = CIFAR10_WIDTH * CIFAR10_HEIGHT * CIFAR10_CHANNEL;
|
59
59
|
|
60
|
-
sprintf(script, "
|
60
|
+
sprintf(script, "Xumo::UInt8.zeros(%d, %d, %d, %d)", num_datas, CIFAR10_CHANNEL, CIFAR10_WIDTH, CIFAR10_HEIGHT);
|
61
61
|
rb_na_x = rb_eval_string(&script[0]);
|
62
62
|
na_data_x = RNARRAY_DATA(rb_na_x);
|
63
63
|
for(i = 0; i < 64; i++) {
|
64
64
|
script[i] = 0;
|
65
65
|
}
|
66
|
-
sprintf(script, "
|
66
|
+
sprintf(script, "Xumo::UInt8.zeros(%d)", num_datas);
|
67
67
|
rb_na_y = rb_eval_string(&script[0]);
|
68
68
|
na_data_y = RNARRAY_DATA(rb_na_y);
|
69
69
|
|
@@ -128,7 +128,7 @@ extern int na_debug_flag;
|
|
128
128
|
|
129
129
|
#ifndef NARRAY_C
|
130
130
|
extern VALUE numo_cNArray;
|
131
|
-
extern VALUE
|
131
|
+
extern VALUE rb_mXumo;
|
132
132
|
extern VALUE nary_eCastError;
|
133
133
|
extern VALUE nary_eShapeError;
|
134
134
|
extern VALUE nary_eOperationError;
|
@@ -140,7 +140,7 @@ extern const rb_data_type_t na_data_type;
|
|
140
140
|
#endif
|
141
141
|
|
142
142
|
#define cNArray numo_cNArray
|
143
|
-
#define
|
143
|
+
#define mXumo rb_mXumo
|
144
144
|
//#define na_upcast(x,y) numo_na_upcast(x,y)
|
145
145
|
|
146
146
|
/* global variables within this module */
|
@@ -17,7 +17,7 @@ VALUE image_io_read(VALUE self, VALUE rb_file_name) {
|
|
17
17
|
VALUE rb_na;
|
18
18
|
narray_data_t* na_data;
|
19
19
|
pixels = stbi_load(file_name, &width, &height, &bpp, 3);
|
20
|
-
sprintf(script, "
|
20
|
+
sprintf(script, "Xumo::UInt8.zeros(%d, %d, 3)", width, height);
|
21
21
|
rb_na = rb_eval_string((char*)script);
|
22
22
|
na_data = RNARRAY_DATA(rb_na);
|
23
23
|
memcpy(na_data->ptr, pixels, na_data->base.size);
|
@@ -128,7 +128,7 @@ extern int na_debug_flag;
|
|
128
128
|
|
129
129
|
#ifndef NARRAY_C
|
130
130
|
extern VALUE numo_cNArray;
|
131
|
-
extern VALUE
|
131
|
+
extern VALUE rb_mXumo;
|
132
132
|
extern VALUE nary_eCastError;
|
133
133
|
extern VALUE nary_eShapeError;
|
134
134
|
extern VALUE nary_eOperationError;
|
@@ -140,7 +140,7 @@ extern const rb_data_type_t na_data_type;
|
|
140
140
|
#endif
|
141
141
|
|
142
142
|
#define cNArray numo_cNArray
|
143
|
-
#define
|
143
|
+
#define mXumo rb_mXumo
|
144
144
|
//#define na_upcast(x,y) numo_na_upcast(x,y)
|
145
145
|
|
146
146
|
/* global variables within this module */
|
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.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- unagiootoro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-07-
|
11
|
+
date: 2018-07-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-narray
|