11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
4+ using System . Linq ;
35using System . Text ;
46using Tensorflow ;
7+ using TensorFlowNET . Examples . Utility ;
58
69namespace TensorFlowNET . Examples
710{
@@ -24,6 +27,17 @@ public class Word2Vec : Python, IExample
2427
2528 // Evaluation Parameters
2629 string [ ] eval_words = new string [ ] { "five" , "of" , "going" , "hardware" , "american" , "britain" } ;
30+ string [ ] text_words ;
31+
32+ // Word2Vec Parameters
33+ int embedding_size = 200 ; // Dimension of the embedding vector
34+ int max_vocabulary_size = 50000 ; // Total number of different words in the vocabulary
35+ int min_occurrence = 10 ; // Remove all words that does not appears at least n times
36+ int skip_window = 3 ; // How many words to consider left and right
37+ int num_skips = 2 ; // How many times to reuse an input to generate a label
38+ int num_sampled = 64 ; // Number of negative examples to sample
39+
40+ int data_index ;
2741
2842 public bool Run ( )
2943 {
@@ -33,12 +47,78 @@ public bool Run()
3347
3448 tf . train . import_meta_graph ( "graph/word2vec.meta" ) ;
3549
50+ // Initialize the variables (i.e. assign their default value)
51+ var init = tf . global_variables_initializer ( ) ;
52+
53+ with ( tf . Session ( graph ) , sess =>
54+ {
55+ sess . run ( init ) ;
56+ } ) ;
57+
3658 return false ;
3759 }
3860
61+ // Generate training batch for the skip-gram model
62+ private void next_batch ( )
63+ {
64+
65+ }
66+
3967 public void PrepareData ( )
4068 {
41- var url = "" ;
69+ // Download graph meta
70+ var url = "https://github.com/SciSharp/TensorFlow.NET/raw/master/graph/word2vec.meta" ;
71+ Web . Download ( url , "graph" , "word2vec.meta" ) ;
72+
73+ // Download a small chunk of Wikipedia articles collection
74+ url = "https://raw.githubusercontent.com/SciSharp/TensorFlow.NET/master/data/text8.zip" ;
75+ Web . Download ( url , "word2vec" , "text8.zip" ) ;
76+ // Unzip the dataset file. Text has already been processed
77+ Compress . UnZip ( @"word2vec\text8.zip" , "word2vec" ) ;
78+
79+ int wordId = 0 ;
80+ text_words = File . ReadAllText ( @"word2vec\text8" ) . Trim ( ) . ToLower ( ) . Split ( ) ;
81+ // Build the dictionary and replace rare words with UNK token
82+ var word2id = text_words . GroupBy ( x => x )
83+ . Select ( x => new WordId
84+ {
85+ Word = x . Key ,
86+ Occurrence = x . Count ( )
87+ } )
88+ . Where ( x => x . Occurrence >= min_occurrence ) // Remove samples with less than 'min_occurrence' occurrences
89+ . OrderByDescending ( x => x . Occurrence ) // Retrieve the most common words
90+ . Select ( x => new WordId
91+ {
92+ Word = x . Word ,
93+ Id = ++ wordId , // Assign an id to each word
94+ Occurrence = x . Occurrence
95+ } )
96+ . ToList ( ) ;
97+
98+ // Retrieve a word id, or assign it index 0 ('UNK') if not in dictionary
99+ var data = ( from word in text_words
100+ join id in word2id on word equals id . Word into wi
101+ from wi2 in wi . DefaultIfEmpty ( )
102+ select wi2 == null ? 0 : wi2 . Id ) . ToList ( ) ;
103+
104+ word2id . Insert ( 0 , new WordId { Word = "UNK" , Id = 0 , Occurrence = data . Count ( x => x == 0 ) } ) ;
105+
106+ print ( $ "Words count: { text_words . Length } ") ;
107+ print ( $ "Unique words: { text_words . Distinct ( ) . Count ( ) } ") ;
108+ print ( $ "Vocabulary size: { word2id . Count } ") ;
109+ print ( $ "Most common words: { string . Join ( ", " , word2id . Take ( 10 ) ) } ") ;
110+ }
111+
112+ private class WordId
113+ {
114+ public string Word { get ; set ; }
115+ public int Id { get ; set ; }
116+ public int Occurrence { get ; set ; }
117+
118+ public override string ToString ( )
119+ {
120+ return Word + " " + Id + " " + Occurrence ;
121+ }
42122 }
43123 }
44124}
0 commit comments