<jupyter_start><jupyter_text>Digikala comments classification with  RNN - LSTMComments classification  to find out customers recommend them or not<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
import scipy
import tensorflow as tf

#splitting dataset
from sklearn.model_selection import train_test_split

# Building sequential model
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv1D, MaxPooling1D, Embedding, LSTM, Bidirectional

# Word vectorization 
from tensorflow.keras.layers.experimental.preprocessing import TextVectorization

# Avoiding warnings
import warnings
import os
########### Prevent Warnings ###########
warnings.filterwarnings(action='ignore')
########### Prevent Warnings ###########
gpus = tf.config.list_physical_devices('GPU')
gpu = gpus[0]
os.environ["TF_FORCE_GPU_ALLOW_GROWTH"]="true"
tf.config.experimental.set_memory_growth(gpu, True)<jupyter_output><empty_output><jupyter_text>Importing data<jupyter_code>df = pd.read_csv('train.csv', index_col=[0])
df.head()<jupyter_output><empty_output><jupyter_text>Creating train and test datasets<jupyter_code>text = df['clean_comment'].astype(str).to_numpy()
X_train, X_test, Y_train, Y_test = train_test_split(text, df['recommend'], test_size=0.3)
# print(X_train)
X_train = [text for text in X_train]
Y_train = [label for label in Y_train]

X_test = [text for text in X_test]
Y_test = [label for label in Y_test]
X_train, X_test, Y_train, Y_test = np.array(X_train), np.array(X_test), np.array(Y_train), np.array(Y_test)<jupyter_output><empty_output><jupyter_text>Tokenizing and vectorizing data<jupyter_code>max_features = 10000
sequence_length = 500

vectorize_layer = TextVectorization(
    max_tokens=max_features,
    output_mode='int',
    output_sequence_length=sequence_length)
vectorize_layer.adapt(text)
X_train = vectorize_layer(X_train)
X_test = vectorize_layer(X_test)
X_test<jupyter_output><empty_output><jupyter_text>Creating the model<jupyter_code>embedding_dim = 64
max_length = 16
epochs = 4
batch_size = 256


model = tf.keras.Sequential([
    Embedding(
        input_dim=max_features,
        output_dim=embedding_dim,
        # Use masking to handle the variable sequence lengths
        mask_zero=True),
    Bidirectional(LSTM(64,  return_sequences=True)),

    Bidirectional(LSTM(32)),
    Dropout(0.2),
    
    Dense(64, activation='relu'),
    Dropout(0.2),
    
    Dense(1,  activation='sigmoid')
])

model.compile(loss='binary_crossentropy',optimizer='adam',metrics=['accuracy'])<jupyter_output><empty_output><jupyter_text>Training the model<jupyter_code>history = model.fit(X_train, Y_train, batch_size=batch_size, epochs=epochs, verbose=2, validation_data=(X_test, Y_test)) 
plt.plot(history.history['loss'], label='train')
plt.plot(history.history['val_loss'], label='test')
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>To confirm that this works as expected, evaluating some sentences:<jupyter_code>model.predict(vectorize_layer(['خیلی بد بود']))
model.predict(vectorize_layer(['افتضاح بود']))
model.predict(vectorize_layer(['عالی بود']))
model.predict(vectorize_layer(['من از این محصول رضایت دارم']))
model.predict(vectorize_layer(['با این قیمت ارزش خرید ندارد']))
model.predict(vectorize_layer(['با این قیمت ارزش خرید دارد']))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12399403874813711, 'fraction_numerical': 0.010134128166915052, 'mean_word_length': 7.369077306733167, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, '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': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '291312', 'license': 'MIT', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1184, 'n_words': 226, 'path': 'RNN.ipynb', 'repo_name': 'srahnama/Digikala-Comments-Analysis', 'size': '3355'}