Class: CooCoo::Trainer::Stochastic
- Defined in:
- lib/coo-coo/trainer/stochastic.rb
Overview
Implements straight up stochastic gradient descent. No alterations get made to any hyperparameters while learning happens after every example.
Constant Summary
Constants inherited from Base
Instance Method Summary collapse
- #learn(network, input, expecting, rate, cost_function = CostFunctions::MeanSquare, hidden_state) ⇒ Object
- #train(options, &block) ⇒ Object
Methods inherited from Base
Instance Method Details
#learn(network, input, expecting, rate, cost_function = CostFunctions::MeanSquare, hidden_state) ⇒ Object
36 37 38 39 40 41 42 43 44 |
# 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) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/coo-coo/trainer/stochastic.rb', line 12 def train(, &block) = .to_h network = .fetch(:network) training_data = .fetch(:data) learning_rate = .fetch(:learning_rate, 0.3) batch_size = .fetch(:batch_size, 1024) cost_function = .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 |