forked from sharan21/disentangled-style-transfer-vae
-
Notifications
You must be signed in to change notification settings - Fork 0
/
yelp_style_transfer.py
135 lines (104 loc) · 4.57 KB
/
yelp_style_transfer.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import os
import json
import torch
import argparse
from multiprocessing import cpu_count
from collections import defaultdict
from nltk.tokenize import TweetTokenizer
from torch.utils.data import DataLoader
from model_rep import SentenceVae
from utils import to_var, idx2word, interpolate, load_model_params_from_checkpoint
from dataset_preproc_scripts.yelp import Yelp
from random import randint
import numpy as np
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]
# load params
params_path = './saved_vae_models/'+saved_dir_name+'/model_params.json'
if not os.path.exists(params_path):
raise FileNotFoundError(params_path)
params = load_model_params_from_checkpoint(params_path)
# set model and dataset according to options
with open('./data/yelp/yelp.vocab.json', 'r') as file:
vocab = json.load(file)
vaemodel = SentenceVae
dataset = Yelp
# load dataset
split = 'train'
datasets = defaultdict(dict)
datasets[split] = dataset(split=split, create_data=False, min_occ=2)
w2i, i2w = vocab['w2i'], vocab['i2w']
# create model
model = vaemodel(**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("Computing mean style vectors...")
##################### get mean style_z ###################
# 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']
preds = torch.argmax(batch_labels, dim = 1)
if(iteration==0):
pos_preds = style_z[preds == 1].sum(axis = 0) #filter out preds and accumulate
neg_preds = style_z[preds == 0].sum(axis = 0)
else:
pos_preds += style_z[preds == 1].sum(axis = 0)
neg_preds += style_z[preds == 0].sum(axis = 0)
# if iteration==1000:
# # get average
# mean_pos_style = pos_preds/((iteration+1)*args.batch_size)
# mean_neg_style = neg_preds/((iteration+1)*args.batch_size)
# break
mean_pos_style = pos_preds/((iteration+1)*args.batch_size)
mean_neg_style = neg_preds/((iteration+1)*args.batch_size)
################ Experiment 1: flip sentiment of sentences #############3############
for i in range(100):
#pick a sentence
sent1 = datasets[split].__getitem__(i)
# get the lspace vectors for sent1 and sent2
sent1_tokens = torch.tensor(sent1['input']).unsqueeze(0)
batch = torch.cat((sent1_tokens, sent1_tokens), 0).cuda()
style_z, content_z = model.encode_to_lspace(batch, zero_noise=True)
# print outputs
print("{}).---------------------------------------------------".format(i))
print("Sentence 1 with label: {}".format(sent1['label']))
print(*idx2word(sent1_tokens, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n')
print()
label = np.argmax(sent1['label'])
if(label == 1): #if sentiment is positive flip to negative
print("Converting from positive to negative:")
final_z = torch.cat((mean_neg_style, content_z[0]), -1).unsqueeze(0)
else: # and vice versa
print("Converting from negative to positive:")
final_z = torch.cat((mean_pos_style, content_z[0]), -1).unsqueeze(0)
words = model.final_z_to_words(sent1['input'] ,final_z)
# samples, _ = model.inference(z=final_z)
print(*idx2word(words, i2w=i2w, pad_idx=w2i['<pad>']), sep='\n')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-c', '--load_checkpoint', type=str)
parser.add_argument('-p', '--load_params', type=str)
parser.add_argument('-n', '--num_samples', type=int, default=10)
parser.add_argument('-bs', '--batch_size', type=int, default=32)
args = parser.parse_args()
main(args)