Class: RubyZero::Core::Functions::Mean

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

Instance Attribute Summary

Attributes inherited from Function

#inputs, #output

Instance Method Summary collapse

Methods inherited from Function

#call, #inspect, plot

Constructor Details

#initialize(axis) ⇒ Mean

Returns a new instance of Mean.



66
67
68
# File 'lib/rubyzero/core/functions/tensor_functions.rb', line 66

def initialize(axis)
    @axis = axis
end

Instance Method Details

#backward(dy) ⇒ Object



76
77
78
# File 'lib/rubyzero/core/functions/tensor_functions.rb', line 76

def backward(dy)
    return [dy.repeat(@repeats, axis: @axis)]
end

#forward(x1) ⇒ Object



69
70
71
72
73
74
75
# File 'lib/rubyzero/core/functions/tensor_functions.rb', line 69

def forward(x1)
    @repeats = x1.shape[@axis]
    arr = x1.data
    arr = arr.mean(axis: @axis)
    new_t = RubyZero::Core::Tensor.new(arr, device: x1.device)
    return new_t
end