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 TensorMixins

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

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

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



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

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

.variables_initializer(collection) ⇒ Object



71
72
73
# File 'lib/tensor_stream/variable.rb', line 71

def self.variables_initializer(collection)
  TensorStream.group(TensorStream.get_default_graph.get_collection(collection).map(&:initializer))
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, self, value, name: name)
end

#assign_add(value, name: nil) ⇒ Object



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

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

#assign_sub(value) ⇒ Object



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

def assign_sub(value)
  TensorStream.check_data_types(self, value)
  _op(:assign_sub, self, value)
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



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

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
55
# File 'lib/tensor_stream/variable.rb', line 52

def read_value
  @value = buffer.to_ruby if buffer
  @value
end

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



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

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