-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathctm.py
805 lines (694 loc) · 28.8 KB
/
ctm.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
import datetime
import multiprocessing as mp
import os
import warnings
from collections import defaultdict
import matplotlib.pyplot as plt
import numpy as np
import torch
import wordcloud
from scipy.special import softmax
from torch import optim
from torch.optim.lr_scheduler import ReduceLROnPlateau
from torch.utils.data import DataLoader
from tqdm import tqdm
from contextualized_topic_models.utils.early_stopping.early_stopping import (
EarlyStopping,
)
from contextualized_topic_models.networks.decoding_network import DecoderNetwork
class CTM:
"""Class to train the contextualized topic model. This is the more general class that we are keeping to
avoid braking code, users should use the two subclasses ZeroShotTM and CombinedTm to do topic modeling.
:param bow_size: int, dimension of input
:param contextual_size: int, dimension of input that comes from BERT embeddings
:param inference_type: string, you can choose between the contextual model and the combined model
:param n_components: int, number of topic components, (default 10)
:param model_type: string, 'prodLDA' or 'LDA' (default 'prodLDA')
:param hidden_sizes: tuple, length = n_layers, (default (100, 100))
:param activation: string, 'softplus', 'relu', (default 'softplus')
:param dropout: float, dropout to use (default 0.2)
:param learn_priors: bool, make priors a learnable parameter (default True)
:param batch_size: int, size of batch to use for training (default 64)
:param lr: float, learning rate to use for training (default 2e-3)
:param momentum: float, momentum to use for training (default 0.99)
:param solver: string, optimizer 'adam' or 'sgd' (default 'adam')
:param num_epochs: int, number of epochs to train for, (default 100)
:param reduce_on_plateau: bool, reduce learning rate by 10x on plateau of 10 epochs (default False)
:param num_data_loader_workers: int, number of data loader workers (default cpu_count). set it to 0 if you are using Windows
:param label_size: int, number of total labels (default: 0)
:param loss_weights: dict, it contains the name of the weight parameter (key) and the weight (value) for each loss.
It supports only the weight parameter beta for now. If None, then the weights are set to 1 (default: None).
"""
def __init__(
self,
bow_size,
contextual_size,
inference_type="combined",
n_components=10,
model_type="prodLDA",
hidden_sizes=(100, 100),
activation="softplus",
dropout=0.2,
learn_priors=True,
batch_size=64,
lr=2e-3,
momentum=0.99,
solver="adam",
num_epochs=100,
reduce_on_plateau=False,
num_data_loader_workers=mp.cpu_count(),
label_size=0,
loss_weights=None,
):
self.device = (
torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
)
if self.__class__.__name__ == "CTM":
raise Exception("You cannot call this class. Use ZeroShotTM or CombinedTM")
assert (
isinstance(bow_size, int) and bow_size > 0
), "input_size must by type int > 0."
assert (
isinstance(n_components, int) and bow_size > 0
), "n_components must by type int > 0."
assert model_type in ["LDA", "prodLDA"], "model must be 'LDA' or 'prodLDA'."
assert isinstance(hidden_sizes, tuple), "hidden_sizes must be type tuple."
assert activation in [
"softplus",
"relu",
], "activation must be 'softplus' or 'relu'."
assert dropout >= 0, "dropout must be >= 0."
assert isinstance(learn_priors, bool), "learn_priors must be boolean."
assert (
isinstance(batch_size, int) and batch_size > 0
), "batch_size must be int > 0."
assert lr > 0, "lr must be > 0."
assert (
isinstance(momentum, float) and 0 < momentum <= 1
), "momentum must be 0 < float <= 1."
assert solver in ["adam", "sgd"], "solver must be 'adam' or 'sgd'."
assert isinstance(
reduce_on_plateau, bool
), "reduce_on_plateau must be type bool."
assert (
isinstance(num_data_loader_workers, int) and num_data_loader_workers >= 0
), "num_data_loader_workers must by type int >= 0. set 0 if you are using windows"
self.bow_size = bow_size
self.n_components = n_components
self.model_type = model_type
self.hidden_sizes = hidden_sizes
self.activation = activation
self.dropout = dropout
self.learn_priors = learn_priors
self.batch_size = batch_size
self.lr = lr
self.contextual_size = contextual_size
self.momentum = momentum
self.solver = solver
self.num_epochs = num_epochs
self.reduce_on_plateau = reduce_on_plateau
self.num_data_loader_workers = num_data_loader_workers
self.training_doc_topic_distributions = None
if loss_weights:
self.weights = loss_weights
else:
self.weights = {"beta": 1}
self.model = DecoderNetwork(
bow_size,
self.contextual_size,
inference_type,
n_components,
model_type,
hidden_sizes,
activation,
dropout,
learn_priors,
label_size=label_size,
)
self.early_stopping = None
# init optimizer
if self.solver == "adam":
self.optimizer = optim.Adam(
self.model.parameters(), lr=lr, betas=(self.momentum, 0.99)
)
elif self.solver == "sgd":
self.optimizer = optim.SGD(
self.model.parameters(), lr=lr, momentum=self.momentum
)
# init lr scheduler
if self.reduce_on_plateau:
self.scheduler = ReduceLROnPlateau(self.optimizer, patience=10)
# performance attributes
self.best_loss_train = float("inf")
# training attributes
self.model_dir = None
self.nn_epoch = None
# validation attributes
self.validation_data = None
# learned topics
self.best_components = None
# Use cuda if available
if torch.cuda.is_available():
self.USE_CUDA = True
else:
self.USE_CUDA = False
self.model = self.model.to(self.device)
def _loss(
self,
inputs,
word_dists,
prior_mean,
prior_variance,
posterior_mean,
posterior_variance,
posterior_log_variance,
):
# KL term
# var division term
var_division = torch.sum(posterior_variance / prior_variance, dim=1)
# diff means term
diff_means = prior_mean - posterior_mean
diff_term = torch.sum((diff_means * diff_means) / prior_variance, dim=1)
# logvar det division term
logvar_det_division = prior_variance.log().sum() - posterior_log_variance.sum(
dim=1
)
# combine terms
KL = 0.5 * (var_division + diff_term - self.n_components + logvar_det_division)
# Reconstruction term
RL = -torch.sum(inputs * torch.log(word_dists + 1e-10), dim=1)
# loss = self.weights["beta"]*KL + RL
return KL, RL
def _train_epoch(self, loader):
"""Train epoch."""
self.model.train()
train_loss = 0
samples_processed = 0
for batch_samples in loader:
# batch_size x vocab_size
X_bow = batch_samples["X_bow"]
X_bow = X_bow.reshape(X_bow.shape[0], -1)
X_contextual = batch_samples["X_contextual"]
if "labels" in batch_samples.keys():
labels = batch_samples["labels"]
labels = labels.reshape(labels.shape[0], -1)
labels = labels.to(self.device)
else:
labels = None
if self.USE_CUDA:
X_bow = X_bow.cuda()
X_contextual = X_contextual.cuda()
# forward pass
self.model.zero_grad()
(
prior_mean,
prior_variance,
posterior_mean,
posterior_variance,
posterior_log_variance,
word_dists,
estimated_labels,
) = self.model(X_bow, X_contextual, labels)
# backward pass
kl_loss, rl_loss = self._loss(
X_bow,
word_dists,
prior_mean,
prior_variance,
posterior_mean,
posterior_variance,
posterior_log_variance,
)
loss = self.weights["beta"] * kl_loss + rl_loss
loss = loss.sum()
if labels is not None:
target_labels = torch.argmax(labels, 1)
label_loss = torch.nn.CrossEntropyLoss()(
estimated_labels, target_labels
)
loss += label_loss
loss.backward()
self.optimizer.step()
# compute train loss
samples_processed += X_bow.size()[0]
train_loss += loss.item()
train_loss /= samples_processed
return samples_processed, train_loss
def fit(
self,
train_dataset,
validation_dataset=None,
save_dir=None,
verbose=False,
patience=5,
delta=0,
n_samples=20,
do_train_predictions=True,
):
"""
Train the CTM model.
:param train_dataset: PyTorch Dataset class for training data.
:param validation_dataset: PyTorch Dataset class for validation data. If not None, the training stops if validation loss doesn't improve after a given patience
:param save_dir: directory to save checkpoint models to.
:param verbose: verbose
:param patience: How long to wait after last time validation loss improved. Default: 5
:param delta: Minimum change in the monitored quantity to qualify as an improvement. Default: 0
:param n_samples: int, number of samples of the document topic distribution (default: 20)
:param do_train_predictions: bool, whether to compute train predictions after fitting (default: True)
"""
# Print settings to output file
if verbose:
print(
"Settings: \n\
N Components: {}\n\
Topic Prior Mean: {}\n\
Topic Prior Variance: {}\n\
Model Type: {}\n\
Hidden Sizes: {}\n\
Activation: {}\n\
Dropout: {}\n\
Learn Priors: {}\n\
Learning Rate: {}\n\
Momentum: {}\n\
Reduce On Plateau: {}\n\
Save Dir: {}".format(
self.n_components,
0.0,
1.0 - (1.0 / self.n_components),
self.model_type,
self.hidden_sizes,
self.activation,
self.dropout,
self.learn_priors,
self.lr,
self.momentum,
self.reduce_on_plateau,
save_dir,
)
)
self.model_dir = save_dir
self.idx2token = train_dataset.idx2token
train_data = train_dataset
self.validation_data = validation_dataset
if self.validation_data is not None:
self.early_stopping = EarlyStopping(
patience=patience, verbose=verbose, path=save_dir, delta=delta
)
train_loader = DataLoader(
train_data,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_data_loader_workers,
drop_last=True,
)
# init training variables
samples_processed = 0
# train loop
pbar = tqdm(self.num_epochs, position=0, leave=True)
for epoch in range(self.num_epochs):
self.nn_epoch = epoch
# train epoch
s = datetime.datetime.now()
sp, train_loss = self._train_epoch(train_loader)
samples_processed += sp
e = datetime.datetime.now()
pbar.update(1)
if self.validation_data is not None:
validation_loader = DataLoader(
self.validation_data,
batch_size=self.batch_size,
shuffle=True,
num_workers=self.num_data_loader_workers,
drop_last=True,
)
# train epoch
s = datetime.datetime.now()
val_samples_processed, val_loss = self._validation(validation_loader)
e = datetime.datetime.now()
# report
if verbose:
print(
"Epoch: [{}/{}]\tSamples: [{}/{}]\tValidation Loss: {}\tTime: {}".format(
epoch + 1,
self.num_epochs,
val_samples_processed,
len(self.validation_data) * self.num_epochs,
val_loss,
e - s,
)
)
pbar.set_description(
"Epoch: [{}/{}]\t Seen Samples: [{}/{}]\tTrain Loss: {}\tValid Loss: {}\tTime: {}".format(
epoch + 1,
self.num_epochs,
samples_processed,
len(train_data) * self.num_epochs,
train_loss,
val_loss,
e - s,
)
)
self.early_stopping(val_loss, self)
if self.early_stopping.early_stop:
print("Early stopping")
break
else:
# save last epoch
self.best_components = self.model.beta
if save_dir is not None:
self.save(save_dir)
pbar.set_description(
"Epoch: [{}/{}]\t Seen Samples: [{}/{}]\tTrain Loss: {}\tTime: {}".format(
epoch + 1,
self.num_epochs,
samples_processed,
len(train_data) * self.num_epochs,
train_loss,
e - s,
)
)
pbar.close()
if do_train_predictions:
self.training_doc_topic_distributions = self.get_doc_topic_distribution(
train_dataset, n_samples
)
def _validation(self, loader):
"""Validation epoch."""
self.model.eval()
val_loss = 0
samples_processed = 0
for batch_samples in loader:
# batch_size x vocab_size
X_bow = batch_samples["X_bow"]
X_bow = X_bow.reshape(X_bow.shape[0], -1)
X_contextual = batch_samples["X_contextual"]
if "labels" in batch_samples.keys():
labels = batch_samples["labels"]
labels = labels.to(self.device)
labels = labels.reshape(labels.shape[0], -1)
else:
labels = None
if self.USE_CUDA:
X_bow = X_bow.cuda()
X_contextual = X_contextual.cuda()
# forward pass
self.model.zero_grad()
(
prior_mean,
prior_variance,
posterior_mean,
posterior_variance,
posterior_log_variance,
word_dists,
estimated_labels,
) = self.model(X_bow, X_contextual, labels)
kl_loss, rl_loss = self._loss(
X_bow,
word_dists,
prior_mean,
prior_variance,
posterior_mean,
posterior_variance,
posterior_log_variance,
)
loss = self.weights["beta"] * kl_loss + rl_loss
loss = loss.sum()
if labels is not None:
target_labels = torch.argmax(labels, 1)
label_loss = torch.nn.CrossEntropyLoss()(
estimated_labels, target_labels
)
loss += label_loss
# compute train loss
samples_processed += X_bow.size()[0]
val_loss += loss.item()
val_loss /= samples_processed
return samples_processed, val_loss
def get_thetas(self, dataset, n_samples=20):
"""
Get the document-topic distribution for a dataset of topics. Includes multiple sampling to reduce variation via
the parameter n_sample.
:param dataset: a PyTorch Dataset containing the documents
:param n_samples: the number of sample to collect to estimate the final distribution (the more the better).
"""
return self.get_doc_topic_distribution(dataset, n_samples=n_samples)
def get_doc_topic_distribution(self, dataset, n_samples=20):
"""
Get the document-topic distribution for a dataset of topics. Includes multiple sampling to reduce variation via
the parameter n_sample.
:param dataset: a PyTorch Dataset containing the documents
:param n_samples: the number of sample to collect to estimate the final distribution (the more the better).
"""
self.model.eval()
loader = DataLoader(
dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_data_loader_workers,
)
final_thetas = []
with torch.no_grad():
for batch_samples in tqdm(loader):
# batch_size x vocab_size
X_bow = batch_samples["X_bow"]
X_bow = X_bow.reshape(X_bow.shape[0], -1)
X_contextual = batch_samples["X_contextual"]
if "labels" in batch_samples.keys():
labels = batch_samples["labels"]
labels = labels.to(self.device)
labels = labels.reshape(labels.shape[0], -1)
else:
labels = None
if self.USE_CUDA:
X_bow = X_bow.cuda()
X_contextual = X_contextual.cuda()
# forward pass
self.model.zero_grad()
mu, log_var = self.model.get_posterior(X_bow, X_contextual, labels)
thetas = self.model.sample(mu, log_var, n_samples).cpu().numpy()
final_thetas.append(thetas)
return np.concatenate(final_thetas, axis=0)
def get_doc_topic_distribution_iterator(self, dataset, n_samples=20):
"""
Get the document-topic distribution for a dataset of topics. Includes multiple sampling to reduce variation via
the parameter n_sample. Returns an iterator over the document-topic distributions.
:param dataset: a PyTorch Dataset containing the documents
:param n_samples: the number of sample to collect to estimate the final distribution (the more the better).
"""
self.model.eval()
loader = DataLoader(
dataset,
batch_size=self.batch_size,
shuffle=False,
num_workers=self.num_data_loader_workers,
)
with torch.no_grad():
for batch_samples in loader:
# batch_size x vocab_size
X_bow = batch_samples["X_bow"]
X_bow = X_bow.reshape(X_bow.shape[0], -1)
X_contextual = batch_samples["X_contextual"]
if "labels" in batch_samples.keys():
labels = batch_samples["labels"]
labels = labels.to(self.device)
labels = labels.reshape(labels.shape[0], -1)
else:
labels = None
if self.USE_CUDA:
X_bow = X_bow.cuda()
X_contextual = X_contextual.cuda()
# forward pass
self.model.zero_grad()
mu, log_var = self.model.get_posterior(X_bow, X_contextual, labels)
thetas = self.model.sample(mu, log_var, n_samples).cpu().numpy()
for theta in thetas:
yield theta
def get_most_likely_topic(self, doc_topic_distribution):
"""get the most likely topic for each document
:param doc_topic_distribution: ndarray representing the topic distribution of each document
"""
return np.argmax(doc_topic_distribution, axis=0)
def get_topics(self, k=10):
"""
Retrieve topic words.
:param k: int, number of words to return per topic, default 10.
"""
assert k <= self.bow_size, "k must be <= input size."
component_dists = self.best_components
topics = defaultdict(list)
for i in range(self.n_components):
_, idxs = torch.topk(component_dists[i], k)
component_words = [self.idx2token[idx] for idx in idxs.cpu().numpy()]
topics[i] = component_words
return topics
def get_topic_lists(self, k=10):
"""
Retrieve the lists of topic words.
:param k: (int) number of words to return per topic, default 10.
"""
assert k <= self.bow_size, "k must be <= input size."
# TODO: collapse this method with the one that just returns the topics
component_dists = self.best_components
topics = []
for i in range(self.n_components):
_, idxs = torch.topk(component_dists[i], k)
component_words = [self.idx2token[idx] for idx in idxs.cpu().numpy()]
topics.append(component_words)
return topics
def _format_file(self):
model_dir = "contextualized_topic_model_nc_{}_tpm_{}_tpv_{}_hs_{}_ac_{}_do_{}_lr_{}_mo_{}_rp_{}".format(
self.n_components,
0.0,
1 - (1.0 / self.n_components),
self.model_type,
self.hidden_sizes,
self.activation,
self.dropout,
self.lr,
self.momentum,
self.reduce_on_plateau,
)
return model_dir
def save(self, models_dir=None):
"""
Save model. (Experimental Feature, not tested)
:param models_dir: path to directory for saving NN models.
"""
warnings.simplefilter("always", Warning)
warnings.warn(
"This is an experimental feature that we has not been fully tested. Refer to the following issue:"
"https://github.com/MilaNLProc/contextualized-topic-models/issues/38",
Warning,
)
if (self.model is not None) and (models_dir is not None):
model_dir = self._format_file()
if not os.path.isdir(os.path.join(models_dir, model_dir)):
os.makedirs(os.path.join(models_dir, model_dir))
filename = "epoch_{}".format(self.nn_epoch) + ".pth"
fileloc = os.path.join(models_dir, model_dir, filename)
with open(fileloc, "wb") as file:
torch.save(
{"state_dict": self.model.state_dict(), "dcue_dict": self.__dict__},
file,
)
def load(self, model_dir, epoch):
"""
Load a previously trained model. (Experimental Feature, not tested)
:param model_dir: directory where models are saved.
:param epoch: epoch of model to load.
"""
warnings.simplefilter("always", Warning)
warnings.warn(
"This is an experimental feature that we has not been fully tested. Refer to the following issue:"
"https://github.com/MilaNLProc/contextualized-topic-models/issues/38",
Warning,
)
epoch_file = "epoch_" + str(epoch) + ".pth"
model_file = os.path.join(model_dir, epoch_file)
with open(model_file, "rb") as model_dict:
checkpoint = torch.load(model_dict, map_location=torch.device(self.device))
for (k, v) in checkpoint["dcue_dict"].items():
setattr(self, k, v)
self.model.load_state_dict(checkpoint["state_dict"])
def get_topic_word_matrix(self):
"""
Return the topic-word matrix (dimensions: number of topics x length of the vocabulary).
If model_type is LDA, the matrix is normalized; otherwise the matrix is unnormalized.
"""
return self.model.topic_word_matrix.cpu().detach().numpy()
def get_topic_word_distribution(self):
"""
Return the topic-word distribution (dimensions: number of topics x length of the vocabulary).
"""
mat = self.get_topic_word_matrix()
return softmax(mat, axis=1)
def get_word_distribution_by_topic_id(self, topic):
"""
Return the word probability distribution of a topic sorted by probability.
:param topic: id of the topic (int)
:returns list of tuples (word, probability) sorted by the probability in descending order
"""
if topic >= self.n_components:
raise Exception("Topic id must be lower than the number of topics")
else:
wd = self.get_topic_word_distribution()
t = [(word, wd[topic][idx]) for idx, word in self.idx2token.items()]
t = sorted(t, key=lambda x: -x[1])
return t
def get_wordcloud(
self, topic_id, n_words=5, background_color="black", width=1000, height=400
):
"""
Plotting the wordcloud. It is an adapted version of the code found here:
http://amueller.github.io/word_cloud/auto_examples/simple.html#sphx-glr-auto-examples-simple-py and
here https://github.com/ddangelov/Top2Vec/blob/master/top2vec/Top2Vec.py
:param topic_id: id of the topic
:param n_words: number of words to show in word cloud
:param background_color: color of the background
:param width: width of the produced image
:param height: height of the produced image
"""
word_score_list = self.get_word_distribution_by_topic_id(topic_id)[:n_words]
word_score_dict = {tup[0]: tup[1] for tup in word_score_list}
plt.figure(figsize=(10, 4), dpi=200)
plt.axis("off")
plt.imshow(
wordcloud.WordCloud(
width=width, height=height, background_color=background_color
).generate_from_frequencies(word_score_dict)
)
plt.title("Displaying Topic " + str(topic_id), loc="center", fontsize=24)
plt.show()
def get_predicted_topics(self, dataset, n_samples):
"""
Return the list containing the predicted topic for each document (length: number of documents).
:param dataset: CTMDataset to infer topics
:param n_samples: number of sampling of theta
:return: the predicted topics
"""
predicted_topics = []
thetas = self.get_doc_topic_distribution(dataset, n_samples)
for idd in range(len(dataset)):
predicted_topic = np.argmax(thetas[idd] / np.sum(thetas[idd]))
predicted_topics.append(predicted_topic)
return predicted_topics
def get_ldavis_data_format(self, vocab, dataset, n_samples):
"""
Returns the data that can be used in input to pyldavis to plot
the topics
"""
term_frequency = np.ravel(dataset.X_bow.sum(axis=0))
doc_lengths = np.ravel(dataset.X_bow.sum(axis=1))
term_topic = self.get_topic_word_distribution()
doc_topic_distribution = self.get_doc_topic_distribution(
dataset, n_samples=n_samples
)
data = {
"topic_term_dists": term_topic,
"doc_topic_dists": doc_topic_distribution,
"doc_lengths": doc_lengths,
"vocab": vocab,
"term_frequency": term_frequency,
}
return data
def get_top_documents_per_topic_id(
self, unpreprocessed_corpus, document_topic_distributions, topic_id, k=5
):
probability_list = document_topic_distributions.T[topic_id]
ind = probability_list.argsort()[-k:][::-1]
res = []
for i in ind:
res.append(
(unpreprocessed_corpus[i], document_topic_distributions[i][topic_id])
)
return res
class ZeroShotTM(CTM):
"""ZeroShotTM, as described in https://arxiv.org/pdf/2004.07737v1.pdf"""
def __init__(self, **kwargs):
inference_type = "zeroshot"
super().__init__(**kwargs, inference_type=inference_type)
class CombinedTM(CTM):
"""CombinedTM, as described in https://arxiv.org/pdf/2004.03974.pdf"""
def __init__(self, **kwargs):
inference_type = "combined"
super().__init__(**kwargs, inference_type=inference_type)