Class: Toy::Linear
- Inherits:
-
Object
- Object
- Toy::Linear
- Defined in:
- lib/toy.rb
Overview
Toy::Linear
y = x · W + b (b optional; bias=false skips it).
Instance Attribute Summary collapse
-
#b ⇒ Object
Returns the value of attribute b.
-
#has_bias ⇒ Object
Returns the value of attribute has_bias.
-
#in_dim ⇒ Object
Returns the value of attribute in_dim.
-
#out_dim ⇒ Object
Returns the value of attribute out_dim.
-
#w ⇒ Object
Returns the value of attribute w.
Instance Method Summary collapse
-
#forward(x) ⇒ Object
x: [T, in_dim] → [T, out_dim].
-
#initialize(in_dim, out_dim, with_bias) ⇒ Linear
constructor
A new instance of Linear.
- #param_count ⇒ Object
- #summary ⇒ Object
Constructor Details
Instance Attribute Details
#b ⇒ Object
Returns the value of attribute b.
94 95 96 |
# File 'lib/toy.rb', line 94 def b @b end |
#has_bias ⇒ Object
Returns the value of attribute has_bias.
94 95 96 |
# File 'lib/toy.rb', line 94 def has_bias @has_bias end |
#in_dim ⇒ Object
Returns the value of attribute in_dim.
94 95 96 |
# File 'lib/toy.rb', line 94 def in_dim @in_dim end |
#out_dim ⇒ Object
Returns the value of attribute out_dim.
94 95 96 |
# File 'lib/toy.rb', line 94 def out_dim @out_dim end |
#w ⇒ Object
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_count ⇒ Object
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 |
#summary ⇒ Object
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 |