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, #given_name, #graph, #internal, #is_const, #name, #native_buffer, #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
# File 'lib/tensor_stream/variable.rb', line 5

def initialize(data_type, rank, shape, options = {})
  @graph = options[:graph] || TensorStream.get_default_graph

  @data_type = data_type
  @rank = rank
  @value = nil
  @source = format_source(caller_locations)
  @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

#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



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

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

.variables_initializer(collection) ⇒ Object



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

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

Instance Method Details

#assign(value) ⇒ Object



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

def assign(value)
  Operation.new(:assign, self, value)
end

#assign_add(value) ⇒ Object



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

def assign_add(value)
  Operation.new(:assign_add, self, value)
end

#assign_sub(value) ⇒ Object



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

def assign_sub(value)
  Operation.new(:assign_sub, self, value)
end

#initializerObject



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

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



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

def read_value
  @value
end

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



45
46
47
# File 'lib/tensor_stream/variable.rb', line 45

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

#trainable?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/tensor_stream/variable.rb', line 22

def trainable?
  @trainable
end