Class: Torch::NN::Linear
Instance Attribute Summary collapse
-
#bias ⇒ Object
readonly
Returns the value of attribute bias.
-
#weight ⇒ Object
readonly
Returns the value of attribute weight.
Instance Method Summary collapse
- #call(input) ⇒ Object
-
#initialize(in_features, out_features, bias: true) ⇒ Linear
constructor
A new instance of Linear.
- #inspect ⇒ Object
- #reset_parameters ⇒ Object
Methods inherited from Module
#method_missing, #parameters, #respond_to?, #zero_grad
Constructor Details
#initialize(in_features, out_features, bias: true) ⇒ Linear
Returns a new instance of Linear.
6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/torch/nn/linear.rb', line 6 def initialize(in_features, out_features, bias: true) @in_features = in_features @out_features = out_features @weight = Parameter.new(Tensor.new(out_features, in_features)) if bias @bias = Parameter.new(Tensor.new(out_features)) end reset_parameters end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Torch::NN::Module
Instance Attribute Details
#bias ⇒ Object (readonly)
Returns the value of attribute bias.
4 5 6 |
# File 'lib/torch/nn/linear.rb', line 4 def bias @bias end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
4 5 6 |
# File 'lib/torch/nn/linear.rb', line 4 def weight @weight end |
Instance Method Details
#call(input) ⇒ Object
18 19 20 |
# File 'lib/torch/nn/linear.rb', line 18 def call(input) F.linear(input, @weight, @bias) end |
#inspect ⇒ Object
31 32 33 |
# File 'lib/torch/nn/linear.rb', line 31 def inspect "Linear(in_features: #{@in_features.inspect}, out_features: #{@out_features.inspect}, bias: #{(!@bias.nil?).inspect})" end |
#reset_parameters ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/torch/nn/linear.rb', line 22 def reset_parameters Init.kaiming_uniform_(@weight, Math.sqrt(5)) if @bias fan_in, _ = Init.calculate_fan_in_and_fan_out(@weight) bound = 1 / Math.sqrt(fan_in) Init.uniform_(@bias, -bound, bound) end end |