Class: Glove::Workers::CooccurrenceWorker
- Inherits:
-
Object
- Object
- Glove::Workers::CooccurrenceWorker
- Extended by:
- Forwardable
- Defined in:
- lib/glove/workers/cooccurrence_worker.rb
Overview
Constructs the co-occurrence matrix for Model
Instance Attribute Summary collapse
-
#token_index ⇒ Hash{String=>Integer}
readonly
Clone of @caller.token_index.
-
#token_pairs ⇒ Object
readonly
Returns the value of attribute token_pairs.
-
#word_biases ⇒ Array<(Glove::TokenPair)>
readonly
Clone of @caller.token_pairs.
Instance Method Summary collapse
-
#build_cooc_matrix_col(slice) ⇒ Array
Creates a vector column for the cooc_matrix based on given token.
-
#initialize(caller) ⇒ CooccurrenceWorker
constructor
Creates instance of the class.
-
#run ⇒ GSL::Matrix
Perform the building of the matrix.
Constructor Details
#initialize(caller) ⇒ CooccurrenceWorker
Creates instance of the class
18 19 20 21 22 |
# File 'lib/glove/workers/cooccurrence_worker.rb', line 18 def initialize(caller) @caller = caller @token_index = @caller.token_index.dup @token_pairs = @caller.token_pairs.dup end |
Instance Attribute Details
#token_index ⇒ Hash{String=>Integer} (readonly)
Returns Clone of @caller.token_index.
11 12 13 |
# File 'lib/glove/workers/cooccurrence_worker.rb', line 11 def token_index @token_index end |
#token_pairs ⇒ Object (readonly)
Returns the value of attribute token_pairs.
11 12 13 |
# File 'lib/glove/workers/cooccurrence_worker.rb', line 11 def token_pairs @token_pairs end |
#word_biases ⇒ Array<(Glove::TokenPair)> (readonly)
Returns Clone of @caller.token_pairs.
11 |
# File 'lib/glove/workers/cooccurrence_worker.rb', line 11 attr_reader :token_index, :token_pairs |
Instance Method Details
#build_cooc_matrix_col(slice) ⇒ Array
Creates a vector column for the cooc_matrix based on given token. Calculates sum for how many times the word exists in the constext of the entire vocabulary
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/glove/workers/cooccurrence_worker.rb', line 41 def build_cooc_matrix_col(slice) token = slice[0] vector = GSL::Vector.alloc(token_index.size) token_pairs.each do |pair| key = token_index[pair.token] sum = pair.neighbors.select{ |word| word == token }.size vector[key] += sum end vector.to_a end |
#run ⇒ GSL::Matrix
Perform the building of the matrix
27 28 29 30 31 32 33 |
# File 'lib/glove/workers/cooccurrence_worker.rb', line 27 def run vectors = Parallel.map(token_index, in_processes: threads) do |slice| build_cooc_matrix_col(slice) end GSL::Matrix.alloc(*vectors) end |