def main(args): # load checkpoint if not os.path.exists(args.load_checkpoint): raise FileNotFoundError(args.load_checkpoint) saved_dir_name = args.load_checkpoint.split('/')[2] params_path = './saved_vae_models/' + saved_dir_name + '/model_params.json' if not os.path.exists(params_path): raise FileNotFoundError(params_path) # load params params = load_model_params_from_checkpoint(params_path) # create and load model model = SentenceVae(**params) print(model) model.load_state_dict(torch.load(args.load_checkpoint)) print("Model loaded from %s" % args.load_checkpoint) if torch.cuda.is_available(): model = model.cuda() model.eval() # load the vocab of the chosen dataset if (model.dataset == 'yelp'): print("Yelp dataset used!") with open(args.data_dir + '/yelp/yelp.vocab.json', 'r') as file: vocab = json.load(file) w2i, i2w = vocab['w2i'], vocab['i2w'] samples, z = model.inference(n=args.num_samples) print('----------SAMPLES----------') print(*idx2word(samples, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n') z1 = torch.randn([params['latent_size']]).numpy() z2 = torch.randn([params['latent_size']]).numpy() z = to_var( torch.from_numpy(interpolate(start=z1, end=z2, steps=8)).float()) samples, _ = model.inference(z=z) print('-------INTERPOLATION-------') print(*idx2word(samples, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n')
def main(args): with open(args.data_dir + '/snli_yelp/snli_yelp.vocab.json', 'r') as file: vocab = json.load(file) w2i, i2w = vocab['w2i'], vocab['i2w'] if not os.path.exists(args.load_checkpoint): raise FileNotFoundError(args.load_checkpoint) if not os.path.exists(args.load_params): raise FileNotFoundError(args.load_params) # load params params = load_model_params_from_checkpoint(args.load_params) # create model model = SentenceVaeMultiTask(**params) print(model) model.load_state_dict(torch.load(args.load_checkpoint)) print("Model loaded from %s" % args.load_checkpoint) if torch.cuda.is_available(): model = model.cuda() model.eval() # print(params['latent_size']) # exit() samples, z = model.inference(n=args.num_samples) print('----------SAMPLES----------') print(*idx2word(samples, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n') z1 = torch.randn([params['latent_size']]).numpy() z2 = torch.randn([params['latent_size']]).numpy() z = to_var( torch.from_numpy(interpolate(start=z1, end=z2, steps=8)).float()) samples, _ = model.inference(z=z) print('-------INTERPOLATION-------') print(*idx2word(samples, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n')
def main(args): if not os.path.exists(args.load_checkpoint): raise FileNotFoundError(args.load_checkpoint) saved_dir_name = args.load_checkpoint.split('/')[2] params_path = './saved_vae_models/' + saved_dir_name + '/model_params.json' if not os.path.exists(params_path): raise FileNotFoundError(params_path) # load params params = load_model_params_from_checkpoint(params_path) # create model model = SentenceVae(**params) print(model) model.load_state_dict(torch.load(args.load_checkpoint)) print("Model loaded from %s" % args.load_checkpoint) if torch.cuda.is_available(): model = model.cuda() model.eval() datasets = OrderedDict() style_tsne_values = np.empty((0, model.style_space_size), int) content_tsne_values = np.empty((0, model.content_space_size), int) if (model.dataset == "yelp"): output_size = 2 print("Using Yelp!") tsne_labels = np.empty((0, output_size), int) dataset = Yelp splits = ['train'] for split in splits: print("creating dataset for: {}".format(split)) datasets[split] = dataset(split=split, ) for split in splits: # create dataloader data_loader = DataLoader(dataset=datasets[split], batch_size=args.batch_size, shuffle=split == 'train', num_workers=cpu_count(), pin_memory=torch.cuda.is_available()) for iteration, batch in enumerate(data_loader): # get batch size batch_size = batch['input'].size(0) for k, v in batch.items(): if torch.is_tensor(v): batch[k] = to_var(v) style_z, content_z = model.get_style_content_space(batch['input']) batch_labels = batch['label'].cpu().detach().numpy() style_z = style_z.cpu().detach().numpy() content_z = content_z.cpu().detach().numpy() style_tsne_values = np.append(style_tsne_values, style_z, axis=0) content_tsne_values = np.append(content_tsne_values, content_z, axis=0) tsne_labels = np.append(tsne_labels, batch_labels, axis=0) if iteration == 1000: break pca = PCA(n_components=8) tsne = TSNE(n_components=2, verbose=1) style_pca_result = pca.fit_transform(style_tsne_values) content_pca_result = pca.fit_transform(content_tsne_values) style_tsne_results = tsne.fit_transform(style_pca_result[:]) content_tsne_results = tsne.fit_transform(content_pca_result[:]) #plot style color_map = np.argmax(tsne_labels, axis=1) plt.figure(figsize=(10, 10)) for cl in range(output_size): indices = np.where(color_map == cl) indices = indices[0] plt.scatter(style_tsne_results[indices, 0], style_tsne_results[indices, 1], label=cl) plt.legend() plt.savefig('./experiments/' + model.dataset + '-style-tsne.png') #plot content color_map = np.argmax(tsne_labels, axis=1) plt.figure(figsize=(10, 10)) for cl in range(output_size): indices = np.where(color_map == cl) indices = indices[0] plt.scatter(content_tsne_results[indices, 0], content_tsne_results[indices, 1], label=cl) plt.legend() plt.savefig('./experiments/' + model.dataset + '-content-tsne.png')
def main(args): # load params params = load_model_params_from_checkpoint(args.load_params) # create model model = SentenceVaeYelp(**params) print(model) model.load_state_dict(torch.load(args.load_checkpoint)) print("Model loaded from %s" % args.load_checkpoint) # splits = ['train', 'test'] splits = ['train'] if torch.cuda.is_available(): model = model.cuda() model.eval() datasets = OrderedDict() tsne_values = np.empty((0, 256), int) tsne_labels = np.empty((0, 2), int) for split in splits: print("creating dataset for: {}".format(split)) datasets[split] = Yelpd(split=split, create_data=args.create_data, min_occ=args.min_occ) for split in splits: # create dataloader data_loader = DataLoader(dataset=datasets[split], batch_size=args.batch_size, shuffle=split == 'train', num_workers=cpu_count(), pin_memory=torch.cuda.is_available()) for iteration, batch in enumerate(data_loader): # get batch size batch_size = batch['input'].size(0) for k, v in batch.items(): if torch.is_tensor(v): batch[k] = to_var(v) hidden_emb = model.tsne_plot(batch['input']) batch_labels = batch['label'].cpu().detach().numpy() hidden_emb = hidden_emb.squeeze().cpu().detach().numpy() tsne_values = np.append(tsne_values, hidden_emb, axis=0) tsne_labels = np.append(tsne_labels, batch_labels, axis=0) if iteration == 3: break pca = PCA(n_components=20) pca_result = pca.fit_transform(tsne_values) tsne = TSNE(n_components=2, verbose=1) tsne_results = tsne.fit_transform(pca_result[:]) color_map = np.argmax(tsne_labels, axis=1) plt.figure(figsize=(10, 10)) for cl in range(2): indices = np.where(color_map == cl) indices = indices[0] plt.scatter(tsne_results[indices, 0], tsne_results[indices, 1], label=cl) plt.legend() plt.savefig('yelp.png')
def main(args): with open(args.data_dir + '/ptb/ptb.vocab.json', 'r') as file: vocab = json.load(file) w2i, i2w = vocab['w2i'], vocab['i2w'] # load params params = load_model_params_from_checkpoint(args.load_params) # create model model = SentenceVAE(**params) print(model) model.load_state_dict(torch.load(args.load_checkpoint)) print("Model loaded from %s" % args.load_checkpoint) # splits = ['train', 'test'] splits = ['test'] if torch.cuda.is_available(): model = model.cuda() model.eval() datasets = OrderedDict() tsne_values = np.empty((0, 256), int) tsne_labels = np.empty((0, 2), int) for split in splits: print("creating dataset for: {}".format(split)) datasets[split] = PTB(split=split, create_data=args.create_data, min_occ=args.min_occ) total_bleu = 0.0 total_iterations = 0 for split in splits: # create dataloader data_loader = DataLoader(dataset=datasets[split], batch_size=args.batch_size, shuffle=split == 'train', num_workers=cpu_count(), pin_memory=torch.cuda.is_available()) for iteration, batch in enumerate(data_loader): # get batch size batch_size = batch['input'].size(0) for k, v in batch.items(): if torch.is_tensor(v): batch[k] = to_var(v) logp = model.bleu(batch['input'], batch['length']) gen_sents = idx2word(logp, i2w=i2w, pad_idx=w2i['<pad>']) batch_sents = idx2word(batch['input'], i2w=i2w, pad_idx=w2i['<pad>']) generated = [line.strip().split() for line in gen_sents] actual = [line.strip().split() for line in batch_sents] all_actual = [actual for i in range(len(generated))] bleus = nltk.translate.bleu_score.corpus_bleu( all_actual, generated) total_bleu = total_bleu + bleus total_iterations = iteration + 1 # if iteration==: # break bleu_score = total_bleu / total_iterations print(bleu_score)
def main(args): # check args if not os.path.exists(args.load_checkpoint): raise FileNotFoundError(args.load_checkpoint) if not os.path.exists(args.load_params): raise FileNotFoundError(args.load_params) # load dataset split = 'test' datasets = defaultdict(dict) datasets[split] = SnliYelp(split=split, create_data=False, min_occ=2) # load pretrained vocab with open('./data/snli_yelp/snli_yelp.vocab.json', 'r') as file: vocab = json.load(file) w2i, i2w = vocab['w2i'], vocab['i2w'] # load params params = load_model_params_from_checkpoint(args.load_params) # create model model = SentenceVaeMultiTask(**params) print(model) model.load_state_dict(torch.load(args.load_checkpoint)) print("Model loaded from %s" % args.load_checkpoint) if torch.cuda.is_available(): model = model.cuda() model.eval() # get random sentence 1 from snli sent1 = datasets[split].__getitem__(6) # get random sentence 1 from yelp sent2 = datasets[split].__getitem__(74) # get the lspace vectors for sent1 and sent2 sent1_tokens = torch.tensor(sent1['input']).unsqueeze(0) sent2_tokens = torch.tensor(sent2['input']).unsqueeze(0) batch = torch.cat((sent1_tokens, sent1_tokens), 0).cuda() style_z, content_z = model.encode_to_lspace(batch) # print sent1 and 2 print() print("Sentence 1:") print() print(*idx2word(sent1_tokens, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n') print() print("Sentence 2:") print() print(*idx2word(sent2_tokens, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n') print() print("Style of Sentence2 on content of Sentence1:") print() # contact style_z of sent 1 to content_z of sent_2 final_z = torch.cat((style_z[1], content_z[0]), -1).unsqueeze(0) samples, _ = model.inference(z=final_z) print(*idx2word(samples, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n') # print(final_z.shape) exit()