From ea56bd2fcd4a6eea506c7a05ad37d4cf0f440cb2 Mon Sep 17 00:00:00 2001 From: Haoran Wang <792711822@qq.com> Date: Tue, 18 Jul 2023 15:37:13 +0800 Subject: [PATCH 1/3] debug --- gan_language.py | 28 ++++++++++++++-------------- tflib/__init__.py | 8 ++++---- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/gan_language.py b/gan_language.py index 5d92013..dcbfad8 100644 --- a/gan_language.py +++ b/gan_language.py @@ -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' @@ -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 @@ -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) @@ -204,13 +204,13 @@ 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 @@ -218,7 +218,7 @@ def generate_samples(netG): 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 @@ -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])) diff --git a/tflib/__init__.py b/tflib/__init__.py index 97f57e8..c8e6ab1 100644 --- a/tflib/__init__.py +++ b/tflib/__init__.py @@ -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) From e0bfe5adff4fe0333b10504c943998fd13e8490b Mon Sep 17 00:00:00 2001 From: Haoran Wang <792711822@qq.com> Date: Tue, 18 Jul 2023 15:49:45 +0800 Subject: [PATCH 2/3] debug2 --- gan_cifar10.py | 10 +++++----- language_helpers.py | 12 ++++++------ 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/gan_cifar10.py b/gan_cifar10.py index cfb8f61..7458f3b 100644 --- a/gan_cifar10.py +++ b/gan_cifar10.py @@ -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: @@ -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) @@ -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() diff --git a/language_helpers.py b/language_helpers.py index f8dd99d..b000b7a 100644 --- a/language_helpers.py +++ b/language_helpers.py @@ -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): @@ -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: @@ -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 From 081b0f3d43117195481e21c2acae25dd8cf04773 Mon Sep 17 00:00:00 2001 From: Haoran Wang <792711822@qq.com> Date: Tue, 18 Jul 2023 15:54:05 +0800 Subject: [PATCH 3/3] save --- gan_mnist.py | 8 ++++---- gan_toy.py | 18 +++++++++--------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/gan_mnist.py b/gan_mnist.py index b35857e..d2c07d3 100644 --- a/gan_mnist.py +++ b/gan_mnist.py @@ -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) @@ -170,7 +170,7 @@ 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 @@ -178,7 +178,7 @@ def calc_gradient_penalty(netD, real_data, fake_data): 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: diff --git a/gan_toy.py b/gan_toy.py index 0a2dc64..49e0ef5 100644 --- a/gan_toy.py +++ b/gan_toy.py @@ -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 @@ -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': @@ -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] @@ -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() @@ -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: