Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gan_cifar10.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ def forward(self, input):

netG = Generator()
netD = Discriminator()
print netG
print netD
print(netG)
print(netD)

use_cuda = torch.cuda.is_available()
if use_cuda:
Expand Down Expand Up @@ -152,7 +152,7 @@ def generate_image(frame, netG):
# For calculating inception score
def get_inception_score(G, ):
all_samples = []
for i in xrange(10):
for i in range(10):
samples_100 = torch.randn(100, 128)
if use_cuda:
samples_100 = samples_100.cuda(gpu)
Expand All @@ -177,14 +177,14 @@ def inf_train_gen():
torchvision.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)),
])

for iteration in xrange(ITERS):
for iteration in range(ITERS):
start_time = time.time()
############################
# (1) Update D network
###########################
for p in netD.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update
for i in xrange(CRITIC_ITERS):
for i in range(CRITIC_ITERS):
_data = gen.next()
netD.zero_grad()

Expand Down
28 changes: 14 additions & 14 deletions gan_language.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ def forward(self, input):
def inf_train_gen():
while True:
np.random.shuffle(lines)
for i in xrange(0, len(lines)-BATCH_SIZE+1, BATCH_SIZE):
for i in range(0, len(lines)-BATCH_SIZE+1, BATCH_SIZE):
yield np.array(
[[charmap[c] for c in l] for l in lines[i:i+BATCH_SIZE]],
dtype='int32'
Expand Down Expand Up @@ -172,9 +172,9 @@ def generate_samples(netG):

samples = np.argmax(samples, axis=2)
decoded_samples = []
for i in xrange(len(samples)):
for i in range(len(samples)):
decoded = []
for j in xrange(len(samples[i])):
for j in range(len(samples[i])):
decoded.append(inv_charmap[samples[i][j]])
decoded_samples.append(tuple(decoded))
return decoded_samples
Expand All @@ -183,8 +183,8 @@ def generate_samples(netG):

netG = Generator()
netD = Discriminator()
print netG
print netD
print(netG)
print(netD)

if use_cuda:
netD = netD.cuda(gpu)
Expand All @@ -204,21 +204,21 @@ def generate_samples(netG):
# During training we monitor JS divergence between the true & generated ngram
# distributions for n=1,2,3,4. To get an idea of the optimal values, we
# evaluate these statistics on a held-out set first.
true_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines[10*BATCH_SIZE:], tokenize=False) for i in xrange(4)]
validation_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines[:10*BATCH_SIZE], tokenize=False) for i in xrange(4)]
for i in xrange(4):
print "validation set JSD for n={}: {}".format(i+1, true_char_ngram_lms[i].js_with(validation_char_ngram_lms[i]))
true_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines, tokenize=False) for i in xrange(4)]
true_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines[10*BATCH_SIZE:], tokenize=False) for i in range(4)]
validation_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines[:10*BATCH_SIZE], tokenize=False) for i in range(4)]
for i in range(4):
print("validation set JSD for n={}: {}").format(i+1, true_char_ngram_lms[i].js_with(validation_char_ngram_lms[i]))
true_char_ngram_lms = [language_helpers.NgramLanguageModel(i+1, lines, tokenize=False) for i in range(4)]

for iteration in xrange(ITERS):
for iteration in range(ITERS):
start_time = time.time()
############################
# (1) Update D network
###########################
for p in netD.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update

for iter_d in xrange(CRITIC_ITERS):
for iter_d in range(CRITIC_ITERS):
_data = data.next()
data_one_hot = one_hot.transform(_data.reshape(-1, 1)).toarray().reshape(BATCH_SIZE, -1, len(charmap))
#print data_one_hot.shape
Expand Down Expand Up @@ -282,10 +282,10 @@ def generate_samples(netG):

if iteration % 100 == 99:
samples = []
for i in xrange(10):
for i in range(10):
samples.extend(generate_samples(netG))

for i in xrange(4):
for i in range(4):
lm = language_helpers.NgramLanguageModel(i+1, samples, tokenize=False)
lib.plot.plot('tmp/lang/js{}'.format(i+1), lm.js_with(true_char_ngram_lms[i]))

Expand Down
8 changes: 4 additions & 4 deletions gan_mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ def calc_gradient_penalty(netD, real_data, fake_data):

netG = Generator()
netD = Discriminator()
print netG
print netD
print(netG)
print(netD)

if use_cuda:
netD = netD.cuda(gpu)
Expand All @@ -170,15 +170,15 @@ def calc_gradient_penalty(netD, real_data, fake_data):

data = inf_train_gen()

for iteration in xrange(ITERS):
for iteration in range(ITERS):
start_time = time.time()
############################
# (1) Update D network
###########################
for p in netD.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update

for iter_d in xrange(CRITIC_ITERS):
for iter_d in range(CRITIC_ITERS):
_data = data.next()
real_data = torch.Tensor(_data)
if use_cuda:
Expand Down
18 changes: 9 additions & 9 deletions gan_toy.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ def inf_train_gen():
if DATASET == '25gaussians':

dataset = []
for i in xrange(100000 / 25):
for x in xrange(-2, 3):
for y in xrange(-2, 3):
for i in range(100000 / 25):
for x in range(-2, 3):
for y in range(-2, 3):
point = np.random.randn(2) * 0.05
point[0] += 2 * x
point[1] += 2 * y
Expand All @@ -147,7 +147,7 @@ def inf_train_gen():
np.random.shuffle(dataset)
dataset /= 2.828 # stdev
while True:
for i in xrange(len(dataset) / BATCH_SIZE):
for i in range(len(dataset) / BATCH_SIZE):
yield dataset[i * BATCH_SIZE:(i + 1) * BATCH_SIZE]

elif DATASET == 'swissroll':
Expand Down Expand Up @@ -177,7 +177,7 @@ def inf_train_gen():
centers = [(scale * x, scale * y) for x, y in centers]
while True:
dataset = []
for i in xrange(BATCH_SIZE):
for i in range(BATCH_SIZE):
point = np.random.randn(2) * .02
center = random.choice(centers)
point[0] += center[0]
Expand Down Expand Up @@ -215,8 +215,8 @@ def calc_gradient_penalty(netD, real_data, fake_data):
netD = Discriminator()
netD.apply(weights_init)
netG.apply(weights_init)
print netG
print netD
print(netG)
print(netD)

if use_cuda:
netD = netD.cuda()
Expand All @@ -233,14 +233,14 @@ def calc_gradient_penalty(netD, real_data, fake_data):

data = inf_train_gen()

for iteration in xrange(ITERS):
for iteration in range(ITERS):
############################
# (1) Update D network
###########################
for p in netD.parameters(): # reset requires_grad
p.requires_grad = True # they are set to False below in netG update

for iter_d in xrange(CRITIC_ITERS):
for iter_d in range(CRITIC_ITERS):
_data = data.next()
real_data = torch.Tensor(_data)
if use_cuda:
Expand Down
12 changes: 6 additions & 6 deletions language_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def __init__(self, n, samples, tokenize=False):
def ngrams(self):
n = self._n
for sample in self._samples:
for i in xrange(len(sample)-n+1):
for i in range(len(sample)-n+1):
yield sample[i:i+n]

def unique_ngrams(self):
Expand Down Expand Up @@ -86,13 +86,13 @@ def js_with(self, p):
return 0.5*(kl_p_m + kl_q_m) / np.log(2)

def load_dataset(max_length, max_n_examples, tokenize=False, max_vocab_size=2048, data_dir='/home/ishaan/data/1-billion-word-language-modeling-benchmark-r13output'):
print "loading dataset..."
print("loading dataset...")

lines = []

finished = False

for i in xrange(99):
for i in range(99):
path = data_dir+("/training-monolingual.tokenized.shuffled/news.en-{}-of-00100".format(str(i+1).zfill(5)))
with open(path, 'r') as f:
for line in f:
Expand Down Expand Up @@ -136,8 +136,8 @@ def load_dataset(max_length, max_n_examples, tokenize=False, max_vocab_size=2048
filtered_line.append('unk')
filtered_lines.append(tuple(filtered_line))

for i in xrange(100):
print filtered_lines[i]
for i in range(100):
print(filtered_lines[i])

print "loaded {} lines in dataset".format(len(lines))
print("loaded {} lines in dataset").format(len(lines))
return filtered_lines, charmap, inv_charmap
8 changes: 4 additions & 4 deletions tflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,16 +99,16 @@ def delete_param_aliases():
# )

def print_model_settings(locals_):
print "Uppercase local vars:"
print("Uppercase local vars:")
all_vars = [(k,v) for (k,v) in locals_.items() if (k.isupper() and k!='T' and k!='SETTINGS' and k!='ALL_SETTINGS')]
all_vars = sorted(all_vars, key=lambda x: x[0])
for var_name, var_value in all_vars:
print "\t{}: {}".format(var_name, var_value)
print("\t{}: {}").format(var_name, var_value)


def print_model_settings_dict(settings):
print "Settings dict:"
print("Settings dict:")
all_vars = [(k,v) for (k,v) in settings.items()]
all_vars = sorted(all_vars, key=lambda x: x[0])
for var_name, var_value in all_vars:
print "\t{}: {}".format(var_name, var_value)
print("\t{}: {}").format(var_name, var_value)