Class: TensorFlow::Variable
- Inherits:
-
Object
- Object
- TensorFlow::Variable
- Defined in:
- lib/tensorflow/variable.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #-(other) ⇒ Object
- #assign(value) ⇒ Object
- #assign_add(value) ⇒ Object
- #assign_sub(value) ⇒ Object
-
#initialize(initial_value = nil, dtype: nil, shape: nil, name: nil) ⇒ Variable
constructor
A new instance of Variable.
- #inspect ⇒ Object
- #read_value ⇒ Object
- #shape ⇒ Object
- #to_ptr ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(initial_value = nil, dtype: nil, shape: nil, name: nil) ⇒ Variable
Returns a new instance of Variable.
5 6 7 8 9 10 11 |
# File 'lib/tensorflow/variable.rb', line 5 def initialize(initial_value = nil, dtype: nil, shape: nil, name: nil) @dtype = dtype || Utils.infer_type(Array(initial_value).flatten) @shape = shape @name = name @pointer = RawOps.var_handle_op(dtype: type_enum, shape: [], shared_name: Utils.default_context.shared_name) assign(initial_value) if initial_value end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/tensorflow/variable.rb', line 3 def name @name end |
Instance Method Details
#+(other) ⇒ Object
35 36 37 38 |
# File 'lib/tensorflow/variable.rb', line 35 def +(other) v = Variable.new(read_value.value, dtype: @dtype) v.assign_add(other).read_value end |
#-(other) ⇒ Object
40 41 42 43 |
# File 'lib/tensorflow/variable.rb', line 40 def -(other) v = Variable.new(read_value.value, dtype: @dtype) v.assign_sub(other).read_value end |
#assign(value) ⇒ Object
13 14 15 16 17 |
# File 'lib/tensorflow/variable.rb', line 13 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
19 20 21 22 23 |
# File 'lib/tensorflow/variable.rb', line 19 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
25 26 27 28 29 |
# File 'lib/tensorflow/variable.rb', line 25 def assign_sub(value) value = TensorFlow.convert_to_tensor(value, dtype: @dtype) RawOps.assign_sub_variable_op(resource: @pointer, value: value) self end |
#inspect ⇒ Object
53 54 55 56 57 58 |
# File 'lib/tensorflow/variable.rb', line 53 def inspect value = read_value inspection = %w(numo shape dtype).map { |v| "#{v}: #{value.send(v).inspect}"} inspection.unshift("name: #{name}") if name "#<#{self.class} #{inspection.join(", ")}>" end |
#read_value ⇒ Object
31 32 33 |
# File 'lib/tensorflow/variable.rb', line 31 def read_value RawOps.read_variable_op(resource: @pointer, dtype: type_enum) end |
#shape ⇒ Object
49 50 51 |
# File 'lib/tensorflow/variable.rb', line 49 def shape read_value.shape end |
#to_ptr ⇒ Object
60 61 62 |
# File 'lib/tensorflow/variable.rb', line 60 def to_ptr read_value.to_ptr end |
#to_s ⇒ Object
45 46 47 |
# File 'lib/tensorflow/variable.rb', line 45 def to_s inspect end |