<jupyter_start><jupyter_text>Implement Tweet2Vec from Dhingra et al. Ensure to install graphviz: https://www.graphviz.org/For, ubuntu install using "sudo apt-get install graphviz"<jupyter_code>%matplotlib inline
import numpy as np
np.random.seed(13)
from keras.models import Model
from keras.layers import Dense, Embedding, GRU, Input, Bidirectional
from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
from keras.regularizers import l2
from keras.utils import np_utils
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
from matplotlib import pyplot as plt
plt.style.use("ggplot")
#tweets.tsv is in the format of <tweet text><tab><hashtag_id> where hashtag_id is a unique id given to each hashtag

tweets = []
labels = []
with open("./../data/tweets.tsv") as f:
    for l in f:
        tweet, label = l.strip().split("\t")
        tweets.append(" ".join(list(tweet)))
        labels.append(int(label))

maxlen = 140

tokenizer = Tokenizer(filters="")
tokenizer.fit_on_texts(tweets)
X_train = tokenizer.texts_to_sequences(tweets)
X_train = sequence.pad_sequences(X_train, maxlen=maxlen)
#Y_train = np_utils.to_categorical(labels, len(set(labels)))
Y_train = np_utils.to_categorical(labels, len(set(labels)))
V = len(tokenizer.word_index) + 1
Y_train.shape
l2_coef = 0.001
tweet = Input(shape=(maxlen,), dtype='int32')
x = Embedding(V, 128, input_length=maxlen, W_regularizer=l2(l=l2_coef))(tweet)
x = Bidirectional(layer=GRU(128, return_sequences=False, 
                            W_regularizer=l2(l=l2_coef),
                            b_regularizer=l2(l=l2_coef),
                            U_regularizer=l2(l=l2_coef)),
                  merge_mode='sum')(x)
x = Dense(len(set(labels)), W_regularizer=l2(l=l2_coef), activation="softmax")(x)

tweet2vec = Model(inputs=tweet, outputs=x)

tweet2vec.compile(loss='categorical_crossentropy',
                  optimizer='RMSprop',
                  metrics=['accuracy'])

SVG(model_to_dot(tweet2vec, show_shapes=True).create(prog='dot', format='svg'))
tweet2vec.fit(X_train, Y_train, epochs=10, batch_size=32, validation_split=0.1)<jupyter_output>Train on 2 samples, validate on 1 samples
Epoch 1/10
2/2 [==============================] - 1s 663ms/step - loss: 1.7800 - acc: 0.0000e+00 - val_loss: 1.9400 - val_acc: 0.0000e+00
Epoch 2/10
2/2 [==============================] - 0s 52ms/step - loss: 1.5549 - acc: 1.0000 - val_loss: 2.1600 - val_acc: 0.0000e+00
Epoch 3/10
2/2 [==============================] - 0s 56ms/step - loss: 1.4028 - acc: 1.0000 - val_loss: 2.4700 - val_acc: 0.0000e+00
Epoch 4/10
2/2 [==============================] - 0s 56ms/step - loss: 1.2605 - acc: 1.0000 - val_loss: 2.9268 - val_acc: 0.0000e+00
Epoch 5/10
2/2 [==============================] - 0s 58ms/step - loss: 1.1209 - acc: 1.0000 - val_loss: 3.6293 - val_acc: 0.0000e+00
Epoch 6/10
2/2 [==============================] - 0s 60ms/step - loss: 0.9844 - acc: 1.0000 - val_loss: 4.7528 - val_acc: 0.0000e+00
Epoch 7/10
2/2 [==============================] - 0s 58ms/step - loss: 0.8541 - acc: 1.0000 - val_loss: 6.1632 - val_acc: 0.0000e+00
Epoch 8/10
2/2 [======[...]
{'fraction_non_alphanumeric': 0.19703189137985475, 'fraction_numerical': 0.08241237764445848, 'mean_word_length': 4.044585987261146, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '244238', 'license': 'MIT', 'n_tokens_mistral': 1369, 'n_tokens_neox': 1176, 'n_words': 257, 'path': 'module4/Tweet2Vec.ipynb', 'repo_name': 'anujgupta82/NLP_Bootcamp', 'size': '3167'}