Class: DNN::Model
- Inherits:
-
Object
- Object
- DNN::Model
- Defined in:
- lib/dnn/core/model.rb
Overview
This class deals with the model of the network.
Instance Attribute Summary collapse
-
#layers ⇒ Object
All layers possessed by the model.
-
#trainable ⇒ Object
Setting false prevents learning of parameters.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(layer) ⇒ Object
- #accurate(x, y, batch_size = 100, &batch_proc) ⇒ Object
- #backward(dout, learning_phase) ⇒ Object
- #build(super_model = nil) ⇒ Object
- #compile(optimizer, loss) ⇒ Object
- #compiled? ⇒ Boolean
- #copy ⇒ Object
- #forward(x, learning_phase) ⇒ Object
- #get_all_layers ⇒ Object
- #get_layer(*args) ⇒ Object
- #get_prev_layer(layer) ⇒ Object
-
#initialize ⇒ Model
constructor
A new instance of Model.
- #input_shape ⇒ Object
- #load_json_params(json_str) ⇒ Object
- #loss ⇒ Object
- #optimizer ⇒ Object
- #output_shape ⇒ Object
- #params_to_json ⇒ Object
- #predict(x) ⇒ Object
- #predict1(x) ⇒ Object
- #save(file_name) ⇒ Object
- #to_hash ⇒ Object
- #to_json ⇒ Object
- #train(x, y, epochs, batch_size: 1, test: nil, verbose: true, batch_proc: nil, &epoch_proc) ⇒ Object
- #train_on_batch(x, y, &batch_proc) ⇒ Object
- #update ⇒ Object
Constructor Details
#initialize ⇒ Model
Returns a new instance of Model.
29 30 31 32 33 34 |
# File 'lib/dnn/core/model.rb', line 29 def initialize @layers = [] @trainable = true @optimizer = nil @compiled = false end |
Instance Attribute Details
#layers ⇒ Object
All layers possessed by the model
9 10 11 |
# File 'lib/dnn/core/model.rb', line 9 def layers @layers end |
#trainable ⇒ Object
Setting false prevents learning of parameters.
10 11 12 |
# File 'lib/dnn/core/model.rb', line 10 def trainable @trainable end |
Class Method Details
.load(file_name) ⇒ Object
12 13 14 |
# File 'lib/dnn/core/model.rb', line 12 def self.load(file_name) Marshal.load(Zlib::Inflate.inflate(File.binread(file_name))) end |
Instance Method Details
#<<(layer) ⇒ Object
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/dnn/core/model.rb', line 85 def <<(layer) # Due to a bug in saving nested models, temporarily prohibit model nesting. # if !layer.is_a?(Layers::Layer) && !layer.is_a?(Model) # raise TypeError.new("layer is not an instance of the DNN::Layers::Layer class or DNN::Model class.") # end unless layer.is_a?(Layers::Layer) raise TypeError.new("layer:#{layer.class.name} is not an instance of the DNN::Layers::Layer class.") end @layers << layer self end |
#accurate(x, y, batch_size = 100, &batch_proc) ⇒ Object
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/dnn/core/model.rb', line 209 def accurate(x, y, batch_size = 100, &batch_proc) check_xy_type(x, y) input_data_shape_check(x, y) batch_size = batch_size >= x.shape[0] ? x.shape[0] : batch_size correct = 0 (x.shape[0].to_f / batch_size).ceil.times do |i| x_batch = Xumo::SFloat.zeros(batch_size, *x.shape[1..-1]) y_batch = Xumo::SFloat.zeros(batch_size, *y.shape[1..-1]) batch_size.times do |j| k = i * batch_size + j break if k >= x.shape[0] x_batch[j, false] = x[k, false] y_batch[j, false] = y[k, false] end x_batch, y_batch = batch_proc.call(x_batch, y_batch) if batch_proc out = forward(x_batch, false) batch_size.times do |j| if @layers.last.output_shape == [1] correct += 1 if out[j, 0].round == y_batch[j, 0].round else correct += 1 if out[j, true].max_index == y_batch[j, true].max_index end end end correct.to_f / x.shape[0] end |
#backward(dout, learning_phase) ⇒ Object
278 279 280 281 282 283 284 285 286 287 |
# File 'lib/dnn/core/model.rb', line 278 def backward(dout, learning_phase) @layers.reverse.each do |layer| if layer.is_a?(Layers::Dropout) || layer.is_a?(Layers::BatchNormalization) || layer.is_a?(Model) dout = layer.backward(dout, learning_phase) else dout = layer.backward(dout) end end dout end |
#build(super_model = nil) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/dnn/core/model.rb', line 112 def build(super_model = nil) @super_model = super_model shape = if super_model super_model.output_shape else @layers.first.build end @layers[1..-1].each do |layer| if layer.is_a?(Model) layer.build(self) else layer.build(shape) end shape = layer.output_shape end end |
#compile(optimizer, loss) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/dnn/core/model.rb', line 97 def compile(optimizer, loss) unless optimizer.is_a?(Optimizers::Optimizer) raise TypeError.new("optimizer:#{optimizer.class} is not an instance of DNN::Optimizers::Optimizer class.") end unless loss.is_a?(Losses::Loss) raise TypeError.new("loss:#{loss.class} is not an instance of DNN::Losses::Loss class.") end @compiled = true layers_check @optimizer = optimizer @loss = loss build layers_shape_check end |
#compiled? ⇒ Boolean
147 148 149 |
# File 'lib/dnn/core/model.rb', line 147 def compiled? @compiled end |
#copy ⇒ Object
247 248 249 |
# File 'lib/dnn/core/model.rb', line 247 def copy Marshal.load(Marshal.dump(self)) end |
#forward(x, learning_phase) ⇒ Object
267 268 269 270 271 272 273 274 275 276 |
# File 'lib/dnn/core/model.rb', line 267 def forward(x, learning_phase) @layers.each do |layer| x = if layer.is_a?(Layers::Dropout) || layer.is_a?(Layers::BatchNormalization) || layer.is_a?(Model) layer.forward(x, learning_phase) else layer.forward(x) end end x end |
#get_all_layers ⇒ Object
261 262 263 264 265 |
# File 'lib/dnn/core/model.rb', line 261 def get_all_layers @layers.map { |layer| layer.is_a?(Model) ? layer.get_all_layers : layer }.flatten end |
#get_layer(*args) ⇒ Object
251 252 253 254 255 256 257 258 259 |
# File 'lib/dnn/core/model.rb', line 251 def get_layer(*args) if args.length == 1 index = args[0] @layers[index] else layer_class, index = args @layers.select { |layer| layer.is_a?(layer_class) }[index] end end |
#get_prev_layer(layer) ⇒ Object
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/dnn/core/model.rb', line 300 def get_prev_layer(layer) layer_index = @layers.index(layer) prev_layer = if layer_index == 0 if @super_model @super_model.layers[@super_model.layers.index(self) - 1] else self end else @layers[layer_index - 1] end if prev_layer.is_a?(Layers::Layer) prev_layer elsif prev_layer.is_a?(Model) prev_layer.layers.last end end |
#input_shape ⇒ Object
129 130 131 |
# File 'lib/dnn/core/model.rb', line 129 def input_shape @layers.first.input_shape end |
#load_json_params(json_str) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dnn/core/model.rb', line 36 def load_json_params(json_str) hash = JSON.parse(json_str, symbolize_names: true) has_param_layers_params = hash[:params] has_param_layers_index = 0 has_param_layers = get_all_layers.select { |layer| layer.is_a?(Layers::HasParamLayer) } has_param_layers.each do |layer| hash_params = has_param_layers_params[has_param_layers_index] hash_params.each do |key, (shape, base64_param)| bin = Base64.decode64(base64_param) data = Xumo::SFloat.from_binary(bin).reshape(*shape) if layer.params[key].is_a?(Param) layer.params[key].data = data else layer.params[key] = data end end has_param_layers_index += 1 end end |
#loss ⇒ Object
142 143 144 145 |
# File 'lib/dnn/core/model.rb', line 142 def loss raise DNN_Error.new("The model is not compiled.") unless compiled? @loss ? @loss : @super_model.loss end |
#optimizer ⇒ Object
137 138 139 140 |
# File 'lib/dnn/core/model.rb', line 137 def optimizer raise DNN_Error.new("The model is not compiled.") unless compiled? @optimizer ? @optimizer : @super_model.optimizer end |
#output_shape ⇒ Object
133 134 135 |
# File 'lib/dnn/core/model.rb', line 133 def output_shape @layers.last.output_shape end |
#params_to_json ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/dnn/core/model.rb', line 73 def params_to_json has_param_layers = get_all_layers.select { |layer| layer.is_a?(Layers::HasParamLayer) } has_param_layers_params = has_param_layers.map do |layer| layer.params.map { |key, param| base64_data = Base64.encode64(param.data.to_binary) [key, [param.data.shape, base64_data]] }.to_h end hash = {version: VERSION, params: has_param_layers_params} JSON.dump(hash) end |
#predict(x) ⇒ Object
236 237 238 239 240 |
# File 'lib/dnn/core/model.rb', line 236 def predict(x) check_xy_type(x) input_data_shape_check(x) forward(x, false) end |
#predict1(x) ⇒ Object
242 243 244 245 |
# File 'lib/dnn/core/model.rb', line 242 def predict1(x) check_xy_type(x) predict(Xumo::SFloat.cast([x]))[0, false] end |
#save(file_name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 |
# File 'lib/dnn/core/model.rb', line 56 def save(file_name) bin = Zlib::Deflate.deflate(Marshal.dump(self)) begin File.binwrite(file_name, bin) rescue Errno::ENOENT => ex dir_name = file_name.match(%r`(.*)/.+$`)[1] Dir.mkdir(dir_name) File.binwrite(file_name, bin) end end |
#to_hash ⇒ Object
318 319 320 321 |
# File 'lib/dnn/core/model.rb', line 318 def to_hash hash_layers = @layers.map { |layer| layer.to_hash } {class: Model.name, layers: hash_layers, optimizer: @optimizer.to_hash, loss: @loss.to_hash} end |
#to_json ⇒ Object
67 68 69 70 71 |
# File 'lib/dnn/core/model.rb', line 67 def to_json hash = self.to_hash hash[:version] = VERSION JSON.pretty_generate(hash) end |
#train(x, y, epochs, batch_size: 1, test: nil, verbose: true, batch_proc: nil, &epoch_proc) ⇒ Object
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/dnn/core/model.rb', line 151 def train(x, y, epochs, batch_size: 1, test: nil, verbose: true, batch_proc: nil, &epoch_proc) unless compiled? raise DNN_Error.new("The model is not compiled.") end check_xy_type(x, y) dataset = Dataset.new(x, y) num_train_datas = x.shape[0] (1..epochs).each do |epoch| puts "【 epoch #{epoch}/#{epochs} 】" if verbose (num_train_datas.to_f / batch_size).ceil.times do |index| x_batch, y_batch = dataset.get_batch(batch_size) loss = train_on_batch(x_batch, y_batch, &batch_proc) if loss.nan? puts "\nloss is nan" if verbose return end num_trained_datas = (index + 1) * batch_size num_trained_datas = num_trained_datas > num_train_datas ? num_train_datas : num_trained_datas log = "\r" 40.times do |i| if i < num_trained_datas * 40 / num_train_datas log << "=" elsif i == num_trained_datas * 40 / num_train_datas log << ">" else log << "_" end end log << " #{num_trained_datas}/#{num_train_datas} loss: #{sprintf('%.8f', loss)}" print log if verbose end if verbose && test acc = accurate(test[0], test[1], batch_size, &batch_proc) print " accurate: #{acc}" end puts "" if verbose epoch_proc.call(epoch) if epoch_proc end end |
#train_on_batch(x, y, &batch_proc) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/dnn/core/model.rb', line 196 def train_on_batch(x, y, &batch_proc) check_xy_type(x, y) input_data_shape_check(x, y) x, y = batch_proc.call(x, y) if batch_proc out = forward(x, true) loss_value = @loss.forward(out, y) + @loss.regularize(get_all_layers) dout = @loss.backward(y) backward(dout, true) @loss.d_regularize(get_all_layers) update loss_value end |
#update ⇒ Object
289 290 291 292 293 294 295 296 297 298 |
# File 'lib/dnn/core/model.rb', line 289 def update return unless @trainable @layers.each do |layer| if layer.is_a?(Layers::HasParamLayer) layer.update(@optimizer) elsif layer.is_a?(Model) layer.update end end end |