Dreaming over text!

Extending the idea of Deep Dream to textual data!

Shivam Kaushik
Towards Data Science

--

“DeepDream is an experiment that visualizes the patterns learned by a neural network. Similar to when a child watches clouds and tries to interpret random shapes, DeepDream over-interprets and enhances the patterns it sees in an image.

It does so by forwarding an image through the network, then calculating the gradient of the image with respect to the activations of a particular layer. The image is then modified to increase these activations, enhancing the patterns seen by the network, and resulting in a dream-like image. This process was dubbed “Inceptionism” (a reference to InceptionNet, and the movie Inception).”

https://www.tensorflow.org/tutorials/generative/deep dream

Let me break it down for you. Consider a Convolutional Neural Network.

LeNet-5 Architecture

Let us assume we want to check what happens when we increase the highlighted neuron activation h_{i,j}, and we want to reflect these changes onto input image when we increase these activations.

In other words, we are optimizing image so that neuron h_{i,j} fires more.

We can pose this optimization problem as:

Image by Author

That is, we need to maximize the square norm (in simple words magnitude), of h_{i,j} by changing image.

Here is what happens when we do as said above.

(Left) Image by Von.grzanka | (Right) Image by Tensorflow Tutorial

The reason here is that, when the CNN was trained, the neuron in intermediate layers learned to see some patterns(here dog faces). When we increased those activations, the input image started containing more and more dog faces, to maximize the activations.

The intuition behind implementing deep dream over text.

Like deep dream in the image, what if we take any hidden layer activation and try to increase its norm, what will happen to the text input?
To answer this, a text-classification model was taken and loss function was set-up to increase the magnitude of the hidden layer’s activation.
We would expect to see the patterns/representation learned by this hidden layer.

Embeddings Layer

Image by Author | Image showing method to calculate embedding of sentence.

The neural network model cannot understand words. To feed words as input, these words are converted to a n-dimensional array of numbers called embeddings. Each word is converted to specific n-dimensional array. To get embedding of sentence we simply take mean of sentences and then this array is fed as input to model. This method solves the issue of variable length of sentence and can work with vanilla Artificial Neural Network.

Model

Image by Author

A model trained to classify IMDB reviews was used. The model achieved validation accuracy of 80.10%.

The experiment was set up to capture the representation given by Fully Connected layer 2 or FC2 in short with 512 dimensions.

The cost function used was the norm of fc2 output.

Note: Because “Sequence of Words” are long tensors, they cannot be optimized by back-propagation. Instead, embedding representation of sentence was optimized.

Outline of Procedure

Step 1: Convert sentence to tensors.

Step 2: Get the sentence embeddings.

Step 3: Pass through the fc2 layer, and get the fc2 output.

Step 4: Optimize the sentence embeddings to increase fc2 layer output.

Step 5: Repeat step 2 to step 4 with the current sentence embeddings for a given number of iteration.

Image by Author

Results

Experiment 1

Simple sentences were used to get classification results and their corresponding sentence embedding we saved.

For example: “I hate this.” , “I love this show.”, we used to classify. These sentences are very simple and convey a negative and positive emotion respectively.

Dreaming or optimization over these embeddings was done and a graph of activation over iteration was recorded.

Image by Author

There are a couple of things that can be observed here.

  1. Activation of the hidden layer’s representation increased almost linearly for both these sentences
  2. Activations of these sentences are different, which means that model can easily differentiate between these two sentences.

For the sentence: “I love this show.”. The model correctly predicts this as positive.

Similar words test

Image by Author | Word cloud based on similarity. Large font means more similar.

Initially sentence embedding as more similar to neutral words like “this, it, even same” but as we increased the magnitude of the fc2 activations, the sentence embedding became similar to positive words like “great, unique”, which makes sense, as the model predicted it a positive sentence. Visualizing embeddings over iterations.

Image by Author | Animation showing how embedding moves away from negative words and move towards positive words.
Video by Author | We visualize the embeddings of positive sentence. Observe how the sentence embedding change with steps.

Try Embeddings here on Tensorflow Projector

Observe how sentence embedding starts from step_1 and move to step_21. The sentence embedding started in between positive and negative words and as algorithm dreams, the embedding move towards positive words.

Try these things.

Observe embeddings in 3d.

Find words similar to step_1.

Find words similar to step_21.

For the sentence: “I hate this”. The model correctly predicts this as negative.

Similar words test

First, we observe what are the words similar (cosine similarity) to the sentence embeddings before and after dreaming.

Image by Author | Word cloud based on similarity. Large font means more similar.

Initially, sentence embedding as more similar to neutral words like “this, it, even, same” but as we increased the magnitude of the fc2 activations, the sentence embedding became similar to words like “bad, nothing, worse” which convey a negative meaning, which it makes sense, as the model predicted it a negative sentence.

Visualizing embeddings over iterations.

To visualize embeddings over iteration, TSNE algorithm was used to reduce the embedding dimension from 100 to 2. These embeddings were plotted on a 2d map with red dots as negative words(like a bad, worse, mean, mistake) and green dots as positive words(like great, celebrated, wonderful).

Grey dots are intermediate locations of sentence embedding and the black dot is the final location of sentence embedding.

Image by Author | 100 dimensional embeddings on 2d space

Try Embeddings here on Tensorflow Projector

The graph clearly shows that embeddings got away from positive words and got near negative words and this is in tune with the model prediction. Moreover, the final sentence embedding is now more similar to red dots(negative words) than green dots(positive words).

The key observation here is that initially, the sentence embedding was in between positive and negative words, but as dreaming progresses the embeddings were pushed away from negative words.

Conclusion

The word embeddings after dreaming become similar to the words in `model prediction`, though if we look at similar words of initial embeddings, they were more or less same for the two sentences even when they were conveying very different meanings, final sentence embedding showed some interesting patterns.

E.g.

1. negative prediction was pushed near to words like mistake, dirty, bad

2. positive prediction was pushed near to words like unique, great, celebrated

Experiment 2

We will use difficult sentences now. Sentences which convey one emotion in the first half but change the sentiment in the second half.

Sentences like

  • The show was very long and boring but the direction was amazing.
  • I hated the show because of nudity but the acting was classy.

These sentences are difficult for a human to judge what kind of emotion they convey.

Again we will optimize the sentence embeddings, to maximize activations in fc2 layer.

Image by Author | Norm of fc2 layer activations as a function of iterations

Unlike the first case. Activations of the two sentences don’t diverge much, i.e. activations for these sentence are more or less similar, this means there is no classifying power of the model for these sentences.
Let’s look at the nearby words of these sentences before and after dreaming.

For sentence: “The show was very long and boring but the direction was really amazing.”, the model predicted positive

Similar Word test

We will find words similar to initial vs final sentence embedding.

Image by Author

Hmm, even though the sentence was classified as positive, the words similar to final sentence embedding don’t reflect any positive sentiment.

For sentence: “I hated the show because of nudity but the acting was really classy.”, the model predicted negative

Similar Word test

Image by Author

The sentence was classified as negative, the embeddings after dreaming reflect negative sentiment.

Conclusion

Because the model had no clear understanding of these sentences, the sentence embedding of these two sentences after dreaming are almost similar(look at similar words after dreaming). This is because model does not have a rich representation of these sentences in its hidden layers.

We started by looking at how deep dream on images work, then we proposed how we can implement deep dream over text. Finally, we have shown how to correctly interpret the results. This method can be used to understand what kind of hidden representation the language model has learnt.

Experiments like these help us understand these black boxes better.

You can try a demo in this notebook.

All the related code is available at my Github Repo.

References

  1. Gradient-Based Learning Applied to Document
    Recognition
  2. https://ai.googleblog.com/2015/06/inceptionism-going-deeper-into-neural.html
  3. https://www.tensorflow.org/tutorials/generative/deepdream
  4. https://youtu.be/YHAIrwRVvWg?list=PLyqSpQzTE6M9gCgajvQbc68Hk_JKGBAYT
  5. https://pytorch.org/tutorials/beginner/text_sentiment_ngrams_tutorial.html

--

--