<jupyter_start><jupyter_text>Importing Libraries<jupyter_code>import tensorflow as tf
import tensorflow_hub as hub
import pandas as pd
import keras
import numpy as np
from keras.layers import Input, Lambda, Dense,Dropout,SpatialDropout1D,Conv1D,GlobalMaxPooling1D
from keras.models import Model
import keras.backend as K
from keras.preprocessing import text, sequence
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.engine.topology import Layer
from keras import initializers, regularizers, constraints
from keras.layers import *
from sklearn.model_selection import train_test_split<jupyter_output>Using TensorFlow backend.<jupyter_text>Loading data<jupyter_code>dataset = pd.read_csv("fake_real_dataset.csv")
dataset.head()
X = dataset["title"].values
Y = dataset["label"].values
X_train,X_test,y_train,y_test = train_test_split(X,Y,test_size=0.2)
#new data
fake = pd.read_csv("gossipcop_fake.csv")
real = pd.read_csv("gossipcop_real.csv")
fake_t = fake.title
real_t = real.title<jupyter_output><empty_output><jupyter_text>Making Model<jupyter_code>#url = "https://tfhub.dev/google/universal-sentence-encoder-large/3"
#embed = hub.text_embedding_column(key='sentence',module_spec=url,)
embedding_feature = hub.text_embedding_column(
    key='sentence', 
    module_spec="https://tfhub.dev/google/universal-sentence-encoder-large/3",
    trainable=False)
#creating custom layer
def UniversalEmbedding(x):
    return embedding_feature(tf.squeeze(tf.cast(x,tf.string)))
#creating a Attention layer
class Attention(Layer):
    def __init__(self, step_dim,
                 W_regularizer=None, b_regularizer=None,
                 W_constraint=None, b_constraint=None,
                 bias=True, **kwargs):
        self.supports_masking = True
        self.init = initializers.get('glorot_uniform')

        self.W_regularizer = regularizers.get(W_regularizer)
        self.b_regularizer = regularizers.get(b_regularizer)

        self.W_constraint = constraints.get(W_constraint)
        self.b_constraint = constraints.get(b_constraint)

        self.bias = bias
        self.step_dim = step_dim
        self.features_dim = 0
        super(Attention, self).__init__(**kwargs)

    def build(self, input_shape):
        assert len(input_shape) == 3

        self.W = self.add_weight((input_shape[-1],),
                                 initializer=self.init,
                                 name='{}_W'.format(self.name),
                                 regularizer=self.W_regularizer,
                                 constraint=self.W_constraint)
        self.features_dim = input_shape[-1]

        if self.bias:
            self.b = self.add_weight((input_shape[1],),
                                     initializer='zero',
                                     name='{}_b'.format(self.name),
                                     regularizer=self.b_regularizer,
                                     constraint=self.b_constraint)
        else:
            self.b = None
        self.built = True

    def compute_mask(self, input, input_mask=None):
        return None

    def call(self, x, mask=None):
        features_dim = self.features_dim
        step_dim = self.step_dim
        eij = K.reshape(K.dot(K.reshape(x, (-1, features_dim)),
                        K.reshape(self.W, (features_dim, 1))), (-1, step_dim))
        if self.bias:
            eij += self.b
        eij = K.tanh(eij)
        a = K.exp(eij)
        if mask is not None:
            a *= K.cast(mask, K.floatx())
        a /= K.cast(K.sum(a, axis=1, keepdims=True) + K.epsilon(), K.floatx())
        a = K.expand_dims(a)
        weighted_input = x * a
        return K.sum(weighted_input, axis=1)

    def compute_output_shape(self, input_shape):
        return input_shape[0],  self.features_dim
#making the model using the functional keras api
input_text = Input(shape=(1,),dtype=tf.string)
embedding_feature = Lambda(UniversalEmbedding,output_shape=(512,))(input_text)
drop_1 = Dropout(0.2)(embedding)
#cnn_1 = Conv1D(256,3,padding='valid',activation='relu',strides=1)(drop_1)
#max_pool = GlobalMaxPooling1D()(cnn_1)
#att_1 = Attention()(max_pool)
dense = Dense(128, activation='relu')(drop_1)
drop_2 = Dropout(0.5)(dense)
pred = Dense(1, activation='softmax')(drop_2)
model = Model(inputs=[input_text], outputs=pred)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
with tf.Session() as session:
    K.set_session(session)
    session.run(tf.global_variables_initializer())  
    session.run(tf.tables_initializer())
    history = model.fit(X, Y, epochs=1, batch_size=32,validation_split=0.2)
    model.save_weights('./model.h5')
#using tf estimator
# Training input on the whole training set with no limit on training epochs.
train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {'sentence': X_train}, y_train, 
    batch_size=256, num_epochs=None, shuffle=True)
    
# Prediction on the whole training set.
predict_train_input_fn = tf.estimator.inputs.numpy_input_fn(
    {'sentence': X_train}, y_train, shuffle=False)
    
# Prediction on the whole validation set.
predict_val_input_fn = tf.estimator.inputs.numpy_input_fn(
    {'sentence': X_test}, y_test, shuffle=False)
    
# Prediction on the test set.
predict_test_input_fn = tf.estimator.inputs.numpy_input_fn(
    {'sentence': X_test}, y_test, shuffle=False)
dnn = tf.estimator.DNNClassifier(
          hidden_units=[512, 128],
          feature_columns=[embedding_feature],
          n_classes=2,
          activation_fn=tf.nn.relu,
          dropout=0.1,
          optimizer=tf.train.AdagradOptimizer(learning_rate=0.005))

tf.logging.set_verbosity(tf.logging.ERROR)
import time

TOTAL_STEPS = 1500
STEP_SIZE = 100
for step in range(0, TOTAL_STEPS+1, STEP_SIZE):
    print()
    print('-'*100)
    print('Training for step =', step)
    start_time = time.time()
    dnn.train(input_fn=train_input_fn, steps=STEP_SIZE)
    elapsed_time = time.time() - start_time
    print('Train Time (s):', elapsed_time)
    print('Eval Metrics (Train):', dnn.evaluate(input_fn=predict_train_input_fn))
    print('Eval Metrics (Validation):', dnn.evaluate(input_fn=predict_val_input_fn))<jupyter_output>----------------------------------------------------------------------------------------------------
Training for step = 0
{'fraction_non_alphanumeric': 0.1302369370782991, 'fraction_numerical': 0.012552957790679428, 'mean_word_length': 3.138961038961039, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '646432', 'license': 'MIT', 'n_tokens_mistral': 2127, 'n_tokens_neox': 2041, 'n_words': 379, 'path': 'model_protos/USE+Attention.ipynb', 'repo_name': 'ANUBHAVNATANI/FakeNewsDetection', 'size': '6373'}