{'fraction_non_alphanumeric': 0.028787989213614168, 'fraction_numerical': 0.007288098535092194, 'mean_word_length': 4.642269736842105, 'pattern_counts': {'":': 0, '<': 0, '<?xml version=': 0, '>': 0, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
Discriminative Neural Topic Models

We propose a neural network based approach for learning topics from text and image datasets. The model makes no assumptions about the conditional distribution of the observed features given the latent topics. This allows us to perform topic modelling efficiently using sentences of documents and patches of images as observed features, rather than limiting ourselves to words. Moreover, the proposed approach is online, and hence can be used for streaming data. Furthermore, since the approach utilizes neural networks, it can be implemented on GPU with ease, and hence it is very scalable.


Introduction
Mixed membership modeling is the task of assigning the observed features of an object to latent classes. Each object is assumed to be a mixture over the latent classes, and these classes are shared among the objects. This is distinct from mixture modeling, where all the features of the object are assumed to be sampled from a single latent class.
Traditional approaches to mixed membership modeling assume a parametric form for the distribution of a latent class over the features, and the distribution of an object over these classes. For instance, in case of documents, the observed features are the words, and the latent classes are the topics. A topic is assumed to have a multinomial distribution over the words, while a document is assumed to have a multinomial distribution over the topics [6]. This statistical model is also referred to as probabilistic latent semantic indexing (pLSI). One can further define Dirichlet priors on the parameters of the multinomial distributions, to obtain a full Bayesian treatment of topic modeling [1,9]. This model, commonly referred to as latent Dirichlet allocation (LDA), is widely used for finding topics in document collections, and inferring population structures from genotype data.
Both LDA and pLSI are generative models, whereby the conditional distribution of the observed features given the latent classes is explicitly modeled. This requires explicit knowledge about the parametric form of these distributions. The number of unique words in a language are finite, and hence, it is possible to make these distributions as general as possible by choosing them to be multinomial. However, as the set of all unique observed features becomes comparable to the size of the collection, this approach becomes less and less meaningful.
Hence, while generative topic models are useful for modeling words in a document collection, they can't be directly used for modeling sentences and paragraphs in documents, or pixels of an image. An intermediate preprocessing step is required that transforms all the observed features in the collection to a relatively small set of unique 'codewords' [3]. Since this intermediate step is independent of topic modeling, it can result in suboptimal features. Finding good intermediate representations suitable for topic modeling can be as challenging as finding the topics themselves.
While the observed features can be extremely complicated to model (for instance, the pixels in an image), the conditional distribution of the latent classes given the observed features is always multinomial. The parameters of the multinomial distribution are functions of the observed features. In this paper, we propose a model for learning the latent classes of an object without explicitly modeling the observed features. This allows us to make minimal assumptions about the distributions of the observed features. In particular, we propose discriminative neural topic models (DNTM), whereby the conditional distribution of the latent classes or topics given the observed features is directly modeled using neural networks. The model can be trained end-to-end using backpropagation. To our knowledge, this is the first approach to topic modeling that doesn't explicitly model the distribution of observed features given the latent classes.
In order to establish that DNTM indeed learns meaningful topics, we use it for assigning words to topics for well-known text corpuses. The learnt topics are used in secondary tasks such as clustering. Despite the fact that LDA makes minimal assumptions for modeling words in document collections, our model is able to outperform LDA even for these datasets. Note that these experiments only serve as a sanity check confirming that the proposed model indeed learns meaningful topics, and is competitive with LDA even for tasks where LDA makes minimal assumptions.
We also use DNTM for learning topics on CIFAR-10 dataset in an unsupervised manner. We use a convolutional neural network to convert the image into spatial features, which correspond to the words of the document. The CNN is trained by the backpropagating the gradient from the topic model, that is, we train the CNN to learn features, that correspond to meaningful topics. In order to prevent the model from overfitting, we use adversarial training by coupling it with a generator [4]. The model is trained to perform poorly on generated images, while the generator is trained to generate images that perform well on the topic modelling task.

Discriminative Neural Topic Models
Let X (1) , . . . X (n) be the observed features for n objects, while Z (1) , . . . , Z (n) be the corresponding latent classes. Let K be the number of latent classes.

Modeling the words in a document
In case of documents, X (i) j represents the embedding vector of the j th word in the i th document X (i) with an arbitrary order, and Z (i) j represents the corresponding topic or latent class. In particular, there exists an embedding vector for every word in the vocabulary. The embeddings are initialized randomly and are trained by backpropagating the gradient of the topic model.
The length of the i th document is given by m i . We assume that given the words of the document, the topics are independently distributed, that is, j , 1 ≤ j ≤ m i are multinomial random variables whose parameters are functions of X (i) . An explicit parametric form for this distribution is specified in the experiments section. In the rest of the paper, the dependence of the distributions on θ is assumed without being explicitly stated.
Since, we wish to associate each word X Minimizing entropy ensures that the conditional distribution over the topics for a word is highly peaked for a very few topics. This is also referred to as cluster assumption, and has been used for clustering [7] and semi supervised learning [5,2], where it enjoys considerable success.
Next, we need to ensure that the words in a single document have similar distribution over the topics. This corresponds to minimizing the variance between the probability distributions over topics for the words of the document. We achieve this by minimizing the KL-divergence between the conditional distribution of topics given the words P (Z (i) j |X (i) ), and the corresponding average over the words, that is where P d (k|X (i) ) is the distribution over the topics for document d. If we assume that each observed word in the document occurs with equal probability, then the document distribution over the topics is given by P d (k|X (i) ) = 1 mi mi j=1 P (Z (i) j = k|X (i) ). Finally, in order to ensure that all the documents don't get assigned the same topic, we add a term for encouraging balance among the topics. Towards this end, we definē We maximize the entropy of the above distribution to enforce a uniform prior on the topics. Ways to relax this assumption are discussed later in the paper.
Combining the above three criteria, the objective function for the j th word of i th document is given by where θ is the parameter of the distribution P (Z i |X (i) ). The above term is minimized for all the words of all the documents in the corpus, that is,

Regularizing the model
In order to ensure that the model doesn't overfit the training data, we use negative sampling. In particular, for text documments, we randomly sample the words in the vocabulary, to create a fake document. Next, we force the model to perform poorly on the fake document as follows: 1. The distributions over topics for the words in a fake document should be highly uncertain. This corresponds to maximizing the entropy of the corresponding topic distributions, that is, The distribution over topics for the words in a fake document, should have high variance.
This corresponds to maximizing the KL-divergence of the topic distribution from the average for the words in the fake document, that is KL(P Z (i) j ||P dZ (i) ; X (i) ) is maximized.

Document Clustering
Firstly, we verify that the model indeed performs topic modelling. Towards that end, we apply our model to learn topics on 20 newsgroup, one of the most widely used datasets for text categorization. In order to ensure reproducibility, we use a pre-processed version of the dataset 1 . The preprocessed 20 newsgroup dataset consists of 11,293 documents for training and 7,528 documents for testing, with a vocabulary size of 8,165 stemmed words. The data is almost evenly divided between the 20 classes. We discard documents with less than 2 words.
For clustering tasks, we combine the training and test sets. We use two metrics to evaluate the effectiveness of the proposed topic model for clustering -purity and normalized mutual information (NMI) 2 .
We compare the proposed model against several standard algorithms for clustering and topicmodelling. These include K-means, Normalized cuts [10], probabilistic Latent Semantic indexing (pLSI) and Latent Dirichlet Allocation (LDA). For LDA, pLSI and the proposed model DNTM, the topics correspond to clusters and a document is assigned to the cluster/topic with the highest posterior probability for the given document. This approach has been shown to be more effective than clustering the topic representations of documents [8].  The clustering results for 20 newsgroup are given in Table 1(a). As can be seen from the results, there is a distinct improvement in performance as one moves from standard clustering algorithms to topic-modelling algorithms such as pLSI, LDA and DNTM. Moreover, the performance obtained using DNTM is at par with the performance using LDA and pLSI.

The topics
Although the proposed model DNTM only learns the distribution of the topics given the words, we can obtain the distribution of the words given the topics as follows: First, we compute the joint probability of the topic t and the word w occurring together as follows: Here, P (X (i) j = w) = 1, if the i th word of the j th document is w and 0 otherwise. Informally, the above equation simply computes the probability of observing the topic t, every time the word w occurs, and takes the average of all these probabilities. Finally, to compute the posterior, we normalize the above distribution.P Using the above method, we obtain the distribution of the topics over the words for DNTM. The 20 most probable words for each topic obtained using DNTM are listed in Table 2.4. One can observe that the topics learnt by the model are coherent, that is, the words corresponding to a single topic occur commonly in a single class.

Topic modelling in images
In order to extend the proposed model for images, one needs to define 'words' and 'documents' for images. The most common approach for obtaining words from images involves extraction of features (SIFT, HOG etc.,) from images. These features are then clustered using K-means. The In this work, however, we learn the words from the pixels of an image by employing a convolutional network. In particular, let X (i) represents the i th image, Y (i) j represents the j th word of the i th image and Z (i) j represents the corresponding topic. In order to convert the the X (i) into words Y (i) j , we feed X (i) to a convolutional neural network. The output of the CNN is treated as words of the image. For instance, if the output of CNN consists of 100 features maps of size 8x8, we treat them as 64 words, where each word is represented using 100 dimensions. The topic distribution of a word is obtained by applying 1 × 1 convolution to the word representation, and then applying softmax to the output. Note that the words Y Now that we have defined the words in an image, we need to define the concept of a document. The definition of a document depends on the underlying task. In this paper, we consider two possible definitions for documents. In the first scenario, we are interested in clustering similar images together. For this task, we consider that all the words in an image belong to the same document. This is the usual assumption in topic modelling. The words and the document are then fed to the proposed model, exactly as for documents.

Treating images as documents
We use the proposed model to learn topics in CIFAR-10 dataset. The dataset consists of 50,000 training images and 10,000 test images divided into 10 classes. This dataset is quite challenging, since there is high variability within each class, even though the individual images are only 32x32 pixels.
We use convolutional neural networks for obtaining the features from the images. In particular, for the 32x32 CIFAR-10 images, we apply 4 layers of strided convolution and ReLU nonlinearity to obtain 64 (8x8) features per image. These features function as words, and are fed as input to the DNTM. Note that CNN is trained only by the DNTM, thereby coupling feature extraction and topic modelling.
We train the DNTM to extract 100 topics from the CIFAR-10 dataset. 9 of those topics are shown in Figure 3. In particular, for each topic, we have listed the 24 most probable images. One can observe that the topics are qualitatively coherent.