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

Commit e8dbcd6

Browse files
committed
update to tf 1.2 and python3
1 parent 51c01e5 commit e8dbcd6

58 files changed

Lines changed: 8750 additions & 245 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ This repository contains code examples for the course CS 20SI: TensorFlow for De
33
It will be updated as the class progresses. <br>
44
Detailed syllabus and lecture notes can be found here http://cs20si.stanford.edu
55

6+
# Note (as of July 11, 2017)
7+
I've updated the code to TensorFlow 1.2 and Python3, except the code for chatbot. I will update the code for chatbot soon.
8+
9+
610
## Models include: <br>
711
### In the folder "examples": <br>
812
Linear Regression with Chicago's Fire-Theft dataset<br>

assignments/chatbot/chatbot.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class CS 20SI: "TensorFlow for Deep Learning Research"
1919

2020
import argparse
2121
import os
22+
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
2223
import random
2324
import sys
2425
import time
@@ -33,7 +34,7 @@ class CS 20SI: "TensorFlow for Deep Learning Research"
3334
def _get_random_bucket(train_buckets_scale):
3435
""" Get a random bucket from which to choose a training sample """
3536
rand = random.random()
36-
return min([i for i in xrange(len(train_buckets_scale))
37+
return min([i for i in range(len(train_buckets_scale))
3738
if train_buckets_scale[i] > rand])
3839

3940
def _assert_lengths(encoder_size, decoder_size, encoder_inputs, decoder_inputs, decoder_masks):
@@ -59,9 +60,9 @@ def run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_
5960

6061
# input feed: encoder inputs, decoder inputs, target_weights, as provided.
6162
input_feed = {}
62-
for step in xrange(encoder_size):
63+
for step in range(encoder_size):
6364
input_feed[model.encoder_inputs[step].name] = encoder_inputs[step]
64-
for step in xrange(decoder_size):
65+
for step in range(decoder_size):
6566
input_feed[model.decoder_inputs[step].name] = decoder_inputs[step]
6667
input_feed[model.decoder_masks[step].name] = decoder_masks[step]
6768

@@ -75,7 +76,7 @@ def run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_
7576
model.losses[bucket_id]] # loss for this batch.
7677
else:
7778
output_feed = [model.losses[bucket_id]] # loss for this batch.
78-
for step in xrange(decoder_size): # output logits.
79+
for step in range(decoder_size): # output logits.
7980
output_feed.append(model.outputs[bucket_id][step])
8081

8182
outputs = sess.run(output_feed, input_feed)
@@ -91,12 +92,12 @@ def _get_buckets():
9192
"""
9293
test_buckets = data.load_data('test_ids.enc', 'test_ids.dec')
9394
data_buckets = data.load_data('train_ids.enc', 'train_ids.dec')
94-
train_bucket_sizes = [len(data_buckets[b]) for b in xrange(len(config.BUCKETS))]
95+
train_bucket_sizes = [len(data_buckets[b]) for b in range(len(config.BUCKETS))]
9596
print("Number of samples in each bucket:\n", train_bucket_sizes)
9697
train_total_size = sum(train_bucket_sizes)
9798
# list of increasing numbers from 0 to 1 that we'll use to select a bucket.
9899
train_buckets_scale = [sum(train_bucket_sizes[:i + 1]) / train_total_size
99-
for i in xrange(len(train_bucket_sizes))]
100+
for i in range(len(train_bucket_sizes))]
100101
print("Bucket scale:\n", train_buckets_scale)
101102
return test_buckets, data_buckets, train_buckets_scale
102103

@@ -117,7 +118,7 @@ def _check_restore_parameters(sess, saver):
117118

118119
def _eval_test_set(sess, model, test_buckets):
119120
""" Evaluate on the test set. """
120-
for bucket_id in xrange(len(config.BUCKETS)):
121+
for bucket_id in range(len(config.BUCKETS)):
121122
if len(test_buckets[bucket_id]) == 0:
122123
print(" Test: empty bucket %d" % (bucket_id))
123124
continue
@@ -175,7 +176,7 @@ def _get_user_input():
175176

176177
def _find_right_bucket(length):
177178
""" Find the proper bucket for an encoder input based on its length """
178-
return min([b for b in xrange(len(config.BUCKETS))
179+
return min([b for b in range(len(config.BUCKETS))
179180
if config.BUCKETS[b][0] >= length])
180181

181182
def _construct_response(output_logits, inv_dec_vocab):

assignments/chatbot/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class CS 20SI: "TensorFlow for Deep Learning Research"
4747
# [37049, 33519, 30223, 33513, 37371]
4848
# BUCKETS = [(8, 10), (12, 14), (16, 19), (23, 26), (39, 43)]
4949

50-
BUCKETS = [(8, 10), (12, 14), (16, 19)]
50+
# BUCKETS = [(8, 10), (12, 14), (16, 19)]
51+
BUCKETS = [(16, 19)]
5152

5253
NUM_LAYERS = 3
5354
HIDDEN_SIZE = 256

assignments/chatbot/data.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ class CS 20SI: "TensorFlow for Deep Learning Research"
1717
"""
1818
from __future__ import print_function
1919

20+
import os
2021
import random
2122
import re
22-
import os
2323

2424
import numpy as np
2525

@@ -215,9 +215,9 @@ def _reshape_batch(inputs, size, batch_size):
215215
""" Create batch-major inputs. Batch inputs are just re-indexed inputs
216216
"""
217217
batch_inputs = []
218-
for length_id in xrange(size):
218+
for length_id in range(size):
219219
batch_inputs.append(np.array([inputs[batch_id][length_id]
220-
for batch_id in xrange(batch_size)], dtype=np.int32))
220+
for batch_id in range(batch_size)], dtype=np.int32))
221221
return batch_inputs
222222

223223

@@ -227,7 +227,7 @@ def get_batch(data_bucket, bucket_id, batch_size=1):
227227
encoder_size, decoder_size = config.BUCKETS[bucket_id]
228228
encoder_inputs, decoder_inputs = [], []
229229

230-
for _ in xrange(batch_size):
230+
for _ in range(batch_size):
231231
encoder_input, decoder_input = random.choice(data_bucket)
232232
# pad both encoder and decoder, reverse the encoder
233233
encoder_inputs.append(list(reversed(_pad_input(encoder_input, encoder_size))))
@@ -239,9 +239,9 @@ def get_batch(data_bucket, bucket_id, batch_size=1):
239239

240240
# create decoder_masks to be 0 for decoders that are padding.
241241
batch_masks = []
242-
for length_id in xrange(decoder_size):
242+
for length_id in range(decoder_size):
243243
batch_mask = np.ones(batch_size, dtype=np.float32)
244-
for batch_id in xrange(batch_size):
244+
for batch_id in range(batch_size):
245245
# we set mask to 0 if the corresponding target is a PAD symbol.
246246
# the corresponding decoder is decoder_input shifted by 1 forward.
247247
if length_id < decoder_size - 1:

assignments/chatbot/model.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ def _create_placeholders(self):
3535
# Feeds for inputs. It's a list of placeholders
3636
print('Create placeholders')
3737
self.encoder_inputs = [tf.placeholder(tf.int32, shape=[None], name='encoder{}'.format(i))
38-
for i in xrange(config.BUCKETS[-1][0])]
38+
for i in range(config.BUCKETS[-1][0])]
3939
self.decoder_inputs = [tf.placeholder(tf.int32, shape=[None], name='decoder{}'.format(i))
40-
for i in xrange(config.BUCKETS[-1][1] + 1)]
40+
for i in range(config.BUCKETS[-1][1] + 1)]
4141
self.decoder_masks = [tf.placeholder(tf.float32, shape=[None], name='mask{}'.format(i))
42-
for i in xrange(config.BUCKETS[-1][1] + 1)]
42+
for i in range(config.BUCKETS[-1][1] + 1)]
4343

4444
# Our targets are decoder inputs shifted by one (to ignore <s> symbol)
4545
self.targets = self.decoder_inputs[1:]
@@ -85,7 +85,7 @@ def _seq2seq_f(encoder_inputs, decoder_inputs, do_decode):
8585
softmax_loss_function=self.softmax_loss_function)
8686
# If we use output projection, we need to project outputs for decoding.
8787
if self.output_projection:
88-
for bucket in xrange(len(config.BUCKETS)):
88+
for bucket in range(len(config.BUCKETS)):
8989
self.outputs[bucket] = [tf.matmul(output,
9090
self.output_projection[0]) + self.output_projection[1]
9191
for output in self.outputs[bucket]]
@@ -111,7 +111,7 @@ def _creat_optimizer(self):
111111
self.gradient_norms = []
112112
self.train_ops = []
113113
start = time.time()
114-
for bucket in xrange(len(config.BUCKETS)):
114+
for bucket in range(len(config.BUCKETS)):
115115

116116
clipped_grads, norm = tf.clip_by_global_norm(tf.gradients(self.losses[bucket],
117117
trainables),
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
x = tf.random_uniform([]) # Empty array as shape creates a scalar.
1616
y = tf.random_uniform([])
17-
out = tf.cond(tf.less(x, y), lambda: tf.add(x, y), lambda: tf.sub(x, y))
17+
out = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.subtract(x, y))
1818

1919
###############################################################################
2020
# 1b: Create two 0-d tensors x and y randomly selected from -1 and 1.
@@ -75,7 +75,7 @@
7575
###############################################################################
7676
# 1h: Create two tensors x and y of shape 300 from any normal distribution,
7777
# as long as they are from the same distribution.
78-
# Use tf.less() and tf.select() to return:
78+
# Use tf.cond() to return:
7979
# - The mean squared error of (x - y) if the average of all elements in (x - y)
8080
# is negative, or
8181
# - The sum of absolute value of all elements in the tensor (x - y) otherwise.
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Solution to simple TensorFlow exercises
33
For the problems
44
"""
5-
65
import tensorflow as tf
76

87
###############################################################################
@@ -14,7 +13,7 @@
1413

1514
x = tf.random_uniform([]) # Empty array as shape creates a scalar.
1615
y = tf.random_uniform([])
17-
out = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.sub(x, y))
16+
out = tf.cond(tf.greater(x, y), lambda: tf.add(x, y), lambda: tf.subtract(x, y))
1817

1918
###############################################################################
2019
# 1b: Create two 0-d tensors x and y randomly selected from -1 and 1.
@@ -25,8 +24,11 @@
2524
x = tf.random_uniform([], -1, 1, dtype=tf.float32)
2625
y = tf.random_uniform([], -1, 1, dtype=tf.float32)
2726
out = tf.case({tf.less(x, y): lambda: tf.add(x, y),
28-
tf.greater(x, y): lambda: tf.sub(x, y)},
27+
tf.greater(x, y): lambda: tf.subtract(x, y)},
2928
default=lambda: tf.constant(0.0), exclusive=True)
29+
print(x)
30+
sess = tf.InteractiveSession()
31+
print(sess.run(x))
3032

3133
###############################################################################
3234
# 1c: Create the tensor x of the value [[0, -2, -1], [0, 1, 2]]
@@ -90,7 +92,7 @@
9092
###############################################################################
9193
# 1h: Create two tensors x and y of shape 300 from any normal distribution,
9294
# as long as they are from the same distribution.
93-
# Use tf.less() and tf.select() to return:
95+
# Use tf.cond() to return:
9496
# - The mean squared error of (x - y) if the average of all elements in (x - y)
9597
# is negative, or
9698
# - The sum of absolute value of all elements in the tensor (x - y) otherwise.
@@ -100,7 +102,6 @@
100102
x = tf.random_normal([300], mean=5, stddev=1)
101103
y = tf.random_normal([300], mean=5, stddev=1)
102104
average = tf.reduce_mean(x - y)
103-
condition = tf.less(average, 0)
104-
left_op = tf.reduce_mean(tf.square(x - y))
105-
right_op = tf.reduce_sum(tf.abs(x - y))
106-
out = tf.select(condition, left_op, right_op)
105+
def f1(): return tf.reduce_mean(tf.square(x - y))
106+
def f2(): return tf.reduce_sum(tf.abs(x - y))
107+
out = tf.cond(average < 0, f1, f2)
23.6 KB
Loading

assignments/style_transfer/style_transfer.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from __future__ import print_function
1010

1111
import os
12+
os.environ['TF_CPP_MIN_LOG_LEVEL']='2'
1213
import time
1314

1415
import numpy as np
@@ -26,6 +27,9 @@
2627
IMAGE_WIDTH = 333
2728
NOISE_RATIO = 0.6 # percentage of weight of the noise for intermixing with the content image
2829

30+
CONTENT_WEIGHT = 0.01
31+
STYLE_WEIGHT = 1
32+
2933
# Layers used for style features. You can change this.
3034
STYLE_LAYERS = ['conv1_1', 'conv2_1', 'conv3_1', 'conv4_1', 'conv5_1']
3135
W = [0.5, 1.0, 1.5, 3.0, 4.0] # give more weights to deeper layers.
@@ -62,13 +66,14 @@ def _create_content_loss(p, f):
6266
the content loss
6367
6468
"""
65-
pass
69+
return tf.reduce_sum((f - p) ** 2) / (4.0 * p.size)
6670

6771
def _gram_matrix(F, N, M):
6872
""" Create and return the gram matrix for tensor F
6973
Hint: you'll first have to reshape F
7074
"""
71-
pass
75+
F = tf.reshape(F, (M, N))
76+
return tf.matmul(tf.transpose(F), F)
7277

7378
def _single_style_loss(a, g):
7479
""" Calculate the style loss at a certain layer
@@ -82,7 +87,11 @@ def _single_style_loss(a, g):
8287
2. we'll use the same coefficient for style loss as in the paper
8388
3. a and g are feature representation, not gram matrices
8489
"""
85-
pass
90+
N = a.shape[3] # number of filters
91+
M = a.shape[1] * a.shape[2] # height times width of the feature map
92+
A = _gram_matrix(a, N, M)
93+
G = _gram_matrix(g, N, M)
94+
return tf.reduce_sum((G - A) ** 2 / ((2 * N * M) ** 2))
8695

8796
def _create_style_loss(A, model):
8897
""" Return the total style loss
@@ -92,7 +101,7 @@ def _create_style_loss(A, model):
92101

93102
###############################
94103
## TO DO: return total style loss
95-
pass
104+
return sum([W[i] * E[i] for i in range(n_layers)])
96105
###############################
97106

98107
def _create_losses(model, input_image, content_image, style_image):
@@ -110,7 +119,7 @@ def _create_losses(model, input_image, content_image, style_image):
110119
##########################################
111120
## TO DO: create total loss.
112121
## Hint: don't forget the content loss and style loss weights
113-
122+
total_loss = CONTENT_WEIGHT * content_loss + STYLE_WEIGHT * style_loss
114123
##########################################
115124

116125
return content_loss, style_loss, total_loss
@@ -119,7 +128,14 @@ def _create_summary(model):
119128
""" Create summary ops necessary
120129
Hint: don't forget to merge them
121130
"""
122-
pass
131+
with tf.name_scope('summaries'):
132+
tf.summary.scalar('content loss', model['content_loss'])
133+
tf.summary.scalar('style loss', model['style_loss'])
134+
tf.summary.scalar('total loss', model['total_loss'])
135+
tf.summary.histogram('histogram content loss', model['content_loss'])
136+
tf.summary.histogram('histogram style loss', model['style_loss'])
137+
tf.summary.histogram('histogram total loss', model['total_loss'])
138+
return tf.summary.merge_all()
123139

124140
def train(model, generated_image, initial_image):
125141
""" Train your model.
@@ -132,6 +148,9 @@ def train(model, generated_image, initial_image):
132148
## TO DO:
133149
## 1. initialize your variables
134150
## 2. create writer to write your graph
151+
saver = tf.train.Saver()
152+
sess.run(tf.global_variables_initializer())
153+
writer = tf.summary.FileWriter('graphs', sess.graph)
135154
###############################
136155
sess.run(generated_image.assign(initial_image))
137156
ckpt = tf.train.get_checkpoint_state(os.path.dirname('checkpoints/checkpoint'))
@@ -150,6 +169,8 @@ def train(model, generated_image, initial_image):
150169
if (index + 1) % skip_step == 0:
151170
###############################
152171
## TO DO: obtain generated image and loss
172+
gen_image, total_loss, summary = sess.run([generated_image, model['total_loss'],
173+
model['summary_op']])
153174

154175
###############################
155176
gen_image = gen_image + MEAN_PIXELS
@@ -172,9 +193,11 @@ def main():
172193
input_image = tf.Variable(np.zeros([1, IMAGE_HEIGHT, IMAGE_WIDTH, 3]), dtype=tf.float32)
173194

174195
utils.download(VGG_DOWNLOAD_LINK, VGG_MODEL, EXPECTED_BYTES)
196+
utils.make_dir('checkpoints')
197+
utils.make_dir('outputs')
175198
model = vgg_model.load_vgg(VGG_MODEL, input_image)
176199
model['global_step'] = tf.Variable(0, dtype=tf.int32, trainable=False, name='global_step')
177-
200+
178201
content_image = utils.get_resized_image(CONTENT_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH)
179202
content_image = content_image - MEAN_PIXELS
180203
style_image = utils.get_resized_image(STYLE_IMAGE, IMAGE_HEIGHT, IMAGE_WIDTH)
@@ -184,7 +207,8 @@ def main():
184207
input_image, content_image, style_image)
185208
###############################
186209
## TO DO: create optimizer
187-
## model['optimizer'] = ...
210+
model['optimizer'] = tf.train.AdamOptimizer(LR).minimize(model['total_loss'],
211+
global_step=model['global_step'])
188212
###############################
189213
model['summary_op'] = _create_summary(model)
190214

22.9 KB
Loading

0 commit comments

Comments
 (0)