📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" The mo 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 = 2000026SKIP_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 output31# center_words have to be int to work on embedding lookup3233# TO DO343536# Step 2: define weights. In word2vec, it's actually the weights that we care about37# vocab size x embed size38# initialized to random uniform -1 to 13940# TOO DO414243# Step 3: define the inference44# get the embed of input words using tf.nn.embedding_lookup45# embed = tf.nn.embedding_lookup(embed_matrix, center_words, name='embed')4647# TO DO484950# Step 4: construct variables for NCE loss51# tf.nn.nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, ...)52# nce_weight (vocab size x embed size), intialized to truncated_normal stddev=1.0 / (EMBED_SIZE ** 0.5)53# bias: vocab size, initialized to 05455# TO DO565758# define loss function to be NCE loss function59# tf.nn.nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, ...)60# need to get the mean accross the batch61# note: you should use embedding of center words for inputs, not center words themselves6263# TO DO646566# Step 5: define optimizer6768# TO DO69707172with tf.Session() as sess:73# TO DO: initialize variables747576total_loss = 0.0 # we use this to calculate the average loss in the last SKIP_STEP steps77writer = tf.summary.FileWriter('./graphs/no_frills/', sess.graph)78for index in range(NUM_TRAIN_STEPS):79centers, targets = next(batch_gen)80# TO DO: create feed_dict, run optimizer, fetch loss_batch8182total_loss += loss_batch83if (index + 1) % SKIP_STEP == 0:84print('Average loss at step {}: {:5.1f}'.format(index, total_loss / SKIP_STEP))85total_loss = 0.086writer.close()8788def main():89batch_gen = process_data(VOCAB_SIZE, BATCH_SIZE, SKIP_WINDOW)90word2vec(batch_gen)9192if __name__ == '__main__':93main()949596