Class: Torch::NN::Linear

Inherits:
Module
  • Object
show all
Defined in:
lib/torch/nn/linear.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#biasObject (readonly)

Returns the value of attribute bias.



4
5
6
# File 'lib/torch/nn/linear.rb', line 4

def bias
  @bias
end

#weightObject (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

#inspectObject



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_parametersObject



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