Class: TensorFlow::Variable

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

Instance Method Summary collapse

Constructor Details

#initialize(initial_value, dtype: nil) ⇒ Variable

Returns a new instance of Variable.



3
4
5
6
7
# File 'lib/tensorflow/variable.rb', line 3

def initialize(initial_value, dtype: nil)
  @dtype = dtype || Utils.infer_type(Array(initial_value).flatten)
  @pointer = RawOps.var_handle_op(dtype: type_enum, shape: [], shared_name: Utils.default_context.shared_name)
  assign(initial_value)
end

Instance Method Details

#+(other) ⇒ Object



31
32
33
34
# File 'lib/tensorflow/variable.rb', line 31

def +(other)
  v = Variable.new(read_value.value, dtype: @dtype)
  v.assign_add(other).read_value
end

#-(other) ⇒ Object



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

def -(other)
  v = Variable.new(read_value.value, dtype: @dtype)
  v.assign_sub(other).read_value
end

#assign(value) ⇒ Object



9
10
11
12
13
# File 'lib/tensorflow/variable.rb', line 9

def assign(value)
  value = TensorFlow.convert_to_tensor(value, dtype: @dtype)
  RawOps.assign_variable_op(resource: @pointer, value: value)
  self
end

#assign_add(value) ⇒ Object



15
16
17
18
19
# File 'lib/tensorflow/variable.rb', line 15

def assign_add(value)
  value = TensorFlow.convert_to_tensor(value, dtype: @dtype)
  RawOps.assign_add_variable_op(resource: @pointer, value: value)
  self
end

#assign_sub(value) ⇒ Object



21
22
23
24
25
# File 'lib/tensorflow/variable.rb', line 21

def assign_sub(value)
  value = TensorFlow.convert_to_tensor(value, dtype: @dtype)
  RawOps.assign_sub_variable_op(resource: @pointer, value: value)
  self
end

#inspectObject



45
46
47
48
49
# File 'lib/tensorflow/variable.rb', line 45

def inspect
  value = read_value
  inspection = %w(numo shape dtype).map { |v| "#{v}: #{value.send(v).inspect}"}
  "#<#{self.class} #{inspection.join(", ")}>"
end

#read_valueObject



27
28
29
# File 'lib/tensorflow/variable.rb', line 27

def read_value
  RawOps.read_variable_op(resource: @pointer, dtype: type_enum)
end

#to_sObject



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

def to_s
  inspect
end