Module: TensorStream::Train::SlotCreator

Includes:
Utils
Included in:
Optimizer
Defined in:
lib/tensor_stream/train/slot_creator.rb

Instance Method Summary collapse

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

Instance Method Details

#create_slot(primary, val, name, colocate_with_primary: true) ⇒ Object

Create a slot initialized to the given value

Args:

primary: Variable - The primary 'Variable' or 'Tensor'
val: Tensor - A `Tensor` specifying the initial value of the slot
name: String - Name to use for the slot variable
colocate_with_primary: Boolean - If true the slot is located
                                 on the same device as `primary`

Returns: A ‘Variable` object



25
26
27
28
29
30
31
32
33
# File 'lib/tensor_stream/train/slot_creator.rb', line 25

def create_slot(primary, val, name, colocate_with_primary: true)
  TensorStream.variable_scope(nil, primary.op.name + "/" + name) do
    return create_slot_var(primary, val, "", nil) if colocate_with_primary

    TensorStream.colocate_with(primary) do
      return create_slot_var(primary, val, "", nil)
    end
  end
end

#create_slot_var(_primary, val, scope, shape) ⇒ Object

Helper function for creating a slot variable.



8
9
10
11
12
# File 'lib/tensor_stream/train/slot_creator.rb', line 8

def create_slot_var(_primary, val, scope, shape)
  slot = get_variable(scope, initializer: val, trainable: false, shape: shape,
                                            validate_shape: val.shape && val.shape.known?)
  slot
end

#create_slot_with_initializer(primary, initializer, shape, dtype, name, colocate_with_primary: true) ⇒ Object



35
36
37
38
39
40
# File 'lib/tensor_stream/train/slot_creator.rb', line 35

def create_slot_with_initializer(primary, initializer, shape, dtype, name, colocate_with_primary: true)
  prefix = primary.op.name
  TensorStream.variable_scope(nil, prefix + "/" + name) do
    create_slot_var(primary, initializer, "", shape.shape)
  end
end

#create_zeros_slot(primary, name, dtype: nil, colocate_with_primary: true) ⇒ Object

Create a slot initialized to 0 with same shape as the primary object.

Args:

primary: The pirmary variable or Tensor
name: String - Name to use for the slot variable
dtype: Symbol - Type of the slot variable
colocate_with_primary: boolean - If true the slot is located on the same device as primary

Returns:

A `Variable` object


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

def create_zeros_slot(primary, name, dtype: nil, colocate_with_primary: true)
  dtype = primary.data_type if dtype.nil?
  slot_shape = primary.shape
  slot_shape = if slot_shape.fully_defined?
    slot_shape.shape
  else
    TensorStream.shape(primary.initialized_value)
  end
  val = TensorStream.zeros(slot_shape, dtype: dtype)
  create_slot(primary, val, name,
    colocate_with_primary: colocate_with_primary)
end