@@ -32,68 +32,53 @@ def word2vec(batch_gen):
3232 # center_words have to be int to work on embedding lookup
3333
3434 # TO DO
35- with tf .name_scope ('data' ):
36- center_words = tf .placeholder (tf .int32 , [BATCH_SIZE ], name = 'center_words' )
37- target_words = tf .placeholder (tf .int32 , [BATCH_SIZE , 1 ], name = 'target_words' )
35+
3836
3937 # Step 2: define weights. In word2vec, it's actually the weights that we care about
4038 # vocab size x embed size
4139 # initialized to random uniform -1 to 1
4240
4341 # TOO DO
44- with tf .name_scope ('embedding_matrix' ):
45- embed_matrix = tf .Variable (tf .random_uniform ([VOCAB_SIZE , EMBED_SIZE ], - 1.0 , 1.0 ), name = 'embed_matrix' )
42+
4643
4744 # Step 3: define the inference
4845 # get the embed of input words using tf.nn.embedding_lookup
4946 # embed = tf.nn.embedding_lookup(embed_matrix, center_words, name='embed')
5047
5148 # TO DO
52- with tf .name_scope ('loss' ):
53- embed = tf .nn .embedding_lookup (embed_matrix , center_words , name = 'embed' )
49+
5450
5551 # Step 4: construct variables for NCE loss
5652 # tf.nn.nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, ...)
5753 # nce_weight (vocab size x embed size), intialized to truncated_normal stddev=1.0 / (EMBED_SIZE ** 0.5)
5854 # bias: vocab size, initialized to 0
5955
6056 # TO DO
61- nce_weights = tf .Variable (tf .truncated_normal ([VOCAB_SIZE , EMBED_SIZE ],
62- stddev = 1.0 / (EMBED_SIZE ** 0.5 )),
63- name = 'nce_weights' )
64- nce_biases = tf .Variable (tf .zeros (VOCAB_SIZE ), name = 'nce_biases' )
57+
6558
6659 # define loss function to be NCE loss function
6760 # tf.nn.nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, ...)
6861 # need to get the mean accross the batch
6962 # note: you should use embedding of center words for inputs, not center words themselves
7063
7164 # TO DO
72- nce_loss = tf .nn .nce_loss (weights = nce_weights ,
73- biases = nce_biases ,
74- labels = target_words ,
75- inputs = embed ,
76- num_sampled = NUM_SAMPLED ,
77- num_classes = VOCAB_SIZE ,
78- name = 'loss' )
79- loss = tf .reduce_mean (nce_loss )
65+
8066
8167 # Step 5: define optimizer
8268
8369 # TO DO
84- optimizer = tf . GradientDescentOptimizer ( LEARNING_RATE ). minimize ( loss )
85-
70+
71+
8672
8773 with tf .Session () as sess :
8874 # TO DO: initialize variables
89- sess . run ( tf . global_variable_initializer ())
75+
9076
9177 total_loss = 0.0 # we use this to calculate the average loss in the last SKIP_STEP steps
9278 writer = tf .summary .FileWriter ('./graphs/no_frills/' , sess .graph )
9379 for index in range (NUM_TRAIN_STEPS ):
9480 centers , targets = next (batch_gen )
9581 # TO DO: create feed_dict, run optimizer, fetch loss_batch
96- _ , loss_batch = sess .run ([optimizer , loss ], feed_dict = {center_words : centers , target_words : targets })
9782
9883 total_loss += loss_batch
9984 if (index + 1 ) % SKIP_STEP == 0 :
0 commit comments