Class: Toy::Embedding
- Inherits:
-
Object
- Object
- Toy::Embedding
- Defined in:
- lib/toy.rb
Overview
Toy::Embedding
Lookup table: row[i] = weight[i, :].
Instance Attribute Summary collapse
-
#d ⇒ Object
Returns the value of attribute d.
-
#vocab ⇒ Object
Returns the value of attribute vocab.
-
#weight ⇒ Object
Returns the value of attribute weight.
Instance Method Summary collapse
-
#initialize(vocab, d) ⇒ Embedding
constructor
A new instance of Embedding.
-
#lookup(ids) ⇒ Object
ids: [T] (Array<Int>) → [T, D].
- #param_count ⇒ Object
-
#slice(start, count) ⇒ Object
Slice rows [start, start + count) as a fresh Mat.
- #summary ⇒ Object
Constructor Details
Instance Attribute Details
#d ⇒ Object
Returns the value of attribute d.
131 132 133 |
# File 'lib/toy.rb', line 131 def d @d end |
#vocab ⇒ Object
Returns the value of attribute vocab.
131 132 133 |
# File 'lib/toy.rb', line 131 def vocab @vocab end |
#weight ⇒ Object
Returns the value of attribute weight.
131 132 133 |
# File 'lib/toy.rb', line 131 def weight @weight end |
Instance Method Details
#lookup(ids) ⇒ Object
ids: [T] (Array<Int>) → [T, D]
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
# File 'lib/toy.rb', line 140 def lookup(ids) t = ids.length d = @d out = Mat.new(t, d) i = 0 while i < t row = ids[i] j = 0 while j < d out.flat[i * d + j] = @weight.flat[row * d + j] j += 1 end i += 1 end out end |
#param_count ⇒ Object
175 |
# File 'lib/toy.rb', line 175 def param_count; @vocab * @d; end |
#slice(start, count) ⇒ Object
Slice rows [start, start + count) as a fresh Mat. Used for absolute position embeddings.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/toy.rb', line 159 def slice(start, count) d = @d out = Mat.new(count, d) i = 0 while i < count j = 0 while j < d out.flat[i * d + j] = @weight.flat[(start + i) * d + j] j += 1 end i += 1 end out end |
#summary ⇒ Object
174 |
# File 'lib/toy.rb', line 174 def summary; "Embedding(vocab=" + @vocab.to_s + ", d=" + @d.to_s + ")"; end |