Class: Toy::Linear

Inherits:
Object
  • Object
show all
Defined in:
lib/toy.rb

Overview

Toy::Linear

y = x · W + b  (b optional; bias=false skips it).

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(in_dim, out_dim, with_bias) ⇒ Linear

Returns a new instance of Linear.



96
97
98
99
100
101
102
# File 'lib/toy.rb', line 96

def initialize(in_dim, out_dim, with_bias)
  @in_dim   = in_dim
  @out_dim  = out_dim
  @has_bias = with_bias
  @w        = Mat.new(in_dim, out_dim)
  @b        = Array.new(out_dim, 0.0)
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



94
95
96
# File 'lib/toy.rb', line 94

def b
  @b
end

#has_biasObject

Returns the value of attribute has_bias.



94
95
96
# File 'lib/toy.rb', line 94

def has_bias
  @has_bias
end

#in_dimObject

Returns the value of attribute in_dim.



94
95
96
# File 'lib/toy.rb', line 94

def in_dim
  @in_dim
end

#out_dimObject

Returns the value of attribute out_dim.



94
95
96
# File 'lib/toy.rb', line 94

def out_dim
  @out_dim
end

#wObject

Returns the value of attribute w.



94
95
96
# File 'lib/toy.rb', line 94

def w
  @w
end

Instance Method Details

#forward(x) ⇒ Object

x: [T, in_dim] → [T, out_dim]



105
106
107
108
109
110
111
# File 'lib/toy.rb', line 105

def forward(x)
  out = x.matmul(@w)
  if @has_bias
    Toy.add_bias!(out, @b)
  end
  out
end

#param_countObject



117
118
119
120
121
122
123
# File 'lib/toy.rb', line 117

def param_count
  n = @in_dim * @out_dim
  if @has_bias
    n = n + @out_dim
  end
  n
end

#summaryObject



113
114
115
116
# File 'lib/toy.rb', line 113

def summary
  bs = @has_bias ? "true" : "false"
  "Linear(in=" + @in_dim.to_s + ", out=" + @out_dim.to_s + ", bias=" + bs + ")"
end