Class: Toy::Embedding

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

Overview

Toy::Embedding

Lookup table: row[i] = weight[i, :].

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vocab, d) ⇒ Embedding

Returns a new instance of Embedding.



133
134
135
136
137
# File 'lib/toy.rb', line 133

def initialize(vocab, d)
  @vocab  = vocab
  @d      = d
  @weight = Mat.new(vocab, d)
end

Instance Attribute Details

#dObject

Returns the value of attribute d.



131
132
133
# File 'lib/toy.rb', line 131

def d
  @d
end

#vocabObject

Returns the value of attribute vocab.



131
132
133
# File 'lib/toy.rb', line 131

def vocab
  @vocab
end

#weightObject

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_countObject



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

#summaryObject



174
# File 'lib/toy.rb', line 174

def summary;     "Embedding(vocab=" + @vocab.to_s + ", d=" + @d.to_s + ")"; end