lib/dnn/core/models.rb in ruby-dnn-1.1.6 vs lib/dnn/core/models.rb in ruby-dnn-1.2.0
- old
+ new
@@ -228,10 +228,11 @@
@last_log[:epoch] = epoch
call_callbacks(:before_epoch)
puts "【 epoch #{epoch}/#{epochs} 】" if verbose
train_iterator.foreach(batch_size) do |x_batch, y_batch, index|
+ @last_log[:step] = index
train_step_met = train_step(x_batch, y_batch)
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|
@@ -303,17 +304,17 @@
output_tensors.each.with_index do |out, i|
loss_opt = {}
loss_opt[:layers] = layers if i == 0
loss_opt[:loss_weight] = @loss_weights[i] if @loss_weights
loss = @loss_func[i].loss(out, Tensor.convert(y[i]), **loss_opt)
- loss_data << loss.data.to_f
+ loss_data << Utils.to_f(loss.data)
loss.link.backward(Xumo::SFloat.ones(y[i][0...1, false].shape[0], 1))
end
else
out = output_tensors
loss = @loss_func.loss(out, Tensor.convert(y), layers: layers)
- loss_data = loss.data.to_f
+ loss_data = Utils.to_f(loss.data)
loss.link.backward(Xumo::SFloat.ones(y[0...1, false].shape[0], 1))
end
@optimizer.update(get_all_trainable_params)
@last_log[:train_loss] = loss_data
call_callbacks(:after_train_on_batch)
@@ -390,17 +391,17 @@
correct = [] if accuracy
loss_data = []
output_tensors.each.with_index do |out, i|
correct << accuracy(out.data, y[i]) if accuracy
loss = @loss_func[i].(out, Tensor.convert(y[i]))
- loss_data << loss.data.to_f
+ loss_data << Utils.to_f(loss.data)
end
else
out = output_tensors
correct = accuracy(out.data, y) if accuracy
loss = @loss_func.(out, Tensor.convert(y))
- loss_data = loss.data.to_f
+ loss_data = Utils.to_f(loss.data)
end
call_callbacks(:after_test_on_batch)
[correct, loss_data]
end
@@ -565,10 +566,60 @@
layer.get_params[key].data = data
end
end
end
+ # Convert the parameters of model and optimizer for cpu.
+ # @return [DNN::Models::Model] Return self.
+ def to_cpu
+ params_data = get_all_params_data
+ clean_layers
+ set_all_params_data(params_data)
+ trainable_layers.each do |layer|
+ layer.get_params.each do |key, param|
+ data = param.data
+ if DNN.use_cumo? && data.is_a?(Cumo::NArray)
+ param.data = Utils.cumo2numo(data)
+ end
+ end
+ end
+ @optimizer.status.each do |key, state|
+ next unless state
+ state.each do |param, data|
+ if DNN.use_cumo? && data.is_a?(Cumo::NArray)
+ state[param] = Utils.cumo2numo(data)
+ end
+ end
+ end
+ self
+ end
+
+ # Convert the parameters of model and optimizer for gpu.
+ # @return [DNN::Models::Model] Return self.
+ def to_gpu
+ params_data = get_all_params_data
+ clean_layers
+ set_all_params_data(params_data)
+ trainable_layers.each do |layer|
+ layer.get_params.each do |(key, param)|
+ data = param.data
+ if DNN.use_cumo? && data.is_a?(Numo::NArray)
+ param.data = Utils.numo2cumo(data)
+ end
+ end
+ end
+ @optimizer.status.each do |(key, state)|
+ next unless state
+ state.each do |(param, data)|
+ if DNN.use_cumo? && data.is_a?(Numo::NArray)
+ state[param] = Utils.numo2cumo(data)
+ end
+ end
+ end
+ self
+ end
+
private
def get_all_trainable_params
layers.select { |layer| layer.is_a?(Layers::TrainableLayer) && layer.trainable }
.map { |layer| layer.get_params.values }.flatten.compact
@@ -582,14 +633,14 @@
end
def metrics_to_str(mertics)
mertics.map { |key, values|
str_values = if values.is_a?(Array)
- values_fmt = values.map { |v| sprintf('%.4f', v) }
+ values_fmt = values.map { |v| sprintf('%.4f', Utils.to_f(v)) }
"[#{values_fmt.join(", ")}]"
else
- sprintf('%.4f', values)
+ sprintf('%.4f', Utils.to_f(values))
end
"#{key}: #{str_values}"
}.join(", ")
end
@@ -654,9 +705,10 @@
end
unless layer.is_a?(Layers::Layer) || layer.is_a?(Chain)
raise TypeError, "layer: #{layer.class.name} is not an instance of the DNN::Layers::Layer class or DNN::Models::Chain class."
end
@stack.insert(index, layer)
+ self
end
# Remove layer to the model.
# @param [DNN::Layers::Layer | DNN::Models::Chain] layer Layer to remove to the model.
# @return [Boolean] Return true if success for remove layer.