lib/dnn/core/model.rb in ruby-dnn-0.5.12 vs lib/dnn/core/model.rb in ruby-dnn-0.6.0
- old
+ new
@@ -20,11 +20,10 @@
end
def initialize
@layers = []
@optimizer = nil
- @batch_size = nil
@training = false
@compiled = false
end
def load_json_params(json_str)
@@ -101,17 +100,16 @@
batch_proc: nil,
&epoch_proc)
unless compiled?
raise DNN_Error.new("The model is not compiled.")
end
- @batch_size = batch_size
num_train_data = x.shape[0]
(1..epochs).each do |epoch|
puts "【 epoch #{epoch}/#{epochs} 】" if verbose
- (num_train_data.to_f / @batch_size).ceil.times do |index|
- x_batch, y_batch = Util.get_minibatch(x, y, @batch_size)
- loss = train_on_batch(x_batch, y_batch, @batch_size, &batch_proc)
+ (num_train_data.to_f / batch_size).ceil.times do |index|
+ x_batch, y_batch = Util.get_minibatch(x, y, 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_data = (index + 1) * batch_size
@@ -128,48 +126,41 @@
end
log << " #{num_trained_data}/#{num_train_data} loss: #{sprintf('%.8f', loss)}"
print log if verbose
end
if verbose && test
- acc = accurate(test[0], test[1], batch_size,&batch_proc)
+ 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
- def train_on_batch(x, y, batch_size, &batch_proc)
- @batch_size = batch_size
+ def train_on_batch(x, y, &batch_proc)
x, y = batch_proc.call(x, y) if batch_proc
forward(x, true)
loss = @layers[-1].loss(y)
backward(y)
@layers.each { |layer| layer.update if layer.respond_to?(:update) }
loss
end
- def accurate(x, y, batch_size = nil, &batch_proc)
- unless batch_size
- if @batch_size
- batch_size = @batch_size >= x.shape[0] ? @batch_size : x.shape[0]
- else
- batch_size = 1
- end
- end
+ def accurate(x, y, batch_size = 1, &batch_proc)
+ batch_size = batch_size >= x.shape[0] ? batch_size : x.shape[0]
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
+ (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|
+ batch_size.times do |j|
if @layers[-1].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