Sha256: ec40509de5b0718ee945727fac6ebb1f15f32b25cfe5a24fd8f88d2aff9ea08f

Contents?: true

Size: 1.26 KB

Versions: 11

Compression:

Stored size: 1.26 KB

Contents

module DNN
  class Tensor
    attr_reader :data
    attr_accessor :link

    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

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

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

    def shape
      @data.shape
    end

    def +@
      self
    end

    def -@
      Neg.(self)
    end

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

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

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

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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
ruby-dnn-1.3.0 lib/dnn/core/tensor.rb
ruby-dnn-1.2.3 lib/dnn/core/tensor.rb
ruby-dnn-1.2.2 lib/dnn/core/tensor.rb
ruby-dnn-1.2.1 lib/dnn/core/tensor.rb
ruby-dnn-1.2.0 lib/dnn/core/tensor.rb
ruby-dnn-1.1.6 lib/dnn/core/tensor.rb
ruby-dnn-1.1.5 lib/dnn/core/tensor.rb
ruby-dnn-1.1.4 lib/dnn/core/tensor.rb
ruby-dnn-1.1.3 lib/dnn/core/tensor.rb
ruby-dnn-1.1.2 lib/dnn/core/tensor.rb
ruby-dnn-1.1.1 lib/dnn/core/tensor.rb