2
votes

I am using word2vec module of TF. I would like to read a data file which consists of multiple lines. Each line is made of multiple words. Each word in one line could be either source or target word.

I followed the tutorial, but I didn't find the so-called example to read customized data in

For the case of Skip-Gram modeling, we've actually already done this for you as an example in tensorflow/models/embedding/word2vec.py.

Should I change the code in word2vec_kernel.cc or use tf.TextLineReader. If so, how? Thanks!

1

1 Answers

0
votes

If the different lines of your input file represent different contexts then tensorflow/models/embedding/word2vec.py won't work for you as it wraps each line into a single giant sentence. You will need to rewrite the customer operator kernel here:

https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec_kernels.cc

Below is an example of one of the functions that needs to change:

void NextExample(int *label, int *example) {
    while (true) {
        if (label_pos_ >= label_limit_) {
            ++total_words_processed_;
            ++sentence_index_;
            // check if we hit the end of the sentence
            if (sentence_index_ >= sentence_size_) {
                sentence_index_ = 0;
                ++corpus_idx_;
                // check if we hit the end of the corpus
                if (corpus_idx_ >= nrows_) {
                    ++current_epoch_;
                    corpus_idx_ = 0;
                }
                sentence_ = corpus_[corpus_idx_];

            }
            label_pos_ = std::max<int>(0, sentence_index_ - window_size_);
            label_limit_ =
                    std::min<int>(sentence_.size(), sentence_index_ + window_size_ + 1);
        }
        if (sentence_index_ != label_pos_) // add constraint that tokens are different
            break;
        ++label_pos_;
    }
    *example = sentence_[sentence_index_];
    *label = sentence_[label_pos_++];
}