CoolFace
Modelpublic

yassiracharki/Pre-trained_model_Binary_CNN_NLP_Amazon_Reviews

sourceHugging Faceapache-2.0updated 2y agoView on Hugging Face
0likes
Model Card

Model Card for Model ID

Downloads

!pip install contractions !pip install textsearch !pip install tqdm

import nltk nltk.download('punkt')

Fundamental classes

import tensorflow as tf from tensorflow import keras import pandas as pd import numpy as np

Time

import time import datetime

Preprocessing

from tensorflow.keras.preprocessing.text import Tokenizer from tensorflow.keras.preprocessing import sequence from sklearn.preprocessing import LabelEncoder import contractions from bs4 import BeautifulSoup import re import tqdm import unicodedata

seed = 3541 np.random.seed(seed)

Define a dummy loss to bypass the error during model loading

def dummyloss(ytrue, ypred): return tf.reducemean(ypred - ytrue)

Loading the model Trained on Amazon reviews

modelAmazon = keras.models.loadmodel( '/kaggle/input/pre-trained-model-binary-cnn-nlp-amazon-reviews/tensorflow1/pretrainedsentimentanalysiscnnmodelamazonreviews/1/BinaryClassification86AmazonReviews_CNN.h5', compile=False )

Compile the model with the correct loss function and reduction

modelAmazon.compile( optimizer='adam', loss=keras.losses.BinaryCrossentropy(reduction=tf.keras.losses.Reduction.SUMOVERBATCH_SIZE), metrics=['accuracy'] )

Loading Amazon test data

datasettestAmazon = pd.readcsv('/kaggle/input/amazon-reviews-for-sa-binary-negative-positive-csv/amazonreviewsabinary_csv/test.csv')

Loading Amazon train data (to be used on the label encoder)

datasettrainAmazon = pd.readcsv('/kaggle/input/amazon-reviews-for-sa-binary-negative-positive-csv/amazonreviewsabinary_csv/train.csv')

Shuffling the Test Data

testAmazon = datasettestAmazon.sample(frac=1) trainAmazon = datasettrainAmazon.sample(frac=1)

Taking a tiny portion of the database (because it will only be used on the label encoder)

trainAmazon = datasettrain_Amazon.iloc[:100, :]

Taking only necessary columns

ytestAmazon = testAmazon['classindex'].values XtrainAmazon = trainAmazon['reviewtext'].values ytrainAmazon = trainAmazon['classindex'].values

Preprocess corpus function

def preprocesscorpus(corpus): processedcorpus = [] for doc in tqdm.tqdm(corpus): doc = contractions.fix(doc) doc = BeautifulSoup(doc, "html.parser").gettext() doc = unicodedata.normalize('NFKD', doc).encode('ascii', 'ignore').decode('utf-8', 'ignore') doc = re.sub(r'[^a-zA-Z\s]', '', doc, re.I|re.A) doc = doc.lower() doc = doc.strip() processedcorpus.append(doc) return processedcorpus

Preprocessing the Data

XtestAmazon = preprocesscorpus(testAmazon['reviewtext'].values) XtrainAmazon = preprocesscorpus(XtrainAmazon)

Creating and Fitting the Tokenizer

etc ...

More info on the Model's page on Kaggle :

https://www.kaggle.com/models/yacharki/pre-trained-model-binary-cnn-nlp-amazon-reviews