Class: TensorStream::Variable

Inherits:
Tensor
  • Object
show all
Defined in:
lib/tensor_stream/variable.rb

Overview

Class that defines a TensorStream variable

Instance Attribute Summary collapse

Attributes inherited from Tensor

#data_type, #given_name, #graph, #internal, #is_const, #name, #native_buffer, #outputs, #rank, #shape, #source, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Tensor

#auto_math, #breakpoint!, cast_dtype, #collect, #consumers, detect_type, #device, #dtype, #eval, #first, #internal?, #print!, reset_counters, #to_a, #to_f, #to_h, #to_i, #to_s

Methods included from OpHelper

#_op, #cons, #format_source, #fp_type?, #i_cons, #i_op, #i_var, #int_type?, #reduced_shape, #shape_eval, #shape_full_specified, #shapes_fully_specified_and_equal

Methods included from TensorMixins

#!=, #%, #*, #**, #+, #-, #-@, #/, #<, #<=, #==, #>, #>=, #[], #and, #cast, #ceil, #dot, #floor, #log, #matmul, #reduce, #reshape, #round, #var, #zero?

Constructor Details

#initialize(data_type) ⇒ Variable

Returns a new instance of Variable.



7
8
9
10
11
# File 'lib/tensor_stream/variable.rb', line 7

def initialize(data_type)
  @data_type = data_type
  @options = {}
  @is_const = false
end

Instance Attribute Details

#bufferObject

Returns the value of attribute buffer.



4
5
6
# File 'lib/tensor_stream/variable.rb', line 4

def buffer
  @buffer
end

#opObject

Returns the value of attribute op.



4
5
6
# File 'lib/tensor_stream/variable.rb', line 4

def op
  @op
end

#optionsObject

Returns the value of attribute options.



4
5
6
# File 'lib/tensor_stream/variable.rb', line 4

def options
  @options
end

#trainableObject

Returns the value of attribute trainable.



4
5
6
# File 'lib/tensor_stream/variable.rb', line 4

def trainable
  @trainable
end

#value=(value) ⇒ Object (writeonly)

Sets the attribute value

Parameters:

  • value

    the value to set the attribute value to.



5
6
7
# File 'lib/tensor_stream/variable.rb', line 5

def value=(value)
  @value = value
end

Class Method Details

.global_variables_initializerObject



78
79
80
# File 'lib/tensor_stream/variable.rb', line 78

def self.global_variables_initializer
  variables_initializer(TensorStream::GraphKeys::GLOBAL_VARIABLES)
end

.variables_initializer(collection) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/tensor_stream/variable.rb', line 70

def self.variables_initializer(collection)
  global_variables_ops = TensorStream.get_default_graph.get_collection(collection).map do |variable|
    _op(:assign, variable.initializer, var_name: variable.name)
  end

  TensorStream.group(global_variables_ops)
end

Instance Method Details

#assign(value, name: nil, use_locking: false) ⇒ Object



47
48
49
50
# File 'lib/tensor_stream/variable.rb', line 47

def assign(value, name: nil, use_locking: false)
  TensorStream.check_data_types(self, value)
  _op(:assign, value, name: name, var_name: @name)
end

#assign_add(value, name: nil) ⇒ Object



56
57
58
59
# File 'lib/tensor_stream/variable.rb', line 56

def assign_add(value, name: nil)
  TensorStream.check_data_types(self, value)
  _op(:assign_add, value, data_type: data_type, name: name, var_name: @name)
end

#assign_sub(value) ⇒ Object



65
66
67
68
# File 'lib/tensor_stream/variable.rb', line 65

def assign_sub(value)
  TensorStream.check_data_types(self, value)
  _op(:assign_sub, value, data_type: data_type, name: name, var_name: @name)
end

#initialized_valueObject



40
41
42
43
44
45
# File 'lib/tensor_stream/variable.rb', line 40

def initialized_value
  init_op = @initalizer_tensor.op
  init_op.shape = @shape || init_op.shape
  init_op.data_type = @data_type || init_op.data_type
  init_op
end

#initializerObject



33
34
35
36
37
38
# File 'lib/tensor_stream/variable.rb', line 33

def initializer
  init_op = @initalizer_tensor.op
  init_op.shape = @shape || init_op.shape
  init_op.data_type = @data_type || init_op.data_type
  assign(init_op)
end

#inspectObject



82
83
84
# File 'lib/tensor_stream/variable.rb', line 82

def inspect
  "Variable(#{@name} shape: #{@shape || "?"} data_type: #{@data_type})"
end

#prepare(rank, shape, variable_scope, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tensor_stream/variable.rb', line 13

def prepare(rank, shape, variable_scope, options = {})
  setup_initial_state(options)

  @rank = rank
  @value = nil

  scope_name = variable_scope ? variable_scope.name : nil
  variable_scope_initializer = variable_scope ? variable_scope.initializer : nil
  @name = [scope_name, options[:name] || build_name].compact.reject(&:empty?).join("/")
  @initalizer_tensor = options[:initializer] || variable_scope_initializer || TensorStream.glorot_uniform_initializer
  shape = @initalizer_tensor.shape.shape if shape.nil? && @initalizer_tensor && @initalizer_tensor.shape

  @shape = TensorShape.new(shape, rank)
  @trainable = options.fetch(:trainable, true)
end

#read_valueObject



52
53
54
# File 'lib/tensor_stream/variable.rb', line 52

def read_value
  Evaluator.read_variable(@graph, @name)
end

#to_math(_tensor, _name_only = false, _max_depth = 99, _unused = 0) ⇒ Object



61
62
63
# File 'lib/tensor_stream/variable.rb', line 61

def to_math(_tensor, _name_only = false, _max_depth = 99, _unused = 0)
  @name
end

#trainable?Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/tensor_stream/variable.rb', line 29

def trainable?
  @trainable
end