Class: DNN::Tensor

Inherits:
Object
  • Object
show all
Defined in:
lib/dnn/core/tensor.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, link = nil) ⇒ Tensor

Returns a new instance of Tensor.



16
17
18
19
# File 'lib/dnn/core/tensor.rb', line 16

def initialize(data, link = nil)
  @data = data
  @link = link
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



3
4
5
# File 'lib/dnn/core/tensor.rb', line 3

def data
  @data
end

Returns the value of attribute link.



4
5
6
# File 'lib/dnn/core/tensor.rb', line 4

def link
  @link
end

Class Method Details

.convert(inputs, link = nil) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/dnn/core/tensor.rb', line 6

def self.convert(inputs, link = nil)
  if inputs.is_a?(Array)
    inputs.map { |input| Tensor.new(input, link) }
  elsif inputs.is_a?(Integer) || inputs.is_a?(Float)
    Tensor.new(Xumo::SFloat[inputs], link)
  else
    Tensor.new(inputs, link)
  end
end

Instance Method Details

#*(other) ⇒ Object



47
48
49
50
# File 'lib/dnn/core/tensor.rb', line 47

def *(other)
  other = Tensor.convert(other) unless other.is_a?(DNN::Tensor) || other.is_a?(DNN::Param)
  Layers::Mul.(self, other)
end

#**(index) ⇒ Object



57
58
59
# File 'lib/dnn/core/tensor.rb', line 57

def **(index)
  Layers::Pow.new(index).(self)
end

#+(other) ⇒ Object



37
38
39
40
# File 'lib/dnn/core/tensor.rb', line 37

def +(other)
  other = Tensor.convert(other) unless other.is_a?(DNN::Tensor) || other.is_a?(DNN::Param)
  Layers::Add.(self, other)
end

#+@Object



29
30
31
# File 'lib/dnn/core/tensor.rb', line 29

def +@
  self
end

#-(other) ⇒ Object



42
43
44
45
# File 'lib/dnn/core/tensor.rb', line 42

def -(other)
  other = Tensor.convert(other) unless other.is_a?(DNN::Tensor) || other.is_a?(DNN::Param)
  Layers::Sub.(self, other)
end

#-@Object



33
34
35
# File 'lib/dnn/core/tensor.rb', line 33

def -@
  Neg.(self)
end

#/(other) ⇒ Object



52
53
54
55
# File 'lib/dnn/core/tensor.rb', line 52

def /(other)
  other = Tensor.convert(other) unless other.is_a?(DNN::Tensor) || other.is_a?(DNN::Param)
  Layers::Div.(self, other)
end

#>>(layer) ⇒ Object



21
22
23
# File 'lib/dnn/core/tensor.rb', line 21

def >>(layer)
  layer.(self)
end

#shapeObject



25
26
27
# File 'lib/dnn/core/tensor.rb', line 25

def shape
  @data.shape
end