Skip to content
This repository was archived by the owner on Jan 1, 2021. It is now read-only.

Commit 7395c45

Browse files
committed
code for lecture 4
1 parent 4828e81 commit 7395c45

File tree

8 files changed

+853
-0
lines changed

8 files changed

+853
-0
lines changed

examples/03_logreg.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
""" Solution for simple logistic regression model for MNIST
2+
with tf.data module
3+
MNIST dataset: yann.lecun.com/exdb/mnist/
4+
Created by Chip Huyen (chiphuyen@cs.stanford.edu)
5+
CS20: "TensorFlow for Deep Learning Research"
6+
cs20.stanford.edu
7+
Lecture 03
8+
"""
9+
import os
10+
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
11+
12+
import numpy as np
13+
import tensorflow as tf
14+
import time
15+
16+
import utils
17+
18+
# Define paramaters for the model
19+
learning_rate = 0.01
20+
batch_size = 128
21+
n_epochs = 30
22+
n_train = 60000
23+
n_test = 10000
24+
25+
# Step 1: Read in data
26+
mnist_folder = 'data/mnist'
27+
utils.download_mnist(mnist_folder)
28+
train, val, test = utils.read_mnist(mnist_folder, flatten=True)
29+
30+
# Step 2: Create datasets and iterator
31+
train_data = tf.data.Dataset.from_tensor_slices(train)
32+
train_data = train_data.shuffle(10000) # if you want to shuffle your data
33+
train_data = train_data.batch(batch_size)
34+
35+
test_data = tf.data.Dataset.from_tensor_slices(test)
36+
test_data = test_data.batch(batch_size)
37+
38+
iterator = tf.data.Iterator.from_structure(train_data.output_types,
39+
train_data.output_shapes)
40+
img, label = iterator.get_next()
41+
42+
train_init = iterator.make_initializer(train_data) # initializer for train_data
43+
test_init = iterator.make_initializer(test_data) # initializer for train_data
44+
45+
# Step 3: create weights and bias
46+
# w is initialized to random variables with mean of 0, stddev of 0.01
47+
# b is initialized to 0
48+
# shape of w depends on the dimension of X and Y so that Y = tf.matmul(X, w)
49+
# shape of b depends on Y
50+
w = tf.get_variable(name='weights', shape=(784, 10), initializer=tf.random_normal_initializer(0, 0.01))
51+
b = tf.get_variable(name='bias', shape=(1, 10), initializer=tf.zeros_initializer())
52+
53+
# Step 4: build model
54+
# the model that returns the logits.
55+
# this logits will be later passed through softmax layer
56+
logits = tf.matmul(img, w) + b
57+
58+
# Step 5: define loss function
59+
# use cross entropy of softmax of logits as the loss function
60+
entropy = tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=label, name='entropy')
61+
loss = tf.reduce_mean(entropy, name='loss') # computes the mean over all the examples in the batch
62+
63+
# Step 6: define training op
64+
# using gradient descent with learning rate of 0.01 to minimize loss
65+
optimizer = tf.train.AdamOptimizer(learning_rate).minimize(loss)
66+
67+
# Step 7: calculate accuracy with test set
68+
preds = tf.nn.softmax(logits)
69+
correct_preds = tf.equal(tf.argmax(preds, 1), tf.argmax(label, 1))
70+
accuracy = tf.reduce_sum(tf.cast(correct_preds, tf.float32))
71+
72+
writer = tf.summary.FileWriter('./graphs/logreg', tf.get_default_graph())
73+
with tf.Session() as sess:
74+
75+
start_time = time.time()
76+
sess.run(tf.global_variables_initializer())
77+
78+
# train the model n_epochs times
79+
for i in range(n_epochs):
80+
sess.run(train_init) # drawing samples from train_data
81+
total_loss = 0
82+
n_batches = 0
83+
try:
84+
while True:
85+
_, l = sess.run([optimizer, loss])
86+
total_loss += l
87+
n_batches += 1
88+
except tf.errors.OutOfRangeError:
89+
pass
90+
print('Average loss epoch {0}: {1}'.format(i, total_loss/n_batches))
91+
print('Total time: {0} seconds'.format(time.time() - start_time))
92+
93+
# test the model
94+
sess.run(test_init) # drawing samples from test_data
95+
total_correct_preds = 0
96+
try:
97+
while True:
98+
accuracy_batch = sess.run(accuracy)
99+
total_correct_preds += accuracy_batch
100+
except tf.errors.OutOfRangeError:
101+
pass
102+
103+
print('Accuracy {0}'.format(total_correct_preds/n_test))
104+
writer.close()

examples/04_linreg_eager.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
""" Starter code for a simple regression example using eager execution.
2+
Created by Akshay Agrawal (akshayka@cs.stanford.edu)
3+
CS20: "TensorFlow for Deep Learning Research"
4+
cs20.stanford.edu
5+
Lecture 04
6+
"""
7+
import time
8+
9+
import tensorflow as tf
10+
import tensorflow.contrib.eager as tfe
11+
import matplotlib.pyplot as plt
12+
13+
import utils
14+
15+
DATA_FILE = 'data/birth_life_2010.txt'
16+
17+
# In order to use eager execution, `tfe.enable_eager_execution()` must be
18+
# called at the very beginning of a TensorFlow program.
19+
tfe.enable_eager_execution()
20+
21+
# Read the data into a dataset.
22+
data, n_samples = utils.read_birth_life_data(DATA_FILE)
23+
dataset = tf.data.Dataset.from_tensor_slices((data[:,0], data[:,1]))
24+
25+
# Create variables.
26+
w = tfe.Variable(0.0)
27+
b = tfe.Variable(0.0)
28+
29+
# Define the linear predictor.
30+
def prediction(x):
31+
return x * w + b
32+
33+
# Define loss functions of the form: L(y, y_predicted)
34+
def squared_loss(y, y_predicted):
35+
return (y - y_predicted) ** 2
36+
37+
def huber_loss(y, y_predicted, m=1.0):
38+
"""Huber loss."""
39+
t = y - y_predicted
40+
# Note that enabling eager execution lets you use Python control flow and
41+
# specificy dynamic TensorFlow computations. Contrast this implementation
42+
# to the graph-construction one found in `utils`, which uses `tf.cond`.
43+
return t ** 2 if tf.abs(t) <= m else m * (2 * tf.abs(t) - m)
44+
45+
def train(loss_fn):
46+
"""Train a regression model evaluated using `loss_fn`."""
47+
print('Training; loss function: ' + loss_fn.__name__)
48+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
49+
50+
# Define the function through which to differentiate.
51+
def loss_for_example(x, y):
52+
return loss_fn(y, prediction(x))
53+
54+
# `grad_fn(x_i, y_i)` returns (1) the value of `loss_for_example`
55+
# evaluated at `x_i`, `y_i` and (2) the gradients of any variables used in
56+
# calculating it.
57+
grad_fn = tfe.implicit_value_and_gradients(loss_for_example)
58+
59+
start = time.time()
60+
for epoch in range(100):
61+
total_loss = 0.0
62+
for x_i, y_i in tfe.Iterator(dataset):
63+
loss, gradients = grad_fn(x_i, y_i)
64+
# Take an optimization step and update variables.
65+
optimizer.apply_gradients(gradients)
66+
total_loss += loss
67+
if epoch % 10 == 0:
68+
print('Epoch {0}: {1}'.format(epoch, total_loss / n_samples))
69+
print('Took: %f seconds' % (time.time() - start))
70+
print('Eager execution exhibits significant overhead per operation. '
71+
'As you increase your batch size, the impact of the overhead will '
72+
'become less noticeable. Eager execution is under active development: '
73+
'expect performance to increase substantially in the near future!')
74+
75+
train(huber_loss)
76+
plt.plot(data[:,0], data[:,1], 'bo')
77+
# The `.numpy()` method of a tensor retrieves the NumPy array backing it.
78+
# In future versions of eager, you won't need to call `.numpy()` and will
79+
# instead be able to, in most cases, pass Tensors wherever NumPy arrays are
80+
# expected.
81+
plt.plot(data[:,0], data[:,0] * w.numpy() + b.numpy(), 'r',
82+
label="huber regression")
83+
plt.legend()
84+
plt.show()
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
""" Starter code for a simple regression example using eager execution.
2+
Created by Akshay Agrawal (akshayka@cs.stanford.edu)
3+
CS20: "TensorFlow for Deep Learning Research"
4+
cs20.stanford.edu
5+
Lecture 04
6+
"""
7+
import time
8+
9+
import tensorflow as tf
10+
import tensorflow.contrib.eager as tfe
11+
import matplotlib.pyplot as plt
12+
13+
import utils
14+
15+
DATA_FILE = 'data/birth_life_2010.txt'
16+
17+
# In order to use eager execution, `tfe.enable_eager_execution()` must be
18+
# called at the very beginning of a TensorFlow program.
19+
#############################
20+
########## TO DO ############
21+
#############################
22+
23+
# Read the data into a dataset.
24+
data, n_samples = utils.read_birth_life_data(DATA_FILE)
25+
dataset = tf.data.Dataset.from_tensor_slices((data[:,0], data[:,1]))
26+
27+
# Create weight and bias variables, initialized to 0.0.
28+
#############################
29+
########## TO DO ############
30+
#############################
31+
w = None
32+
b = None
33+
34+
# Define the linear predictor.
35+
def prediction(x):
36+
#############################
37+
########## TO DO ############
38+
#############################
39+
pass
40+
41+
# Define loss functions of the form: L(y, y_predicted)
42+
def squared_loss(y, y_predicted):
43+
#############################
44+
########## TO DO ############
45+
#############################
46+
pass
47+
48+
def huber_loss(y, y_predicted):
49+
"""Huber loss with `m` set to `1.0`."""
50+
#############################
51+
########## TO DO ############
52+
#############################
53+
pass
54+
55+
def train(loss_fn):
56+
"""Train a regression model evaluated using `loss_fn`."""
57+
print('Training; loss function: ' + loss_fn.__name__)
58+
optimizer = tf.train.GradientDescentOptimizer(learning_rate=0.01)
59+
60+
# Define the function through which to differentiate.
61+
#############################
62+
########## TO DO ############
63+
#############################
64+
def loss_for_example(x, y):
65+
pass
66+
67+
# Obtain a gradients function using `tfe.implicit_value_and_gradients`.
68+
#############################
69+
########## TO DO ############
70+
#############################
71+
grad_fn = None
72+
73+
start = time.time()
74+
for epoch in range(100):
75+
total_loss = 0.0
76+
for x_i, y_i in tfe.Iterator(dataset):
77+
# Compute the loss and gradient, and take an optimization step.
78+
#############################
79+
########## TO DO ############
80+
#############################
81+
optimizer.apply_gradients(gradients)
82+
total_loss += loss
83+
if epoch % 10 == 0:
84+
print('Epoch {0}: {1}'.format(epoch, total_loss / n_samples))
85+
print('Took: %f seconds' % (time.time() - start))
86+
print('Eager execution exhibits significant overhead per operation. '
87+
'As you increase your batch size, the impact of the overhead will '
88+
'become less noticeable. Eager execution is under active development: '
89+
'expect performance to increase substantially in the near future!')
90+
91+
train(huber_loss)
92+
plt.plot(data[:,0], data[:,1], 'bo')
93+
# The `.numpy()` method of a tensor retrieves the NumPy array backing it.
94+
# In future versions of eager, you won't need to call `.numpy()` and will
95+
# instead be able to, in most cases, pass Tensors wherever NumPy arrays are
96+
# expected.
97+
plt.plot(data[:,0], data[:,0] * w.numpy() + b.numpy(), 'r',
98+
label="huber regression")
99+
plt.legend()
100+
plt.show()

examples/04_word2vec.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
""" starter code for word2vec skip-gram model with NCE loss
2+
CS 20: "TensorFlow for Deep Learning Research"
3+
cs20.stanford.edu
4+
Chip Huyen (chiphuyen@cs.stanford.edu)
5+
Lecture 04
6+
"""
7+
8+
import os
9+
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
10+
11+
import numpy as np
12+
from tensorflow.contrib.tensorboard.plugins import projector
13+
import tensorflow as tf
14+
15+
import utils
16+
import word2vec_utils
17+
18+
# Model hyperparameters
19+
VOCAB_SIZE = 50000
20+
BATCH_SIZE = 128
21+
EMBED_SIZE = 128 # dimension of the word embedding vectors
22+
SKIP_WINDOW = 1 # the context window
23+
NUM_SAMPLED = 64 # number of negative examples to sample
24+
LEARNING_RATE = 1.0
25+
NUM_TRAIN_STEPS = 100000
26+
VISUAL_FLD = 'visualization'
27+
SKIP_STEP = 5000
28+
29+
# Parameters for downloading data
30+
DOWNLOAD_URL = 'http://mattmahoney.net/dc/text8.zip'
31+
EXPECTED_BYTES = 31344016
32+
NUM_VISUALIZE = 3000 # number of tokens to visualize
33+
34+
35+
def word2vec(dataset):
36+
""" Build the graph for word2vec model and train it """
37+
# Step 1: get input, output from the dataset
38+
with tf.name_scope('data'):
39+
iterator = dataset.make_initializable_iterator()
40+
center_words, target_words = iterator.get_next()
41+
42+
# Assemble this part of the graph on the CPU. You can change it to GPU if you have GPU
43+
# Step 2: define weights. In word2vec, it's actually the weights that we care about
44+
with tf.name_scope("embed"):
45+
embed_matrix = tf.get_variable('embed_matrix',
46+
shape=[VOCAB_SIZE, EMBED_SIZE],
47+
initializer=tf.random_uniform_initializer())
48+
49+
with tf.name_scope('loss'):
50+
# Step 3: define the inference
51+
embed = tf.nn.embedding_lookup(embed_matrix, center_words, name='embed')
52+
53+
# Step 4: define loss function
54+
# construct variables for NCE loss
55+
nce_weight = tf.get_variable('nce_weight', shape=[VOCAB_SIZE, EMBED_SIZE],
56+
initializer=tf.truncated_normal_initializer(stddev=1.0 / (EMBED_SIZE ** 0.5)))
57+
nce_bias = tf.get_variable('nce_bias', initializer=tf.zeros([VOCAB_SIZE]))
58+
59+
# define loss function to be NCE loss function
60+
loss = tf.reduce_mean(tf.nn.nce_loss(weights=nce_weight,
61+
biases=nce_bias,
62+
labels=target_words,
63+
inputs=embed,
64+
num_sampled=NUM_SAMPLED,
65+
num_classes=VOCAB_SIZE), name='loss')
66+
67+
# Step 5: define optimizer
68+
with tf.name_scope('optimizer'):
69+
optimizer = tf.train.GradientDescentOptimizer(LEARNING_RATE).minimize(loss)
70+
71+
utils.safe_mkdir('checkpoints')
72+
73+
with tf.Session() as sess:
74+
sess.run(iterator.initializer)
75+
sess.run(tf.global_variables_initializer())
76+
77+
total_loss = 0.0 # we use this to calculate late average loss in the last SKIP_STEP steps
78+
writer = tf.summary.FileWriter('graphs/word2vec_simple', sess.graph)
79+
80+
for index in range(NUM_TRAIN_STEPS):
81+
try:
82+
loss_batch, _ = sess.run([loss, optimizer])
83+
total_loss += loss_batch
84+
if (index + 1) % SKIP_STEP == 0:
85+
print('Average loss at step {}: {:5.1f}'.format(index, total_loss / SKIP_STEP))
86+
total_loss = 0.0
87+
except tf.errors.OutOfRangeError:
88+
sess.run(iterator.initializer)
89+
writer.close()
90+
91+
def gen():
92+
yield from word2vec_utils.batch_gen(DOWNLOAD_URL, EXPECTED_BYTES, VOCAB_SIZE,
93+
BATCH_SIZE, SKIP_WINDOW, VISUAL_FLD)
94+
95+
def main():
96+
dataset = tf.data.Dataset.from_generator(gen,
97+
(tf.int32, tf.int32),
98+
(tf.TensorShape([BATCH_SIZE]), tf.TensorShape([BATCH_SIZE, 1])))
99+
word2vec(dataset)
100+
101+
if __name__ == '__main__':
102+
main()

0 commit comments

Comments
 (0)