📚 The CoCalc Library - books, templates and other resources
License: OTHER
""" starter code for word2vec skip-gram model with NCE loss1Eager execution2CS 20: "TensorFlow for Deep Learning Research"3cs20.stanford.edu4Chip Huyen ([email protected]) & Akshay Agrawal ([email protected])5Lecture 046"""78import os9os.environ['TF_CPP_MIN_LOG_LEVEL']='2'1011import numpy as np12import tensorflow as tf13import tensorflow.contrib.eager as tfe1415import utils16import word2vec_utils1718# Enable eager execution!19#############################20########## TO DO ############21#############################2223# Model hyperparameters24VOCAB_SIZE = 5000025BATCH_SIZE = 12826EMBED_SIZE = 128 # dimension of the word embedding vectors27SKIP_WINDOW = 1 # the context window28NUM_SAMPLED = 64 # number of negative examples to sample29LEARNING_RATE = 1.030NUM_TRAIN_STEPS = 10000031VISUAL_FLD = 'visualization'32SKIP_STEP = 50003334# Parameters for downloading data35DOWNLOAD_URL = 'http://mattmahoney.net/dc/text8.zip'36EXPECTED_BYTES = 313440163738class Word2Vec(object):39def __init__(self, vocab_size, embed_size, num_sampled=NUM_SAMPLED):40self.vocab_size = vocab_size41self.num_sampled = num_sampled42# Create the variables: an embedding matrix, nce_weight, and nce_bias43#############################44########## TO DO ############45#############################46self.embed_matrix = None47self.nce_weight = None48self.nce_bias = None4950def compute_loss(self, center_words, target_words):51"""Computes the forward pass of word2vec with the NCE loss."""52# Look up the embeddings for the center words53#############################54########## TO DO ############55#############################56embed = None5758# Compute the loss, using tf.reduce_mean and tf.nn.nce_loss59#############################60########## TO DO ############61#############################62loss = None63return loss646566def gen():67yield from word2vec_utils.batch_gen(DOWNLOAD_URL, EXPECTED_BYTES,68VOCAB_SIZE, BATCH_SIZE, SKIP_WINDOW,69VISUAL_FLD)7071def main():72dataset = tf.data.Dataset.from_generator(gen, (tf.int32, tf.int32),73(tf.TensorShape([BATCH_SIZE]),74tf.TensorShape([BATCH_SIZE, 1])))75optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE)76# Create the model77#############################78########## TO DO ############79#############################80model = None8182# Create the gradients function, using `tfe.implicit_value_and_gradients`83#############################84########## TO DO ############85#############################86grad_fn = None8788total_loss = 0.0 # for average loss in the last SKIP_STEP steps89num_train_steps = 090while num_train_steps < NUM_TRAIN_STEPS:91for center_words, target_words in tfe.Iterator(dataset):92if num_train_steps >= NUM_TRAIN_STEPS:93break9495# Compute the loss and gradients, and take an optimization step.96#############################97########## TO DO ############98#############################99100if (num_train_steps + 1) % SKIP_STEP == 0:101print('Average loss at step {}: {:5.1f}'.format(102num_train_steps, total_loss / SKIP_STEP))103total_loss = 0.0104num_train_steps += 1105106107if __name__ == '__main__':108main()109110111