📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" The no frills implementation of word2vec skip-gram model using NCE loss.1Author: Chip Huyen2Prepared for the class CS 20SI: "TensorFlow for Deep Learning Research"3cs20si.stanford.edu4"""56from __future__ import absolute_import7from __future__ import division8from __future__ import print_function910import os11os.environ['TF_CPP_MIN_LOG_LEVEL']='2'1213import numpy as np14import tensorflow as tf15from tensorflow.contrib.tensorboard.plugins import projector1617from process_data import process_data1819VOCAB_SIZE = 5000020BATCH_SIZE = 12821EMBED_SIZE = 128 # dimension of the word embedding vectors22SKIP_WINDOW = 1 # the context window23NUM_SAMPLED = 64 # Number of negative examples to sample.24LEARNING_RATE = 1.025NUM_TRAIN_STEPS = 1000026SKIP_STEP = 2000 # how many steps to skip before reporting the loss2728def word2vec(batch_gen):29""" Build the graph for word2vec model and train it """30# Step 1: define the placeholders for input and output31with tf.name_scope('data'):32center_words = tf.placeholder(tf.int32, shape=[BATCH_SIZE], name='center_words')33target_words = tf.placeholder(tf.int32, shape=[BATCH_SIZE, 1], name='target_words')3435# Assemble this part of the graph on the CPU. You can change it to GPU if you have GPU36# Step 2: define weights. In word2vec, it's actually the weights that we care about3738with tf.name_scope('embedding_matrix'):39embed_matrix = tf.Variable(tf.random_uniform([VOCAB_SIZE, EMBED_SIZE], -1.0, 1.0),40name='embed_matrix')4142# Step 3: define the inference43with tf.name_scope('loss'):44embed = tf.nn.embedding_lookup(embed_matrix, center_words, name='embed')4546# Step 4: construct variables for NCE loss47nce_weight = tf.Variable(tf.truncated_normal([VOCAB_SIZE, EMBED_SIZE],48stddev=1.0 / (EMBED_SIZE ** 0.5)),49name='nce_weight')50nce_bias = tf.Variable(tf.zeros([VOCAB_SIZE]), name='nce_bias')5152# define loss function to be NCE loss function53loss = tf.reduce_mean(tf.nn.nce_loss(weights=nce_weight,54biases=nce_bias,55labels=target_words,56inputs=embed,57num_sampled=NUM_SAMPLED,58num_classes=VOCAB_SIZE), name='loss')5960# Step 5: define optimizer61optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss)6263with tf.Session() as sess:64sess.run(tf.global_variables_initializer())6566total_loss = 0.0 # we use this to calculate late average loss in the last SKIP_STEP steps67writer = tf.summary.FileWriter('./graphs/no_frills/', sess.graph)68for index in range(NUM_TRAIN_STEPS):69centers, targets = next(batch_gen)70loss_batch, _ = sess.run([loss, optimizer],71feed_dict={center_words: centers, target_words: targets})72total_loss += loss_batch73if (index + 1) % SKIP_STEP == 0:74print('Average loss at step {}: {:5.1f}'.format(index, total_loss / SKIP_STEP))75total_loss = 0.076writer.close()7778def main():79batch_gen = process_data(VOCAB_SIZE, BATCH_SIZE, SKIP_WINDOW)80word2vec(batch_gen)8182if __name__ == '__main__':83main()8485