Class: GRX::NN::Linear

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

Overview

================================================================

Linear — Dense fully connected layer y = x @ W^T + b

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Module

#call, #load_weights, #parameters, #save_weights, #zero_grad

Constructor Details

#initialize(in_features, out_features, bias: true) ⇒ Linear

Returns a new instance of Linear.



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/grx/nn.rb', line 53

def initialize(in_features, out_features, bias: true)
  @in_features  = in_features
  @out_features = out_features
  @use_bias     = bias

  # Weights: Xavier uniform initialization
  @weight = Tensor.xavier_uniform([out_features, in_features], requires_grad: true)

  # Bias: initialized to zeros
  @bias = bias ? Tensor.zeros([out_features], requires_grad: true) : nil
end

Instance Attribute Details

#biasObject (readonly)

Returns the value of attribute bias.



51
52
53
# File 'lib/grx/nn.rb', line 51

def bias
  @bias
end

#weightObject (readonly)

Returns the value of attribute weight.



51
52
53
# File 'lib/grx/nn.rb', line 51

def weight
  @weight
end

Instance Method Details

#forward(x) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/grx/nn.rb', line 65

def forward(x)
  # x: [batch, in_features]  ->  out: [batch, out_features]
  # out = x @ W^T
  out = x.matmul(@weight.transpose)

  if @use_bias
    batch_size = x.shape[0]
    bias_tiled = _tile_bias(@bias, batch_size, @out_features)
    out + bias_tiled
  else
    out
  end
end

#to_sObject



104
105
106
# File 'lib/grx/nn.rb', line 104

def to_s
  "Linear(#{@in_features} -> #{@out_features}, bias: #{@use_bias})"
end