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

#breakpoint, #consumers, #data_type, #device, #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

#!=, #*, #**, #+, #-, #-@, #/, #<, #<=, #==, #>, #>=, #[], #and, #auto_math, #breakpoint!, cast_dtype, #collect, detect_type, #dot, #dtype, #eval, #first, #internal?, #matmul, #op, #print!, reset_counters, #to_a, #to_f, #to_h, #to_i, #to_s

Methods included from OpHelper

#_op, #cons, #dtype_eval, #format_source, #fp_type?, #i_cons, #i_op, #shape_eval, #val_to_dtype

Constructor Details

#initialize(data_type, rank, shape, options = {}) ⇒ Variable

Returns a new instance of Variable.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/tensor_stream/variable.rb', line 5

def initialize(data_type, rank, shape, options = {})
  setup_initial_state(options)

  @options = {
  }
  @data_type = data_type
  @rank = rank
  @value = nil
  @is_const = false
  @name = [TensorStream.get_variable_scope, options[:name] || build_name].compact.reject(&:empty?).join('/')
  @initalizer_tensor = options[:initializer] ? options[:initializer] : _variable_scope.initializer || TensorStream.glorot_uniform_initializer
  if shape.nil? && @initalizer_tensor && @initalizer_tensor.shape
    shape = @initalizer_tensor.shape.shape
  end
  @shape = TensorShape.new(shape, rank)
  @trainable = options.fetch(:trainable, true)
  @graph.add_variable(self, options)
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

#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

Class Method Details

.global_variables_initializerObject



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

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

.variables_initializer(collection) ⇒ Object



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

def self.variables_initializer(collection)
  TensorStream.group(TensorStream.get_default_graph.get_collection(collection).map(&:initializer))
end

Instance Method Details

#assign(value, name: nil) ⇒ Object



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

def assign(value, name: nil)
  _a, value = TensorStream.check_data_types(self, value)
  Operation.new(:assign, self, value, name: name)
end

#assign_add(value) ⇒ Object



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

def assign_add(value)
  _a, value = TensorStream.check_data_types(self, value)
  Operation.new(:assign_add, self, value, data_type: data_type)
end

#assign_sub(value) ⇒ Object



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

def assign_sub(value)
  _a, value = TensorStream.check_data_types(self, value)
  Operation.new(:assign_sub, self, value)
end

#initializerObject



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

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

#read_valueObject



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

def read_value
  if buffer
    @value = buffer.to_ruby
  end

  @value
end

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



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

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

#trainable?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/tensor_stream/variable.rb', line 24

def trainable?
  @trainable
end