text_plain_output_1.png
from keras.callbacks import EarlyStopping

from sklearn.metrics import accuracy_score, precision_recall_fscore_support

from tensorflow.keras import layers

from tensorflow.keras.layers.experimental.preprocessing import TextVectorization

import numpy as np # linear algebra

import tensorflow as tf

def calculate_results(y_true, y_pred):
    model_accuracy = accuracy_score(y_true, y_pred) * 100
    model_precision, model_recall, model_f1, _ = precision_recall_fscore_support(y_true, y_pred, average='weighted')
    model_results = {'accuracy': model_accuracy, 'precision': model_precision, 'recall': model_recall, 'f1': model_f1}
    return model_results

sent_lens = [len(sentence.split()) for sentence in train_sentences]
sent_lens[:10]

avg_len = np.mean(sent_lens)
avg_len

out_len_seq = np.percentile(sent_lens, 95)
out_len_seq

max_tokens = 65000
output_sequence_length = int(out_len_seq)

text_vectorizer = TextVectorization(max_tokens=max_tokens, output_sequence_length=output_sequence_length)

text_vectorizer.adapt(train_sentences)

news_vocab = text_vectorizer.get_vocabulary()
(print(f'Number of words in vocabulary: {len(news_vocab)}'),)

embedding = layers.Embedding(input_dim=max_tokens, output_dim=128, input_length=out_len_seq, embeddings_initializer='uniform')
embedding

inputs = layers.Input(shape=(1,), dtype=tf.string)
x = text_vectorizer(inputs)
x = embedding(x)
x = layers.Conv1D(filters=64, kernel_size=5, strides=1, activation='relu', padding='valid')(x)
x = layers.GlobalMaxPooling1D()(x)
outputs = layers.Dense(1, activation='sigmoid')(x)
model_2 = tf.keras.Model(inputs, outputs, name='conv1D_model')

early_stopping = EarlyStopping(monitor='val_loss', patience=5)

model_2.compile(loss='binary_crossentropy', optimizer=tf.keras.optimizers.Adam(), metrics=['accuracy'])

model_2_history = model_2.fit(train_sentences, train_label, validation_data=(test_sentences, test_label), batch_size=64, epochs=100, verbose=1, callbacks=[early_stopping])

model_2.summary()

model_2.evaluate(test_sentences, test_label)

model_2_probs = model_2.predict(test_sentences)
model_2_probs[:10]

model_2_preds = tf.squeeze(tf.round(model_2_probs))
model_2_preds[:20]

model_2_results = calculate_results(y_true=test_label, y_pred=model_2_preds)
model_2_results