The Pessimistic Machine

The Pessimistic is a proof of concept for style adaptation. The machine takes a sentence as input and returns a sentence addressing a similar object but conveying a negative sentiment.

The machine needs to be fed with the latent representations of the sentences from the dataset which can be obtained using the script compute_latent_representations.py.


In [141]:
import pandas as pd
import numpy as np
import time
import datetime 
import json
from tqdm import tqdm
import os
import tensorflow as tf
import seaborn as sns
import matplotlib
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 
from bokeh.io import output_notebook
from bokeh.plotting import figure, output_file, show, ColumnDataSource
from bokeh.models import HoverTool
output_notebook()

from data_utils_LMR import prepare_data,read_data, EncoderDecoder
from model import Vrae as Vrae_model
from batch import Generator

prepare_data(1000)

training_dir = 'logs/'
training_dir += 'no_char2word'

# sentiment analyzer
from nltk.sentiment.vader import SentimentIntensityAnalyzer
sentimentAnalyzer = SentimentIntensityAnalyzer()
def getSentimentScore(sentence):
    scores = sentimentAnalyzer.polarity_scores(sentence)
    return (scores['neg'], scores['neu'] ,scores['pos'])

class dotdict(dict):
    """dot.notation access to dictionary attributes"""
    __getattr__ = dict.get
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__
    
def string2bool(st):
    if st.lower() == "true":
        return True
    else:
        return False
    
with open(training_dir +'/flags.json', 'r') as fp:
    FLAGS = dotdict(json.loads( fp.read() ) )
    
for k,v in FLAGS.iteritems():
    print k,':',v
      
n_samples = 5000#int(FLAGS.batch_size)


Loading BokehJS ...
Downloading data from data_LMR/..
Data already downloaded.
Creating Vocabulary..
Vocabulary already created.
Converting sentences to sequences of ids..
Moving some line from test set to train set..
latent_dim : 16
sequence_max : 30
decoder_num_layers : 2
beta_period : 500
training_dir : logs/no_char2word
epoches : 10000
batch_size : 600
char2word_num_layers : 2
initialize : False
teacher_forcing : True
learning_rate_change_rate : 3000
beta_offset : 15
encoder_num_layers : 2
char2word_state_size : 256
sequence_min : 8
use_char2word : False
dtype_precision : 32
cell : GRU
use_sentiment_feature : False
initial_learning_rate : 0.001
decoder_state_size : 1024
output_keep_prob : 0.5
acceptable_accuracy : 0.4
peephole : True
input_keep_prob : 0.9
encoder_state_size : 1024
latent_loss_weight : 0.01

k-NN Decoder


In [142]:
labels = []
zs = []
with tf.gfile.GFile(training_dir + "/latent_representations.txt" , mode="r") as source_file:
    source = source_file.readline()
    counter = 0
    while source:
        source = source_file.readline()
        if len(source.split('|')) > 1:
            z_ =  [ float(u) for u in source.split('|')[1].split(',')]
            if len(z_) == 16:
                labels.append(source.split('|')[0])
                zs.append(z_ )
            counter += 1
print len(zs), 'points'


77976 points

In [143]:
from sklearn.neighbors import KDTree
kdt = KDTree(np.array(zs), leaf_size=1,metric='euclidean')
def getNeighbor(zz, n_similar = 5):
    """ 
    take a z value and returns the neighrest neighbor in the latent space from the training set
    """
    dist, ind = kdt.query( zz, k=n_similar)
    return [ labels[k] for k in list(ind[0]) ], dist

The VAE model


In [144]:
with open(training_dir +'/training_parameters.json', 'r') as fp:
    training_parameters = json.loads( fp.read() )
# vocabulary encoder-decoder
encoderDecoder = EncoderDecoder()
num_symbols = encoderDecoder.vocabularySize()
# prepare data
sentences, ratings = read_data( max_size=None, 
                               max_sentence_size=training_parameters['seq_max'],
                               min_sentence_size=int(FLAGS.sequence_min), 
                               test=False) 
print len(sentences), " sentences"

encoderDecoder = EncoderDecoder()

config = tf.ConfigProto(
        device_count = {'GPU': 0}, # do not use GPU for testing
    )

FLAGS.peephole = False
# load model
vrae_model = Vrae_model(char2word_state_size = int(FLAGS.char2word_state_size), 
                     char2word_num_layers = int(FLAGS.char2word_num_layers), 
                     encoder_state_size = int(FLAGS.encoder_state_size), 
                     encoder_num_layers = int(FLAGS.encoder_num_layers), 
                     decoder_state_size = int(FLAGS.decoder_state_size), 
                     decoder_num_layers = int(FLAGS.decoder_num_layers), 
                          latent_dim=int(FLAGS.latent_dim),
                         batch_size=n_samples,
                         num_symbols=num_symbols,
                        latent_loss_weight=float(FLAGS.latent_loss_weight),
                         dtype_precision=FLAGS.dtype_precision,
                        cell_type=FLAGS.cell, 
                        peephole=FLAGS.peephole,
                        input_keep_prob=float(FLAGS.input_keep_prob),
                        output_keep_prob=float(FLAGS.output_keep_prob),
                      sentiment_feature = string2bool(FLAGS.use_sentiment_feature),
                      use_char2word = string2bool(FLAGS.use_char2word) 
                       )
def zToXdecoded(session,z_sample,s_length):
    x_reconstruct = vrae_model.zToX(session,z_sample,s_length)
    return encoderDecoder.prettyDecode( np.argmax(x_reconstruct[0], axis= 1) )


  reading data line 10000
  reading data line 20000
  reading data line 30000
  reading data line 40000
  reading data line 50000
  reading data line 60000
  reading data line 70000
77977  sentences

The Pessimist Machine


In [153]:
def MachineSays(sess,u,n_sample = 20):
    #print train_dir
    sent = getSentimentScore(u)
    sent_index = 0 # grumpy  
    zz = vrae_model.XToz(sess, *encoderDecoder.encodeForTraining(u),sentiment=getSentimentScore(u))[0]
    res, dist = getNeighbor( [list(zz)] ,n_sample)
    if u.lower() in res:
        res.remove(u.lower())
    out = []
    for uu in sorted(zip(res,list(dist[0])), key=lambda x : getSentimentScore(x[0])[sent_index] , reverse=True):
        out.append(uu[0])
    return out
    #print "\n",dist

In [154]:
answers = []
saver = tf.train.Saver()
with tf.Session(config=config) as sess:
    saver.restore(sess, "./"+training_dir+'/model.ckp')
    for uu in MachineSays(sess,"I like this movie.", 50):
        print uu


INFO:tensorflow:Restoring parameters from ./logs/no_char2word/model.ckp
i hate this movie.
i hate this movie.
i like this movie.
i liked this movies.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i love this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i liked this movie.
i like the movie.
i like it.
i like it.
like this one.
i love this.
i saw this movie on t.
i love this movie a lot.
i liked those movies.

In [152]:
us = [ "I totally loved it.",
"I was really bad.",
"it was terrible.",
"the acting was mostly good.",
"I liked this movie.",
"it was a nice movie.",
"the story was amazing.",
"it was not bad.",
"The acting was good.",
"The music was good.",
]

answers = []
saver = tf.train.Saver()
with tf.Session(config=config) as sess:
    saver.restore(sess, "./"+training_dir+'/model.ckp')
    for u in us:
        answers.append( MachineSays(sess,u)[0] )
df = pd.DataFrame()
df["input"] = us
df["answer"] = answers
df


INFO:tensorflow:Restoring parameters from ./logs/no_char2word/model.ckp
Out[152]:
input answer
0 I totally loved it. i totally hated the movie.
1 I was really bad. it' s really bad.
2 it was terrible. thats terrible.
3 the acting was mostly good. the acting is mostly horrendously bad.
4 I liked this movie. i liked this movie.
5 it was a nice movie. this is a waste of time and money.
6 the story was amazing. the story was bad.
7 it was not bad. it was bad.
8 The acting was good. the irony is horrible.
9 The music was good. the music is horrendous.

In [122]:
print df.to_latex()


\begin{tabular}{lll}
\toprule
{} &                        input &                                  answer \\
\midrule
0 &          I totally loved it. &              i totally hated the movie. \\
1 &            I was really bad. &                       it' s really bad. \\
2 &             it was terrible. &                         thats terrible. \\
3 &  the acting was mostly good. &  the acting is mostly horrendously bad. \\
4 &          I liked this movie. &                     i liked this movie. \\
5 &         it was a nice movie. &      this is a waste of time and money. \\
6 &       the story was amazing. &                      the story was bad. \\
7 &              it was not bad. &                             it was bad. \\
8 &         The acting was good. &                  the irony is horrible. \\
9 &          The music was good. &                the music is horrendous. \\
\bottomrule
\end{tabular}


In [ ]:

neighbordhood


In [125]:
us = [ "I totally loved it.",
"I was really bad.",
"it was terrible.",
"the acting was mostly good.",
"I liked this movie.",
"it was a nice movie.",
"the story was amazing.",
"it was not bad.",
"The acting was good.",
"The music was good.",
]

saver = tf.train.Saver()
df = pd.DataFrame()
with tf.Session(config=config) as sess:
    saver.restore(sess, "./"+training_dir+'/model.ckp')
    #for _ in range(20):
    for u in us:
        k = int(np.random.random() * len(sentences))
        u = encoderDecoder.prettyDecode(sentences[k])
        l = MachineSays(sess,u,20)
        while len(l) < 20:
            l.append("")
        df[u] = l
df


INFO:tensorflow:Restoring parameters from ./logs/no_char2word/model.ckp
Out[125]:
she could blow at any moment! and their chemistry is impeccable. a positive message from a positive film. i was 0 when i saw it. the girl is dying! did they ask the fans? i' m only a child. this film however, is very amusing. it' s like water. the hillermans were a family again.
0 sadly, somebody has lied to frank. were the vietnamese all suicidal? a virus killed 0 billion people. what is a thief? terrible i know. know what else is torture? i' m only a child. the girl' s reactions just seem dumb. it' s lame! the killers are dark with real menace.
1 so well made, no cgi crap. and the violence changes them. impossible to improve or ignore. i cried when i saw this sequence. the gritty' license to kill' did they not watch this mess? i' m only a child. the worse film i have every seen. it' s really a shame. the world is a miserable and hard place.
2 how could a film so bad be made? and the sad thing isinternally she dies. i prefer more vulnerable heroes. it was hot and i was tired. the dialogue is bad. did they inhabit the rv? i' m only a child. this is a horrible film in every way. it' s like hell. the kids are all a mess.
3 the colour is dull and grey. and the car chase is simply amazing. iturbi provides some fine piano playing. i was 0 when i first saw this on tv. this guy is a loser. need that cult cred! i' m only a child. he' s tragic, but also elegant. it' s like a guilty pleasure. the lead actress here is awful.
4 should i be amazed at how bad it is? and the direction? a masterpiece in romance filmmaking. it was a night that i enjoyed. there is no dialog. did they edit out the water? i' m only a child. this film had feeling. it' s like a paperweight. the dialouge was embarrassing at times.
5 the guy looks halfmad anyway. watch the little mermaid instead. a vehicle for michael caine. i was 00 then. this film has no action. and save the day. i' m only a child. this film is forever lodged in my brain. he' s like a child. the shyster lawyer is naturally a man.
6 she could tackle any part. and the dedication page. my family truly enjoyed this movie. i saw ray when it first came out. the dialog was inane. can i get them back? i' m only a child. this movie will be a favorite of mine. it' s like a fetish! the leading actors are a nuisance.
7 should have been a 0! nearly the entire cast is incredible. my favorite country star is from kansas. i gave it 0 stars. the gore is all cgi. do they care if we care? i' m only a child. this film however was mesmerizing. it' s laughable. the wolfgod again, apparently.
8 she could be smart. and dig the lesbian cid agents! a fine piece of captured femininity. i gave it 0 stars. the girl did an okay job. and send her to paris! i' m only a child. this would' ve been a great silent film. it' s like a wireless ad help me! the lead actress flubbed a line?
9 should children be able to enjoy a show? are they speaking spanish? jamie foxx transformed himself into ray. i gave it a 00 rating. the dialog. dance with me? i' m only a child. this film deserves a wide audience. i like' the big screen'. there is hardly any musical score.
10 should i be laughing? and the other is swinging a machete. typical score for eighties action movies. i wasn' t sure that i was getting. the giggling. does he speak, can he speak? i' m only a child. the film was released in 0000. i' ll make this brief. she looks great and has awesome costumes.
11 the wind blows your eyes far away. and the plot is intriguing. a postrevolutionary movie. i was 00. the second is all in good time. and she takes the bait. i' m only a child. there' s a lot visually to like. i' d like that. the locations in europe are awesome 0.
12 the soundtrack also deserves a few words. and there are movies which make lives. fair to mediocre best sums up this film. i couldn' t watch it to the end. the girls in bathing suits. and she needs a better haircut. i' m only a child. this film to me is a very good film! he' s tyler. she loved it and always watched it.
13 should have been titled' balderdash! and the script is a nightmare. it oozes laziness from its every frame. i was told it was prince. the dialogue is turgid. and it does happen in the real world. i' m only a child. this film, however, is a void. this film had neither. should have and x rating!
14 the dvd looks sounds amazing. and the creepy chase begins again. i pictured wendy much differently. it was a big hit when it came out. ted in gilbert, a did they use contacts? i' m only a child. this, however, is altogether different. it' s like that guy. the language is awesome and passionate.
15 this could have been a much better film. and the photography is quiet nice. a must for every family video collection. 0 reared it' s hideous head. he got his kid back. did they use contacts? i' m only a child. this movie was well done in all respects. it' s called taste. she plays a diner waitress.
16 she could do it all. at least the director did his research! basically everything is over the top. we wait with him. that did it. we know that already. i' m only a child. the film works best on a big screen. it' s called taste. the soldiers aren' t realistic.
17 this could have been so much fun! and the change is great. a uwe boll film and positive comments? a woman with a secret. the dialog? did the navy have ltas there? i' m only a child. the film seems rather dated now in 0000. i love salma hayek. she already had traci fingered.
18 this should become a cult favorite. and the soviet trams still in service. emily blunt is perfect as victoria. i saw this 00 years ago on pbs. this film had neither. and the same goes with dave. i' m only a child. this movie feels like a film project. it' s all in vick' s head! thomas gomez is always reliable.
19

In [120]:
print df.to_latex()


\begin{tabular}{lllllllllll}
\toprule
{} &               carla finally gets laid. &           he is gunna be big in hollywood. &      this is one of them. &      warning this could spoil your movie. &                 plot twists? &    this film shows a pirate hero a little! &     yikes! &       disappointed. &         i rate this film capital f minus. &              he should be a star. \\
\midrule
0  &                 cheated on all fronts! &         his fear was because of his guilt. &      this is one of them. &       guess what it took to kill killjoy? &                plain stupid! &      this film is so bad that its painful. &     yikes! &       disappointed. &        i hate this film with a vengeance. &                 he' s flat wrong. \\
1  &          charming film but naive film. &     he is neither a good man or a bad man. &      this is one of them. &    watch this movie forget your troubles. &             plain stupidity? &       this movie was horrible, simply put. &     yikes! &         disappoint. &       although i missed the last episode. &             he should be ashamed. \\
2  &  well and culmination fight full orgy. &  he is driven to find and stop the killer. &      this is one of them. &           what is wrong with some of you? &         the plot was stupid. &      this movie is also very sad at times. &     yikes! &     disappointment. &   a cry in the dark is likewise clinical. &             we should be ashamed. \\
3  &        walker warns pilot about sheba. &     he is uproarious, daring, and moronic. &      this is one of them. &              watch this at your own risk. &          the plot is stupid. &    this film shook me when i first saw it. &     yikes! &      disappointing. &         i rate this movie a very fair 00. &             she looks up in fear. \\
4  &                   randall and fischer? &                    he had no getup and go. &      this is one of them. &         watch this film at your own risk! &          the plot is stupid. &         this film has other flaws as well. &     yikes! &      disappointing. &                   i rate this film a 000. &         he should not be trusted. \\
5  &      raul julia as general santa anna. &    he is a spoiled brat and it bothers me. &      this is one of them. &           watch this with your soul mate. &          the plot is stupid. &     this film should be rated t for tripe. &     yikes! &      disappointing. &                   i rate this film a 000. &       he looks old and lethargic. \\
6  &   was below standards, even for a fan. &              he does great job as riddick. &      this is one of them. &   raisuli what kind of rifle does he use? &           pretty stupid, eh? &     this film came right on time actually. &     yikes! &      disappointing. &                   i rate this film a 000. &            he looks about thirty. \\
7  &      wang lee hom is absolutely great. &             it has dialog with bite in it. &      this is one of them. &                    random shots of dolls. &          flip on the lights? &     this film should have made them stars. &     yikes! &      disappointing. &                 with all the ingredients. &              who is julia to her? \\
8  &              carole landis fares best. &       he' s an adult going to high school. &      this is one of them. &          wait till it comes out on cable. &          pwog fits the bill. &        this film looks so fresh and smart. &     yikes! &       disappointed! &       i saw this title again on shemaroo. &                  he looks around. \\
9  &                 secluded and peaceful. &               so he drives off a building? &  this is not one of them. &      fans will spend tons of money on it. &            blessings to all. &         this movie was absolutely ghastly! &     yikes! &         disturbing. &         i watched this film version of r. &                  he looks around. \\
10 &                   hardly anyone talks. &                it' d rule and you know it. &  this is not one of them. &     watch it with someone you truly love. &       please rent this film. &      this film shows all of that and more. &     yikes! &          delicious. &            all the acting is magnificent. &                 he shouldn' t be. \\
11 &                 screenplay by james l. &             his family owns a local diner. &  this is not one of them. &                 random shots of children. &                    fullstop. &        this film is class pure and simple. &     yikes! &    dir glenn mcquai &                    i rate this movie a 0. &                 she looks upward. \\
12 &                 dallas all over again. &        he is finally bond in a video game! &         this is one of t. &     watch this movie you should enjoy it. &                from the sas? &             she is simply put, his savior. &     yikes! &   dialogue editing. &        i played that back in junior high. &             he wouldn' t, period. \\
13 &               wonderfully atmospheric. &                      he is good in comedy. &     this was one of them. &    characters start to live in the movie. &  plan to return to it at 00. &           she plays a prissy little tramp. &     yikes! &            silence. &  i caught this film late at night on hbo. &                   should be shot. \\
14 &       hydrogen filled flying bladders? &                                         g. &     this is one of those. &       garfield was slimmed down somewhat. &                   flush now. &           this film surprised me a little. &     yikes! &            silence. &                   i gave this film a one. &      this will be somewhat short. \\
15 &   wonderfully acted and superbly cast. &                                         g. &     this is one of those. &           watch this film with your kids. &  plus the dogs are cute too. &           this stuff happens all the time. &  yakitate! &     sidney poitier. &                      i rate this film 00. &              how to tell a story? \\
16 &       stanley yelnats iii henry winkle &                                         g. &     this is one of those. &        giants too, some of us may recall. &        formulaic to the max. &   this movie starts slow, then tapers off. &  yakitate! &  kids flying kites. &               wait till it appears on tv. &         he also paints with love. \\
17 &                    warming and humble. &                        he is a good actor. &     this is one of those. &  and the list of whoppers goes on and on. &  also props to this website! &  the film sags a bit during the third act. &     yikes. &      sidesplitting. &            i caught it last night on tmc. &               he is too talented! \\
18 &                     dallas leads 0000. &               he is good and exudes charm. &     this is one of those. &           all the stunt work is gorgeous. &               plot spoilers! &  this movie was quite a pleasant surprise. &     yikes. &         delusional. &          i gave this movie a rating of 0. &  her two male foils act well too. \\
19 &                                        &                                            &                           &                                           &                              &                                            &            &                     &                                           &                                   \\
\bottomrule
\end{tabular}


In [ ]: