class CooCoo::Trainer::Stochastic
Implements straight up stochastic gradient descent. No alterations get made to any hyperparameters while learning happens after every example.
Public Instance Methods
learn(network, input, expecting, rate, cost_function = CostFunctions::MeanSquare, hidden_state)
click to toggle source
# File lib/coo-coo/trainer/stochastic.rb, line 36 def learn(network, input, expecting, rate, cost_function = CostFunctions::MeanSquare, hidden_state) output, hidden_state = network.forward(input, hidden_state) target = network.prep_output_target(expecting) final_output = network.final_output(output) errors = cost_function.derivative(target, final_output) deltas, hidden_state = network.backprop(input, output, errors, hidden_state) network.update_weights!(input, output, deltas * rate) return cost_function.call(target, final_output), hidden_state end
train(options, &block)
click to toggle source
# File lib/coo-coo/trainer/stochastic.rb, line 12 def train(options, &block) options = options.to_h network = options.fetch(:network) training_data = options.fetch(:data) learning_rate = options.fetch(:learning_rate, 0.3) batch_size = options.fetch(:batch_size, 1024) cost_function = options.fetch(:cost_function, CostFunctions::MeanSquare) t = Time.now training_data.each_slice(batch_size).with_index do |batch, i| total_errs = batch.inject(nil) do |acc, (expecting, input)| errs, hidden_state = learn(network, input, expecting, learning_rate, cost_function, Hash.new) errs + (acc || 0) end if block block.call(BatchStats.new(self, i, batch.size, Time.now - t, total_errs)) end t = Time.now end end