Class: GRX::NN::Linear
Overview
Linear — Capa densa (fully connected) y = x @ W^T + b
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
- #forward(x) ⇒ Object
-
#initialize(in_features, out_features, bias: true) ⇒ Linear
constructor
A new instance of Linear.
- #to_s ⇒ Object
Methods inherited from Module
#call, #parameters, #zero_grad
Constructor Details
#initialize(in_features, out_features, bias: true) ⇒ Linear
Returns a new instance of Linear.
45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/grx/nn.rb', line 45 def initialize(in_features, out_features, bias: true) @in_features = in_features @out_features = out_features @use_bias = bias # Pesos: Xavier uniform (bueno para tanh/sigmoid) @weight = Tensor.xavier_uniform([out_features, in_features], requires_grad: true) # Bias: ceros @bias = bias ? Tensor.zeros([out_features], requires_grad: true) : nil end |
Instance Attribute Details
#bias ⇒ Object (readonly)
Returns the value of attribute bias.
43 44 45 |
# File 'lib/grx/nn.rb', line 43 def bias @bias end |
#weight ⇒ Object (readonly)
Returns the value of attribute weight.
43 44 45 |
# File 'lib/grx/nn.rb', line 43 def weight @weight end |
Instance Method Details
#forward(x) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/grx/nn.rb', line 57 def forward(x) # x: [batch, in_features] → out: [batch, out_features] # out = x @ W^T out = x.matmul(@weight.transpose) if @use_bias # Sumamos bias fila por fila. # Repetimos @bias batch_size veces para crear un tensor [batch, out_features] # que comparte el grafo con @bias original. batch_size = x.shape[0] # Tile del bias: concatenamos el mismo tensor bias_size veces # usando operaciones que mantienen el grafo conectado bias_tiled = _tile_bias(@bias, batch_size, @out_features) out + bias_tiled else out end end |
#to_s ⇒ Object
108 109 110 |
# File 'lib/grx/nn.rb', line 108 def to_s "Linear(#{@in_features} → #{@out_features}, bias: #{@use_bias})" end |