Class: Torch::NN::Linear

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

Instance Attribute Summary collapse

Attributes inherited from Module

#training

Instance Method Summary collapse

Methods inherited from Module

#_apply, #add_module, #apply, #buffers, #call, #children, #cpu, #cuda, #deep_dup, #double, #eval, #float, #half, #inspect, #load_state_dict, #method_missing, #modules, #named_buffers, #named_children, #named_modules, #named_parameters, #parameters, #register_buffer, #register_parameter, #requires_grad!, #respond_to?, #share_memory, #state_dict, #to, #train, #type, #zero_grad

Methods included from Utils

#_activation_fn, #_clones, #_ntuple, #_pair, #_quadrupal, #_single, #_triple

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
17
18
19
# File 'lib/torch/nn/linear.rb', line 6

def initialize(in_features, out_features, bias: true)
  super()
  @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))
  else
    register_parameter("bias", nil)
  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

#in_featuresObject (readonly)

Returns the value of attribute in_features.



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

def in_features
  @in_features
end

#out_featuresObject (readonly)

Returns the value of attribute out_features.



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

def out_features
  @out_features
end

Instance Method Details

#extra_inspectObject



34
35
36
# File 'lib/torch/nn/linear.rb', line 34

def extra_inspect
  format("in_features: %s, out_features: %s, bias: %s", @in_features, @out_features, !@bias.nil?)
end

#forward(input) ⇒ Object



30
31
32
# File 'lib/torch/nn/linear.rb', line 30

def forward(input)
  F.linear(input, @weight, @bias)
end

#reset_parametersObject



21
22
23
24
25
26
27
28
# File 'lib/torch/nn/linear.rb', line 21

def reset_parameters
  Init.kaiming_uniform!(@weight, a: Math.sqrt(5))
  if @bias
    fan_in, _ = Init._calculate_fan_in_and_fan_out(@weight)
    bound = 1 / Math.sqrt(fan_in)
    Init.uniform!(@bias, a: -bound, b: bound)
  end
end