Class: RubyZero::Core::Functions::Function

Inherits:
Object
  • Object
show all
Defined in:
lib/rubyzero/core/functions/function.rb

Overview

Function class

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, **kwargs, &block) ⇒ Function

Returns a new instance of Function.



8
9
10
# File 'lib/rubyzero/core/functions/function.rb', line 8

def initialize(*args, **kwargs, &block)
    
end

Instance Attribute Details

#inputsObject (readonly)

Returns the value of attribute inputs.



7
8
9
# File 'lib/rubyzero/core/functions/function.rb', line 7

def inputs
  @inputs
end

#outputObject (readonly)

Returns the value of attribute output.



7
8
9
# File 'lib/rubyzero/core/functions/function.rb', line 7

def output
  @output
end

Class Method Details

.plot(range: (-10..10).step(0.01)) ⇒ Object



29
30
31
32
33
34
# File 'lib/rubyzero/core/functions/function.rb', line 29

def self.plot(range: (-10..10).step(0.01))
    inputs = range.to_a
    outputs = inputs.map{|t| self.new().call(RubyZero::Core::Tensor.new(t)).data[0]}
    plot = UnicodePlot.lineplot(inputs, outputs, name: self.name)
    plot.render
end

Instance Method Details

#backward(*args, **kwargs, &block) ⇒ Object

Raises:

  • (Execptions::NotImplementedError)


14
15
16
# File 'lib/rubyzero/core/functions/function.rb', line 14

def backward(*args, **kwargs, &block)
    raise Execptions::NotImplementedError, "#{self.class}#backward() not implemented"
end

#call(*args) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/rubyzero/core/functions/function.rb', line 17

def call(*args)
    @inputs = args
    @output = forward(*args)
    if @inputs.any?{|t| t.requires_grad?}
        @output.grad_fn = self
        @output.requires_grad = true
    end
    return @output
end

#forward(*args, **kwargs, &block) ⇒ Object

Raises:

  • (Execptions::NotImplementedError)


11
12
13
# File 'lib/rubyzero/core/functions/function.rb', line 11

def forward(*args, **kwargs, &block)
    raise Execptions::NotImplementedError, "#{self.class}#forward() not implemented"
end

#inspectObject



26
27
28
# File 'lib/rubyzero/core/functions/function.rb', line 26

def inspect
    return "#<#{self.class}>"
end