ruby-dnn 0.5.9 → 0.5.10
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 +21 -21
- data/LIB-API-Reference.ja.md +15 -15
- data/examples/cifar10_example.rb +2 -3
- data/examples/mnist_example.rb +2 -3
- data/examples/mnist_example2.rb +2 -2
- data/examples/xor_example.rb +2 -2
- data/lib/dnn/core/activations.rb +7 -13
- data/lib/dnn/core/cnn_layers.rb +8 -8
- data/lib/dnn/core/layers.rb +11 -13
- data/lib/dnn/core/model.rb +4 -6
- data/lib/dnn/core/optimizers.rb +3 -5
- data/lib/dnn/core/rnn_layers.rb +20 -20
- data/lib/dnn/version.rb +1 -1
- 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: c7df0888afbde3ac66d5aaa66d7ef87f66edd58156945d4f1663552994e0d12f
|
4
|
+
data.tar.gz: 4c638c3f14f4cb0bea17ec31225c19aa2ca30b14a4e81c12bb8ee791137d3072
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4d3672bcb7f3763bd269d1adda133f4e90a8e1ab70f91bc5fb971eb18dfa346ebed16e1b88269fcb923d97fb2cdf131d680f74c487546ec0d121be8254a85e6f
|
7
|
+
data.tar.gz: 0f1f13a53ffb712ac97fea6cd9d06cb10d1076c032728cdc056f7e5489e8a97ac86e6000ce66a62858c1320441ac0b8562d3c47f44ec14eca0203995a80b48e5
|
data/API-Reference.ja.md
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
ruby-dnnのAPIリファレンスです。このリファレンスでは、APIを利用するうえで必要となるクラスとメソッドしか記載していません。
|
3
3
|
そのため、プログラムの詳細が必要な場合は、ソースコードを参照してください。
|
4
4
|
|
5
|
-
最終更新バージョン:0.5.
|
5
|
+
最終更新バージョン:0.5.10
|
6
6
|
|
7
7
|
# module DNN
|
8
8
|
ruby-dnnの名前空間をなすモジュールです。
|
@@ -101,9 +101,9 @@ bool
|
|
101
101
|
## def train(x, y, epochs, batch_size: 1, test: nil, verbose: true, batch_proc: nil, &epoch_proc)
|
102
102
|
コンパイルしたモデルを用いて学習を行います。
|
103
103
|
### arguments
|
104
|
-
* SFloat x
|
104
|
+
* Numo::SFloat x
|
105
105
|
トレーニング用入力データ。
|
106
|
-
* SFloat y
|
106
|
+
* Numo::SFloat y
|
107
107
|
トレーニング用出力データ。
|
108
108
|
* epochs
|
109
109
|
学習回数。
|
@@ -124,9 +124,9 @@ epoch_proc
|
|
124
124
|
## def train_on_batch(x, y, batch_size, &batch_proc)
|
125
125
|
入力されたバッチデータをもとに、一度だけ学習を行います。
|
126
126
|
### arguments
|
127
|
-
* SFloat x
|
127
|
+
* Numo::SFloat x
|
128
128
|
トレーニング用入力バッチデータ。
|
129
|
-
* SFloat y
|
129
|
+
* Numo::SFloat y
|
130
130
|
トレーニング用出力バッチデータ。
|
131
131
|
* Integer batch_size
|
132
132
|
学習に使用するミニバッチの数。
|
@@ -139,9 +139,9 @@ Integer
|
|
139
139
|
## def accurate(x, y, batch_size = nil, &batch_proc)
|
140
140
|
学習結果をもとに認識率を返します。
|
141
141
|
### arguments
|
142
|
-
* SFloat x
|
142
|
+
* Numo::SFloat x
|
143
143
|
テスト用入力データ。
|
144
|
-
* SFloat y
|
144
|
+
* Numo::SFloat y
|
145
145
|
テスト用出力データ。
|
146
146
|
* batch_size
|
147
147
|
ミニバッチの数。学習を行っていないモデルのテストを行いたい場合等に使用します。
|
@@ -155,20 +155,20 @@ Float
|
|
155
155
|
モデルを使用して、結果の推論を行います。
|
156
156
|
入力データは、バッチデータである必要があります。
|
157
157
|
### arguments
|
158
|
-
* SFloat x
|
158
|
+
* Numo::SFloat x
|
159
159
|
推論用入力データ。
|
160
160
|
### return
|
161
|
-
SFloat
|
161
|
+
Numo::SFloat
|
162
162
|
推論結果を返します。
|
163
163
|
|
164
164
|
## def predict1(x)
|
165
165
|
モデルを使用して、結果の推論を行います。
|
166
166
|
predictとは異なり、一つの入力データに対して、一つの出力データを返します。
|
167
167
|
### arguments
|
168
|
-
* SFloat x
|
168
|
+
* Numo::SFloat x
|
169
169
|
推論用入力データ。
|
170
170
|
### return
|
171
|
-
SFloat
|
171
|
+
Numo::SFloat
|
172
172
|
推論結果を返します。
|
173
173
|
|
174
174
|
|
@@ -200,19 +200,19 @@ bool
|
|
200
200
|
## abstruct def forward(x)
|
201
201
|
順方向伝搬を行うメソッドです。Layerクラスを継承するクラスは、このメソッドを実装する必要があります。
|
202
202
|
### arguments
|
203
|
-
* SFloat x
|
203
|
+
* Numo::SFloat x
|
204
204
|
入力データ。
|
205
205
|
### return
|
206
|
-
SFloat
|
206
|
+
Numo::SFloat
|
207
207
|
出力データ。
|
208
208
|
|
209
209
|
## abstruct def backward(dout)
|
210
210
|
逆方向伝搬を行うメソッドです。Layerクラスを継承するクラスは、このメソッドを実装する必要があります。
|
211
211
|
### arguments
|
212
|
-
* SFloat dout
|
212
|
+
* Numo::SFloat dout
|
213
213
|
逆方向から伝搬してきた微分値。
|
214
214
|
### return
|
215
|
-
SFloat
|
215
|
+
Numo::SFloat
|
216
216
|
逆方向に伝搬する微分値。
|
217
217
|
|
218
218
|
## def shape
|
@@ -458,7 +458,7 @@ N次元のデータを平坦化します。
|
|
458
458
|
## abstruct def backward(y)
|
459
459
|
出力層の活性化関数と損失関数を合わせたものを微分した導関数を用いて、教師データの出力データを逆方向に伝搬します。
|
460
460
|
### arguments
|
461
|
-
SFloat y
|
461
|
+
Numo::SFloat y
|
462
462
|
出力データ。
|
463
463
|
### return
|
464
464
|
出力層の活性化関数と損失関数の微分値。
|
@@ -466,7 +466,7 @@ SFloat y
|
|
466
466
|
## abstruct def loss
|
467
467
|
損失関数の値を取得します。
|
468
468
|
### arguments
|
469
|
-
SFloat y
|
469
|
+
Numo::SFloat y
|
470
470
|
出力データ。
|
471
471
|
### return
|
472
472
|
損失関数の値。
|
@@ -575,7 +575,7 @@ Float alpha
|
|
575
575
|
更新対象のパラメータを持つレイヤーを指定します。
|
576
576
|
* Symbol param_key
|
577
577
|
更新す対象のパラメータの名前を指定します。
|
578
|
-
* SFloat param
|
578
|
+
* Numo::SFloat param
|
579
579
|
更新するパラメータです。
|
580
580
|
|
581
581
|
|
@@ -731,9 +731,9 @@ Float beta2
|
|
731
731
|
## def self.get_minibatch(x, y, batch_size)
|
732
732
|
batch_size分のミニバッチを取得します。
|
733
733
|
### arguments
|
734
|
-
* SFloat x
|
734
|
+
* Numo::SFloat x
|
735
735
|
教師データの入力データ。
|
736
|
-
* SFloat y
|
736
|
+
* Numo::SFloat y
|
737
737
|
教師データの出力データ。
|
738
738
|
* Integer batch_size
|
739
739
|
ミニバッチのサイズ。
|
@@ -744,7 +744,7 @@ Array
|
|
744
744
|
## def self.to_categorical(y, num_classes, type = nil)
|
745
745
|
ラベルをnum_classesのベクトルにカテゴライズします。
|
746
746
|
### arguments
|
747
|
-
* SFloat y
|
747
|
+
* Numo::SFloat y
|
748
748
|
教師データの出力データ。
|
749
749
|
* Integer num_classes
|
750
750
|
カテゴライズするクラス数。
|
data/LIB-API-Reference.ja.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# LIB-APIリファレンス
|
2
2
|
ruby-dnnの付属ライブラリのリファレンスです。
|
3
|
-
最終更新バージョン:0.
|
3
|
+
最終更新バージョン:0.5.10
|
4
4
|
|
5
5
|
|
6
6
|
# dnn/lib/mnist
|
@@ -20,9 +20,9 @@ MNISTを扱うモジュールです。
|
|
20
20
|
Array
|
21
21
|
[イメージデータ, ラベルデータ]の形式で取得します。
|
22
22
|
* イメージデータ
|
23
|
-
UInt8の[60000, 28, 28]の形式
|
23
|
+
Numo::UInt8の[60000, 28, 28]の形式
|
24
24
|
* テストデータ
|
25
|
-
UInt8の[60000]の形式
|
25
|
+
Numo::UInt8の[60000]の形式
|
26
26
|
|
27
27
|
## def self.load_test
|
28
28
|
テスト用データを取得します。
|
@@ -32,9 +32,9 @@ Array
|
|
32
32
|
Array
|
33
33
|
[イメージデータ, ラベルデータ]の形式で取得します。
|
34
34
|
* イメージデータ
|
35
|
-
UInt8の[10000, 28, 28]の形式
|
35
|
+
Numo::UInt8の[10000, 28, 28]の形式
|
36
36
|
* テストデータ
|
37
|
-
UInt8の[10000]の形式
|
37
|
+
Numo::UInt8の[10000]の形式
|
38
38
|
|
39
39
|
|
40
40
|
# dnn/lib/cifar10
|
@@ -54,9 +54,9 @@ CIFAR-10を扱うモジュールです。
|
|
54
54
|
Array
|
55
55
|
[イメージデータ, ラベルデータ]の形式で取得します。
|
56
56
|
* イメージデータ
|
57
|
-
UInt8の[50000, 3, 32, 32]の形式
|
57
|
+
Numo::UInt8の[50000, 3, 32, 32]の形式
|
58
58
|
* テストデータ
|
59
|
-
UInt8の[50000]の形式
|
59
|
+
Numo::UInt8の[50000]の形式
|
60
60
|
|
61
61
|
## def self.load_test
|
62
62
|
テスト用データを取得します。
|
@@ -66,9 +66,9 @@ Array
|
|
66
66
|
Array
|
67
67
|
[イメージデータ, ラベルデータ]の形式で取得します。
|
68
68
|
* イメージデータ
|
69
|
-
UInt8の[10000, 32, 32, 3]の形式
|
69
|
+
Numo::UInt8の[10000, 32, 32, 3]の形式
|
70
70
|
* テストデータ
|
71
|
-
UInt8の[10000]の形式
|
71
|
+
Numo::UInt8の[10000]の形式
|
72
72
|
|
73
73
|
|
74
74
|
# dnn/lib/image_io
|
@@ -78,20 +78,20 @@ Array
|
|
78
78
|
# module ImageIO
|
79
79
|
|
80
80
|
## def self.read(file_name)
|
81
|
-
画像をUInt8形式で読み込みます。
|
81
|
+
画像をNumo::UInt8形式で読み込みます。
|
82
82
|
### arguments
|
83
83
|
* String file_name
|
84
84
|
読み込む画像のファイル名。
|
85
85
|
### return
|
86
|
-
UInt8
|
87
|
-
[height, width, rgb]のUInt8配列。
|
86
|
+
Numo::UInt8
|
87
|
+
[height, width, rgb]のNumo::UInt8配列。
|
88
88
|
|
89
89
|
## def self.write(file_name, img, quality: 100)
|
90
|
-
UInt8形式の画像を書き込みます。
|
90
|
+
Numo::UInt8形式の画像を書き込みます。
|
91
91
|
### arguments
|
92
92
|
* String file_name
|
93
93
|
書き込む画像のファイル名。
|
94
|
-
* UInt8 img
|
95
|
-
[height, width, rgb]のUInt8配列。
|
94
|
+
* Numo::UInt8 img
|
95
|
+
[height, width, rgb]のNumo::UInt8配列。
|
96
96
|
* Integer quality: 100
|
97
97
|
画像をJPEGで書き込む場合のクオリティ。
|
data/examples/cifar10_example.rb
CHANGED
@@ -2,7 +2,6 @@ require "dnn"
|
|
2
2
|
require "dnn/lib/cifar10"
|
3
3
|
#require "numo/linalg/autoloader"
|
4
4
|
|
5
|
-
include Numo
|
6
5
|
include DNN::Layers
|
7
6
|
include DNN::Activations
|
8
7
|
include DNN::Optimizers
|
@@ -12,8 +11,8 @@ CIFAR10 = DNN::CIFAR10
|
|
12
11
|
x_train, y_train = CIFAR10.load_train
|
13
12
|
x_test, y_test = CIFAR10.load_test
|
14
13
|
|
15
|
-
x_train = SFloat.cast(x_train)
|
16
|
-
x_test = SFloat.cast(x_test)
|
14
|
+
x_train = Numo::SFloat.cast(x_train)
|
15
|
+
x_test = Numo::SFloat.cast(x_test)
|
17
16
|
|
18
17
|
x_train /= 255
|
19
18
|
x_test /= 255
|
data/examples/mnist_example.rb
CHANGED
@@ -2,7 +2,6 @@ require "dnn"
|
|
2
2
|
require "dnn/lib/mnist"
|
3
3
|
#require "numo/linalg/autoloader"
|
4
4
|
|
5
|
-
include Numo
|
6
5
|
include DNN::Layers
|
7
6
|
include DNN::Activations
|
8
7
|
include DNN::Optimizers
|
@@ -12,8 +11,8 @@ MNIST = DNN::MNIST
|
|
12
11
|
x_train, y_train = MNIST.load_train
|
13
12
|
x_test, y_test = MNIST.load_test
|
14
13
|
|
15
|
-
x_train = SFloat.cast(x_train).reshape(x_train.shape[0], 784)
|
16
|
-
x_test = SFloat.cast(x_test).reshape(x_test.shape[0], 784)
|
14
|
+
x_train = Numo::SFloat.cast(x_train).reshape(x_train.shape[0], 784)
|
15
|
+
x_test = Numo::SFloat.cast(x_test).reshape(x_test.shape[0], 784)
|
17
16
|
|
18
17
|
x_train /= 255
|
19
18
|
x_test /= 255
|
data/examples/mnist_example2.rb
CHANGED
@@ -12,8 +12,8 @@ MNIST = DNN::MNIST
|
|
12
12
|
x_train, y_train = MNIST.load_train
|
13
13
|
x_test, y_test = MNIST.load_test
|
14
14
|
|
15
|
-
x_train = SFloat.cast(x_train).reshape(x_train.shape[0], 28, 28, 1)
|
16
|
-
x_test = SFloat.cast(x_test).reshape(x_test.shape[0], 28, 28, 1)
|
15
|
+
x_train = Numo::SFloat.cast(x_train).reshape(x_train.shape[0], 28, 28, 1)
|
16
|
+
x_test = Numo::SFloat.cast(x_test).reshape(x_test.shape[0], 28, 28, 1)
|
17
17
|
|
18
18
|
x_train /= 255
|
19
19
|
x_test /= 255
|
data/examples/xor_example.rb
CHANGED
@@ -6,8 +6,8 @@ include DNN::Activations
|
|
6
6
|
include DNN::Optimizers
|
7
7
|
Model = DNN::Model
|
8
8
|
|
9
|
-
x = SFloat[[0, 0], [1, 0], [0, 1], [1, 1]]
|
10
|
-
y = SFloat[[0], [1], [1], [0]]
|
9
|
+
x = Numo::SFloat[[0, 0], [1, 0], [0, 1], [1, 1]]
|
10
|
+
y = Numo::SFloat[[0], [1], [1], [0]]
|
11
11
|
|
12
12
|
model = Model.new
|
13
13
|
|
data/lib/dnn/core/activations.rb
CHANGED
@@ -4,7 +4,7 @@ module DNN
|
|
4
4
|
|
5
5
|
class Sigmoid < Layer
|
6
6
|
def forward(x)
|
7
|
-
@out = 1 / (1 + NMath.exp(-x))
|
7
|
+
@out = 1 / (1 + Xumo::NMath.exp(-x))
|
8
8
|
end
|
9
9
|
|
10
10
|
def backward(dout)
|
@@ -14,15 +14,13 @@ module DNN
|
|
14
14
|
|
15
15
|
|
16
16
|
class Tanh < Layer
|
17
|
-
include Xumo
|
18
|
-
|
19
17
|
def forward(x)
|
20
18
|
@x = x
|
21
|
-
NMath.tanh(x)
|
19
|
+
Xumo::NMath.tanh(x)
|
22
20
|
end
|
23
21
|
|
24
22
|
def backward(dout)
|
25
|
-
dout * (1.0 / NMath.cosh(@x)**2)
|
23
|
+
dout * (1.0 / Xumo::NMath.cosh(@x)**2)
|
26
24
|
end
|
27
25
|
end
|
28
26
|
|
@@ -43,8 +41,6 @@ module DNN
|
|
43
41
|
|
44
42
|
|
45
43
|
class LeakyReLU < Layer
|
46
|
-
include Xumo
|
47
|
-
|
48
44
|
attr_reader :alpha
|
49
45
|
|
50
46
|
def initialize(alpha = 0.3)
|
@@ -57,7 +53,7 @@ module DNN
|
|
57
53
|
|
58
54
|
def forward(x)
|
59
55
|
@x = x.clone
|
60
|
-
a = SFloat.ones(x.shape)
|
56
|
+
a = Xumo::SFloat.ones(x.shape)
|
61
57
|
a[x <= 0] = @alpha
|
62
58
|
x * a
|
63
59
|
end
|
@@ -144,7 +140,7 @@ module DNN
|
|
144
140
|
|
145
141
|
class SoftmaxWithLoss < Layers::OutputLayer
|
146
142
|
def forward(x)
|
147
|
-
@out = NMath.exp(x) / NMath.exp(x).sum(1).reshape(x.shape[0], 1)
|
143
|
+
@out = Xumo::NMath.exp(x) / Xumo::NMath.exp(x).sum(1).reshape(x.shape[0], 1)
|
148
144
|
end
|
149
145
|
|
150
146
|
def backward(y)
|
@@ -153,14 +149,12 @@ module DNN
|
|
153
149
|
|
154
150
|
def loss(y)
|
155
151
|
batch_size = y.shape[0]
|
156
|
-
-(y * NMath.log(@out + 1e-7)).sum / batch_size + ridge
|
152
|
+
-(y * Xumo::NMath.log(@out + 1e-7)).sum / batch_size + ridge
|
157
153
|
end
|
158
154
|
end
|
159
155
|
|
160
156
|
|
161
157
|
class SigmoidWithLoss < Layers::OutputLayer
|
162
|
-
include Xumo
|
163
|
-
|
164
158
|
def initialize
|
165
159
|
@sigmoid = Sigmoid.new
|
166
160
|
end
|
@@ -175,7 +169,7 @@ module DNN
|
|
175
169
|
|
176
170
|
def loss(y)
|
177
171
|
batch_size = y.shape[0]
|
178
|
-
-(y * NMath.log(@out + 1e-7) + (1 - y) * NMath.log(1 - @out + 1e-7)).sum / batch_size + ridge
|
172
|
+
-(y * Xumo::NMath.log(@out + 1e-7) + (1 - y) * Xumo::NMath.log(1 - @out + 1e-7)).sum / batch_size + ridge
|
179
173
|
end
|
180
174
|
end
|
181
175
|
|
data/lib/dnn/core/cnn_layers.rb
CHANGED
@@ -7,7 +7,7 @@ module DNN
|
|
7
7
|
def im2col(img, out_h, out_w, fil_h, fil_w, strides)
|
8
8
|
bsize = img.shape[0]
|
9
9
|
ch = img.shape[3]
|
10
|
-
col = SFloat.zeros(bsize, ch, fil_h, fil_w, out_h, out_w)
|
10
|
+
col = Xumo::SFloat.zeros(bsize, ch, fil_h, fil_w, out_h, out_w)
|
11
11
|
img = img.transpose(0, 3, 1, 2)
|
12
12
|
(0...fil_h).each do |i|
|
13
13
|
i_range = (i...(i + strides[0] * out_h)).step(strides[0]).to_a
|
@@ -22,7 +22,7 @@ module DNN
|
|
22
22
|
def col2im(col, img_shape, out_h, out_w, fil_h, fil_w, strides)
|
23
23
|
bsize, img_h, img_w, ch = img_shape
|
24
24
|
col = col.reshape(bsize, out_h, out_w, fil_h, fil_w, ch).transpose(0, 5, 3, 4, 1, 2)
|
25
|
-
img = SFloat.zeros(bsize, ch, img_h, img_w)
|
25
|
+
img = Xumo::SFloat.zeros(bsize, ch, img_h, img_w)
|
26
26
|
(0...fil_h).each do |i|
|
27
27
|
i_range = (i...(i + strides[0] * out_h)).step(strides[0]).to_a
|
28
28
|
(0...fil_w).each do |j|
|
@@ -35,7 +35,7 @@ module DNN
|
|
35
35
|
|
36
36
|
def padding(img, pad)
|
37
37
|
bsize, img_h, img_w, ch = img.shape
|
38
|
-
img2 = SFloat.zeros(bsize, img_h + pad[0], img_w + pad[1], ch)
|
38
|
+
img2 = Xumo::SFloat.zeros(bsize, img_h + pad[0], img_w + pad[1], ch)
|
39
39
|
i_begin = pad[0] / 2
|
40
40
|
i_end = i_begin + img_h
|
41
41
|
j_begin = pad[1] / 2
|
@@ -152,8 +152,8 @@ module DNN
|
|
152
152
|
|
153
153
|
def init_params
|
154
154
|
num_prev_filter = prev_layer.shape[2]
|
155
|
-
@params[:weight] = SFloat.new(num_prev_filter * @filter_size.reduce(:*), @num_filters)
|
156
|
-
@params[:bias] = SFloat.new(@num_filters)
|
155
|
+
@params[:weight] = Xumo::SFloat.new(num_prev_filter * @filter_size.reduce(:*), @num_filters)
|
156
|
+
@params[:bias] = Xumo::SFloat.new(@num_filters)
|
157
157
|
@weight_initializer.init_param(self, :weight)
|
158
158
|
@bias_initializer.init_param(self, :bias)
|
159
159
|
end
|
@@ -230,7 +230,7 @@ module DNN
|
|
230
230
|
end
|
231
231
|
|
232
232
|
def backward(dout)
|
233
|
-
dmax = SFloat.zeros(dout.size * @pool_size.reduce(:*))
|
233
|
+
dmax = Xumo::SFloat.zeros(dout.size * @pool_size.reduce(:*))
|
234
234
|
dmax[@max_index] = dout.flatten
|
235
235
|
dcol = dmax.reshape(dout.shape[0..2].reduce(:*), dout.shape[3] * @pool_size.reduce(:*))
|
236
236
|
super(dcol)
|
@@ -251,7 +251,7 @@ module DNN
|
|
251
251
|
def backward(dout)
|
252
252
|
row_length = @pool_size.reduce(:*)
|
253
253
|
dout /= row_length
|
254
|
-
davg = SFloat.zeros(dout.size, row_length)
|
254
|
+
davg = Xumo::SFloat.zeros(dout.size, row_length)
|
255
255
|
row_length.times do |i|
|
256
256
|
davg[true, i] = dout.flatten
|
257
257
|
end
|
@@ -286,7 +286,7 @@ module DNN
|
|
286
286
|
def forward(x)
|
287
287
|
@x_shape = x.shape
|
288
288
|
unpool_h, unpool_w = @unpool_size
|
289
|
-
x2 = SFloat.zeros(x.shape[0], x.shape[1], unpool_h, x.shape[2], unpool_w, @num_channel)
|
289
|
+
x2 = Xumo::SFloat.zeros(x.shape[0], x.shape[1], unpool_h, x.shape[2], unpool_w, @num_channel)
|
290
290
|
x2[true, true, 0, true, 0, true] = x
|
291
291
|
x2.reshape(x.shape[0], *@out_size, x.shape[3])
|
292
292
|
end
|
data/lib/dnn/core/layers.rb
CHANGED
@@ -3,8 +3,6 @@ module DNN
|
|
3
3
|
|
4
4
|
# Super class of all optimizer classes.
|
5
5
|
class Layer
|
6
|
-
include Xumo
|
7
|
-
|
8
6
|
def initialize
|
9
7
|
@built = false
|
10
8
|
end
|
@@ -161,8 +159,8 @@ module DNN
|
|
161
159
|
|
162
160
|
def init_params
|
163
161
|
num_prev_nodes = prev_layer.shape[0]
|
164
|
-
@params[:weight] = SFloat.new(num_prev_nodes, @num_nodes)
|
165
|
-
@params[:bias] = SFloat.new(@num_nodes)
|
162
|
+
@params[:weight] = Xumo::SFloat.new(num_prev_nodes, @num_nodes)
|
163
|
+
@params[:bias] = Xumo::SFloat.new(@num_nodes)
|
166
164
|
@weight_initializer.init_param(self, :weight)
|
167
165
|
@bias_initializer.init_param(self, :bias)
|
168
166
|
end
|
@@ -238,7 +236,7 @@ module DNN
|
|
238
236
|
|
239
237
|
def forward(x)
|
240
238
|
if @model.training?
|
241
|
-
@mask = SFloat.ones(*x.shape).rand < @dropout_ratio
|
239
|
+
@mask = Xumo::SFloat.ones(*x.shape).rand < @dropout_ratio
|
242
240
|
x[@mask] = 0
|
243
241
|
else
|
244
242
|
x *= (1 - @dropout_ratio)
|
@@ -261,8 +259,8 @@ module DNN
|
|
261
259
|
attr_reader :momentum
|
262
260
|
|
263
261
|
def self.load_hash(hash)
|
264
|
-
running_mean = SFloat.cast(hash[:running_mean])
|
265
|
-
running_var = SFloat.cast(hash[:running_var])
|
262
|
+
running_mean = Xumo::SFloat.cast(hash[:running_mean])
|
263
|
+
running_var = Xumo::SFloat.cast(hash[:running_var])
|
266
264
|
self.new(momentum: hash[:momentum], running_mean: running_mean, running_var: running_var)
|
267
265
|
end
|
268
266
|
|
@@ -275,8 +273,8 @@ module DNN
|
|
275
273
|
|
276
274
|
def build(model)
|
277
275
|
super
|
278
|
-
@running_mean ||= SFloat.zeros(*shape)
|
279
|
-
@running_var ||= SFloat.zeros(*shape)
|
276
|
+
@running_mean ||= Xumo::SFloat.zeros(*shape)
|
277
|
+
@running_var ||= Xumo::SFloat.zeros(*shape)
|
280
278
|
end
|
281
279
|
|
282
280
|
def forward(x)
|
@@ -284,14 +282,14 @@ module DNN
|
|
284
282
|
mean = x.mean(0)
|
285
283
|
@xc = x - mean
|
286
284
|
var = (@xc**2).mean(0)
|
287
|
-
@std = NMath.sqrt(var + 1e-7)
|
285
|
+
@std = Xumo::NMath.sqrt(var + 1e-7)
|
288
286
|
xn = @xc / @std
|
289
287
|
@xn = xn
|
290
288
|
@running_mean = @momentum * @running_mean + (1 - @momentum) * mean
|
291
289
|
@running_var = @momentum * @running_var + (1 - @momentum) * var
|
292
290
|
else
|
293
291
|
xc = x - @running_mean
|
294
|
-
xn = xc / NMath.sqrt(@running_var + 1e-7)
|
292
|
+
xn = xc / Xumo::NMath.sqrt(@running_var + 1e-7)
|
295
293
|
end
|
296
294
|
@params[:gamma] * xn + @params[:beta]
|
297
295
|
end
|
@@ -318,8 +316,8 @@ module DNN
|
|
318
316
|
private
|
319
317
|
|
320
318
|
def init_params
|
321
|
-
@params[:gamma] = SFloat.ones(*shape)
|
322
|
-
@params[:beta] = SFloat.zeros(*shape)
|
319
|
+
@params[:gamma] = Xumo::SFloat.ones(*shape)
|
320
|
+
@params[:beta] = Xumo::SFloat.zeros(*shape)
|
323
321
|
end
|
324
322
|
end
|
325
323
|
end
|
data/lib/dnn/core/model.rb
CHANGED
@@ -3,8 +3,6 @@ require "json"
|
|
3
3
|
module DNN
|
4
4
|
# This class deals with the model of the network.
|
5
5
|
class Model
|
6
|
-
include Xumo
|
7
|
-
|
8
6
|
attr_accessor :layers
|
9
7
|
attr_reader :optimizer
|
10
8
|
attr_reader :batch_size
|
@@ -36,7 +34,7 @@ module DNN
|
|
36
34
|
next unless layer.is_a?(HasParamLayer)
|
37
35
|
hash_params = has_param_layers_params[has_param_layers_index]
|
38
36
|
hash_params.each do |key, param|
|
39
|
-
layer.params[key] = SFloat.cast(param)
|
37
|
+
layer.params[key] = Xumo::SFloat.cast(param)
|
40
38
|
end
|
41
39
|
has_param_layers_index += 1
|
42
40
|
end
|
@@ -160,8 +158,8 @@ module DNN
|
|
160
158
|
end
|
161
159
|
correct = 0
|
162
160
|
(x.shape[0].to_f / @batch_size).ceil.times do |i|
|
163
|
-
x_batch = SFloat.zeros(@batch_size, *x.shape[1..-1])
|
164
|
-
y_batch = SFloat.zeros(@batch_size, *y.shape[1..-1])
|
161
|
+
x_batch = Xumo::SFloat.zeros(@batch_size, *x.shape[1..-1])
|
162
|
+
y_batch = Xumo::SFloat.zeros(@batch_size, *y.shape[1..-1])
|
165
163
|
@batch_size.times do |j|
|
166
164
|
k = i * @batch_size + j
|
167
165
|
break if k >= x.shape[0]
|
@@ -186,7 +184,7 @@ module DNN
|
|
186
184
|
end
|
187
185
|
|
188
186
|
def predict1(x)
|
189
|
-
predict(SFloat.cast([x]))[0, false]
|
187
|
+
predict(Xumo::SFloat.cast([x]))[0, false]
|
190
188
|
end
|
191
189
|
|
192
190
|
def forward(x, training)
|
data/lib/dnn/core/optimizers.rb
CHANGED
@@ -88,7 +88,7 @@ module DNN
|
|
88
88
|
layer.params.each_key do |key|
|
89
89
|
@g[layer][key] ||= 0
|
90
90
|
@g[layer][key] += layer.grads[key]**2
|
91
|
-
layer.params[key] -= (@learning_rate / NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
|
91
|
+
layer.params[key] -= (@learning_rate / Xumo::NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
|
92
92
|
end
|
93
93
|
end
|
94
94
|
end
|
@@ -112,7 +112,7 @@ module DNN
|
|
112
112
|
layer.params.each_key do |key|
|
113
113
|
@g[layer][key] ||= 0
|
114
114
|
@g[layer][key] = @muse * @g[layer][key] + (1 - @muse) * layer.grads[key]**2
|
115
|
-
layer.params[key] -= (@learning_rate / NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
|
115
|
+
layer.params[key] -= (@learning_rate / Xumo::NMath.sqrt(@g[layer][key] + 1e-7)) * layer.grads[key]
|
116
116
|
end
|
117
117
|
end
|
118
118
|
|
@@ -123,8 +123,6 @@ module DNN
|
|
123
123
|
|
124
124
|
|
125
125
|
class Adam < Optimizer
|
126
|
-
include Xumo
|
127
|
-
|
128
126
|
attr_accessor :beta1
|
129
127
|
attr_accessor :beta2
|
130
128
|
|
@@ -151,7 +149,7 @@ module DNN
|
|
151
149
|
@v[layer][key] ||= 0
|
152
150
|
@m[layer][key] += (1 - @beta1) * (layer.grads[key] - @m[layer][key])
|
153
151
|
@v[layer][key] += (1 - @beta2) * (layer.grads[key]**2 - @v[layer][key])
|
154
|
-
layer.params[key] -= lr * @m[layer][key] / NMath.sqrt(@v[layer][key] + 1e-7)
|
152
|
+
layer.params[key] -= lr * @m[layer][key] / Xumo::NMath.sqrt(@v[layer][key] + 1e-7)
|
155
153
|
end
|
156
154
|
end
|
157
155
|
|
data/lib/dnn/core/rnn_layers.rb
CHANGED
@@ -63,8 +63,8 @@ module DNN
|
|
63
63
|
|
64
64
|
def forward(xs)
|
65
65
|
@xs_shape = xs.shape
|
66
|
-
hs = SFloat.zeros(xs.shape[0], *shape)
|
67
|
-
h = (@stateful && @h) ? @h : SFloat.zeros(xs.shape[0], @num_nodes)
|
66
|
+
hs = Xumo::SFloat.zeros(xs.shape[0], *shape)
|
67
|
+
h = (@stateful && @h) ? @h : Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
|
68
68
|
xs.shape[1].times do |t|
|
69
69
|
x = xs[true, t, false]
|
70
70
|
h = @layers[t].forward(x, h)
|
@@ -75,10 +75,10 @@ module DNN
|
|
75
75
|
end
|
76
76
|
|
77
77
|
def backward(dh2s)
|
78
|
-
@grads[:weight] = SFloat.zeros(*@params[:weight].shape)
|
79
|
-
@grads[:weight2] = SFloat.zeros(*@params[:weight2].shape)
|
80
|
-
@grads[:bias] = SFloat.zeros(*@params[:bias].shape)
|
81
|
-
dxs = SFloat.zeros(@xs_shape)
|
78
|
+
@grads[:weight] = Xumo::SFloat.zeros(*@params[:weight].shape)
|
79
|
+
@grads[:weight2] = Xumo::SFloat.zeros(*@params[:weight2].shape)
|
80
|
+
@grads[:bias] = Xumo::SFloat.zeros(*@params[:bias].shape)
|
81
|
+
dxs = Xumo::SFloat.zeros(@xs_shape)
|
82
82
|
dh = 0
|
83
83
|
(0...dh2s.shape[1]).to_a.reverse.each do |t|
|
84
84
|
dh2 = dh2s[true, t, false]
|
@@ -114,9 +114,9 @@ module DNN
|
|
114
114
|
def init_params
|
115
115
|
@time_length = prev_layer.shape[0]
|
116
116
|
num_prev_nodes = prev_layer.shape[1]
|
117
|
-
@params[:weight] = SFloat.new(num_prev_nodes, @num_nodes)
|
118
|
-
@params[:weight2] = SFloat.new(@num_nodes, @num_nodes)
|
119
|
-
@params[:bias] = SFloat.new(@num_nodes)
|
117
|
+
@params[:weight] = Xumo::SFloat.new(num_prev_nodes, @num_nodes)
|
118
|
+
@params[:weight2] = Xumo::SFloat.new(@num_nodes, @num_nodes)
|
119
|
+
@params[:bias] = Xumo::SFloat.new(@num_nodes)
|
120
120
|
@weight_initializer.init_param(self, :weight)
|
121
121
|
@weight_initializer.init_param(self, :weight2)
|
122
122
|
@bias_initializer.init_param(self, :bias)
|
@@ -167,7 +167,7 @@ module DNN
|
|
167
167
|
dg = @g_tanh.backward(dcell2_tmp * @in)
|
168
168
|
dforget = @forget_sigmoid.backward(dcell2_tmp * @cell)
|
169
169
|
|
170
|
-
da = SFloat.hstack([dforget, dg, din, dout])
|
170
|
+
da = Xumo::SFloat.hstack([dforget, dg, din, dout])
|
171
171
|
|
172
172
|
@grads[:weight] += @x.transpose.dot(da)
|
173
173
|
@grads[:weight2] += @h.transpose.dot(da)
|
@@ -215,15 +215,15 @@ module DNN
|
|
215
215
|
|
216
216
|
def forward(xs)
|
217
217
|
@xs_shape = xs.shape
|
218
|
-
hs = SFloat.zeros(xs.shape[0], *shape)
|
218
|
+
hs = Xumo::SFloat.zeros(xs.shape[0], *shape)
|
219
219
|
h = nil
|
220
220
|
cell = nil
|
221
221
|
if @stateful
|
222
222
|
h = @h if @h
|
223
223
|
cell = @cell if @cell
|
224
224
|
end
|
225
|
-
h ||= SFloat.zeros(xs.shape[0], @num_nodes)
|
226
|
-
cell ||= SFloat.zeros(xs.shape[0], @num_nodes)
|
225
|
+
h ||= Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
|
226
|
+
cell ||= Xumo::SFloat.zeros(xs.shape[0], @num_nodes)
|
227
227
|
xs.shape[1].times do |t|
|
228
228
|
x = xs[true, t, false]
|
229
229
|
h, cell = @layers[t].forward(x, h, cell)
|
@@ -235,10 +235,10 @@ module DNN
|
|
235
235
|
end
|
236
236
|
|
237
237
|
def backward(dh2s)
|
238
|
-
@grads[:weight] = SFloat.zeros(*@params[:weight].shape)
|
239
|
-
@grads[:weight2] = SFloat.zeros(*@params[:weight2].shape)
|
240
|
-
@grads[:bias] = SFloat.zeros(*@params[:bias].shape)
|
241
|
-
dxs = SFloat.zeros(@xs_shape)
|
238
|
+
@grads[:weight] = Xumo::SFloat.zeros(*@params[:weight].shape)
|
239
|
+
@grads[:weight2] = Xumo::SFloat.zeros(*@params[:weight2].shape)
|
240
|
+
@grads[:bias] = Xumo::SFloat.zeros(*@params[:bias].shape)
|
241
|
+
dxs = Xumo::SFloat.zeros(@xs_shape)
|
242
242
|
dh = 0
|
243
243
|
dcell = 0
|
244
244
|
(0...dh2s.shape[1]).to_a.reverse.each do |t|
|
@@ -274,9 +274,9 @@ module DNN
|
|
274
274
|
def init_params
|
275
275
|
@time_length = prev_layer.shape[0]
|
276
276
|
num_prev_nodes = prev_layer.shape[1]
|
277
|
-
@params[:weight] = SFloat.new(num_prev_nodes, @num_nodes * 4)
|
278
|
-
@params[:weight2] = SFloat.new(@num_nodes, @num_nodes * 4)
|
279
|
-
@params[:bias] = SFloat.new(@num_nodes * 4)
|
277
|
+
@params[:weight] = Xumo::SFloat.new(num_prev_nodes, @num_nodes * 4)
|
278
|
+
@params[:weight2] = Xumo::SFloat.new(@num_nodes, @num_nodes * 4)
|
279
|
+
@params[:bias] = Xumo::SFloat.new(@num_nodes * 4)
|
280
280
|
@weight_initializer.init_param(self, :weight)
|
281
281
|
@weight_initializer.init_param(self, :weight2)
|
282
282
|
@bias_initializer.init_param(self, :bias)
|
data/lib/dnn/version.rb
CHANGED
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.5.
|
4
|
+
version: 0.5.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- unagiootoro
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-08-
|
11
|
+
date: 2018-08-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: numo-narray
|