Class: TensorStream::Train::Optimizer

Inherits:
Object
  • Object
show all
Includes:
SlotCreator
Defined in:
lib/tensor_stream/train/optimizer.rb

Overview

Base class for an optimizer This is a straight up port from the python version

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from SlotCreator

#create_slot, #create_slot_var, #create_slot_with_initializer, #create_zeros_slot

Methods included from Utils

#__v_scope_name, #apply_data_type_coercion, #assign, #check_allowed_types, #check_data_types, #check_if_dense, #colocate_with, #constant, #control_dependencies, #convert_to_tensor, #device, #disable_eager_execution, #dynamic_stitch, #enable_eager_execution, #executing_eagerly?, #float32, #get_collection, #get_default_graph, #get_variable, #get_variable_scope, #global_variables_initializer, #graph, #group, #image, #layers, #list_local_devices, #math, #name_scope, #placeholder, #program, #reset_default_graph, #session, #set_random_seed, #train, #trainable_variables, #variable, #variable_scope

Constructor Details

#initialize(name:, use_locking:) ⇒ Optimizer

Returns a new instance of Optimizer.



10
11
12
13
14
15
16
17
# File 'lib/tensor_stream/train/optimizer.rb', line 10

def initialize(name:, use_locking:)
  @name = name
  @use_locking = use_locking
  raise TensorStream::ValueError, "Must specify the optimizer name" unless @name

  @slots = {}
  @non_slots = {}
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'lib/tensor_stream/train/optimizer.rb', line 8

def name
  @name
end

Instance Method Details

#apply_gradients(grads_and_vars, global_step: nil, name: nil) ⇒ Object

Apply gradients to variables. This is the second part of minimize(). It returns an Operation that applies gradients.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tensor_stream/train/optimizer.rb', line 27

def apply_gradients(grads_and_vars, global_step: nil, name: nil)
  varlist = grads_and_vars.map { |_grad, var| var }
  create_slots(varlist)
  TensorStream.name_scope(name, default: @name) do
    prepare
    apply_ops = grads_and_vars.map { |grad, var|
      TensorStream.name_scope("update_" + var.op.name) do
        apply_dense(grad, var)
      end
    }

    if global_step.nil?
      finish(apply_ops, name)
    else
      TensorStream.control_dependencies([finish(apply_ops, "update")]) do
        global_step.assign_add(1)
      end
    end
  end
end

#compute_gradients(loss, var_list: nil, grad_loss: nil) ⇒ Object

Compute gradients of loss for the variables in var_list.

This is the first part of minimize(). It returns a list of (gradient, variable) pairs where “gradient” is the gradient for “variable”.



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/tensor_stream/train/optimizer.rb', line 52

def compute_gradients(loss, var_list: nil, grad_loss: nil)
  trainable_vars = if var_list
    raise "var_list must be an array" unless var_list.is_a?(Array)

    var_list.each_with_index { |var, index| raise "var #{index} not a Variable" unless var.is_a?(Variable) }

    var_list
  else
    loss.graph.get_collection(TensorStream::GraphKeys::TRAINABLE_VARIABLES)
  end
  all_grads = grad_loss || TensorStream.gradients(loss, trainable_vars)
  trainable_vars.each_with_index.collect do |var, index|
    [all_grads[index], var]
  end
end

#get_slot(var, name) ⇒ Object



68
69
70
71
72
73
# File 'lib/tensor_stream/train/optimizer.rb', line 68

def get_slot(var, name)
  named_slots = @slots.fetch(name, nil)
  return nil if named_slots.nil?

  named_slots.fetch(var_key(var), nil)
end

#get_slot_namesObject



75
76
77
# File 'lib/tensor_stream/train/optimizer.rb', line 75

def get_slot_names
  @slots.keys.sort
end

#minimize(loss, var_list: nil, grad_loss: nil, global_step: nil, name: nil) ⇒ Object



19
20
21
22
# File 'lib/tensor_stream/train/optimizer.rb', line 19

def minimize(loss, var_list: nil, grad_loss: nil, global_step: nil, name: nil)
  grads_and_vars = compute_gradients(loss, var_list: var_list, grad_loss: grad_loss)
  apply_gradients(grads_and_vars, global_step: global_step, name: name)
end