<jupyter_start><jupyter_text>Problem Statement We are given a dataset consisting of two csv files train_bodies.csv which contains the set of news articles bodies,while train-stances.csv resembles the articles for each of these bodies being identified using the body id.After training from these samples we need to detect whether the given headline agrees,disagrees,discusses,unrelated with the body id  Importing Libraries<jupyter_code>import pandas as pd
import numpy as np
import os
from keras.preprocessing.sequence import pad_sequences
from keras.preprocessing.text import Tokenizer
from keras.utils import to_categorical,plot_model

from keras.models import Input,Model,Sequential
from keras.layers import LSTM,Embedding,Dropout,Activation,Reshape,Dense,GRU,Add,Flatten,concatenate

from keras.preprocessing.text import one_hot
from keras.preprocessing.sequence import pad_sequences<jupyter_output><empty_output><jupyter_text>Dataset Preparation<jupyter_code>DATASET_PATH = "/home/abhinav/fake_news_challenge/fake_news_challenge/dataset"

train_bodies = pd.read_csv(os.path.join(DATASET_PATH,'train_bodies.csv'))
# train_bodies.head()
train_stance = pd.read_csv(os.path.join(DATASET_PATH,'train_stances.csv'))
# #Run commented code to combine the two csv file{train_bodies.csv,train_stances.csv} into data_combined.csv file
# from tqdm.notebook import tqdm
# count=0
# for i in tqdm(range(train_stance.shape[0])):
#     for j in range(train_bodies.shape[0]):
#         if train_bodies.loc[j,'Body ID']==train_stance.loc[i,'Body ID']:
#             train_stance.loc[i,'articleBody'] = train_bodies.loc[j,'articleBody']


# train_stance.to_csv('data_combined.csv',index=False)
data = pd.read_csv('data_combined.csv')#generated from Fake News stanford.ipynb
data.head()
data['stance_cat'] = data['Stance'].map({'agree':0,'disagree':1,'discuss':2,'unrelated':3}).astype(int)
data['Stance'].value_counts()
corpus = np.r_[data['Headline'].values,data['articleBody'].values]
print(49972*2)
print(len(corpus)) # first 49972 contains the Headline and next 49972 contains the articleBody

vocabulary = []
for sentence in corpus:
    vocabulary.extend(sentence.split(' '))

vocabulary = list(set(vocabulary))
vocab_length = len(vocabulary)
print("Vocabulary Length is {0}".format(vocab_length))

max_features = 5000
max_nb_words = 24000
EMBEDDING_DIM = 50
MAX_SEQUENCE_LENGTH_HEADLINE = 64
MAX_SEQUENCE_LENGTH_BODY = 64<jupyter_output><empty_output><jupyter_text>BASELINE - ONE HOT ENCODING<jupyter_code>encoded_docs_headline = [one_hot(sentence,vocab_length) for sentence in data.loc[:,'Headline'].tolist()]
padded_docs_headline = pad_sequences(encoded_docs_headline,MAX_SEQUENCE_LENGTH_HEADLINE,padding='post')

encoded_docs_body = [one_hot(sentence,vocab_length) for sentence in data.loc[:,'articleBody'].tolist()]
padded_docs_body = pad_sequences(encoded_docs_body,MAX_SEQUENCE_LENGTH_BODY,padding='post')


labels = to_categorical(data.loc[:,'stance_cat'])

padded_docs_headline_train = padded_docs_headline[:int(len(padded_docs_headline)*0.8),:]
padded_docs_headline_test = padded_docs_headline[int(len(padded_docs_headline)*0.8):,:]

padded_docs_body_train = padded_docs_body[:int(len(padded_docs_body)*0.8),:]
padded_docs_body_test = padded_docs_body[int(len(padded_docs_body)*0.8):,:]

labels_train = labels[:int(len(labels)*0.8),:]
labels_test = labels[int(len(labels)*0.8):,:]<jupyter_output><empty_output><jupyter_text>Defining Model Architecture<jupyter_code>input_headline = Input(shape=[64],name='input_headline')
embedding_headline = Embedding(vocab_length,50,input_length = MAX_SEQUENCE_LENGTH_HEADLINE)(input_headline)
# dense_headline = Dense(16,activation='relu')(embedding_headline)

input_body = Input(shape=[64],name='input_body')
embedding_body = Embedding(vocab_length,50,input_length = MAX_SEQUENCE_LENGTH_BODY)(input_body)
# dense_body = Dense(16,activation='relu')(embedding_body)

addition_layer = concatenate([embedding_headline,embedding_body])
lstm = LSTM(units=64)(addition_layer)
# drop = Dropout(0.25)(lstm)

# flatten = Flatten()(addition_layer)
output = Dense(4,activation='sigmoid')(lstm)

model_combined = Model(inputs=[input_headline,input_body],outputs=output)

model_combined.compile(optimizer = 'adam',loss ='categorical_crossentropy',metrics = ['accuracy'])<jupyter_output><empty_output><jupyter_text>Model Architecture<jupyter_code>model_combined.summary()
plot_model(model_combined, to_file='model_one_hot.png', show_shapes=True, show_layer_names=True)<jupyter_output><empty_output><jupyter_text>Model Training<jupyter_code>model_combined.fit([padded_docs_headline_train,padded_docs_body_train],labels_train,epochs=15,verbose=1,validation_data=([padded_docs_headline_test,padded_docs_body_test],labels_test))<jupyter_output>Epoch 1/15
1250/1250 [==============================] - 82s 64ms/step - loss: 0.7372 - accuracy: 0.7495 - val_loss: 0.5413 - val_accuracy: 0.7983
Epoch 2/15
1250/1250 [==============================] - 76s 61ms/step - loss: 0.5119 - accuracy: 0.8139 - val_loss: 0.4628 - val_accuracy: 0.8412
Epoch 3/15
1250/1250 [==============================] - 75s 60ms/step - loss: 0.4267 - accuracy: 0.8523 - val_loss: 0.4050 - val_accuracy: 0.8593
Epoch 4/15
1250/1250 [==============================] - 74s 59ms/step - loss: 0.3511 - accuracy: 0.8735 - val_loss: 0.3731 - val_accuracy: 0.8683
Epoch 5/15
1250/1250 [==============================] - 74s 59ms/step - loss: 0.2998 - accuracy: 0.8891 - val_loss: 0.3548 - val_accuracy: 0.8749
Epoch 6/15
1250/1250 [==============================] - 74s 59ms/step - loss: 0.2525 - accuracy: 0.9073 - val_loss: 0.3255 - val_accuracy: 0.8916
Epoch 7/15
1250/1250 [==============================] - 72s 58ms/step - loss: 0.2142 - accuracy: 0.9198 - val_loss: 0.3358 -[...]
{'fraction_non_alphanumeric': 0.15739296238516207, 'fraction_numerical': 0.05408216328653146, 'mean_word_length': 7.973561430793157, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '262206', 'license': 'MIT', 'n_tokens_mistral': 2291, 'n_tokens_neox': 2043, 'n_words': 337, 'path': 'code/FNC One Hot Encoding+LSTM.ipynb', 'repo_name': 'Abhinav1004/Fake-News-Stance-Detection', 'size': '5769'}