{"id":25164355,"url":"https://github.com/ozzy-zy/ai_project_1_sentimentanalysis","last_synced_at":"2026-03-07T18:31:14.962Z","repository":{"id":269867446,"uuid":"901523544","full_name":"Ozzy-ZY/Ai_project_1_SentimentAnalysis","owner":"Ozzy-ZY","description":"a small Sentiment analysis Deep learning model that classifies movies Reviews either Positive or Negative using neural networks layers","archived":false,"fork":false,"pushed_at":"2025-05-01T22:55:34.000Z","size":40850,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-06-20T06:41:29.661Z","etag":null,"topics":["ai","classification-model","deep-learning","neural-networks","nlp","sentiment-analysis","tensorflow"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Ozzy-ZY.png","metadata":{"files":{"readme":"Readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-12-10T20:18:25.000Z","updated_at":"2025-05-01T22:55:37.000Z","dependencies_parsed_at":"2025-06-20T06:47:35.130Z","dependency_job_id":null,"html_url":"https://github.com/Ozzy-ZY/Ai_project_1_SentimentAnalysis","commit_stats":null,"previous_names":["ozzy-zy/ai_project_1_sentimentanalysis"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/Ozzy-ZY/Ai_project_1_SentimentAnalysis","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ozzy-ZY%2FAi_project_1_SentimentAnalysis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ozzy-ZY%2FAi_project_1_SentimentAnalysis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ozzy-ZY%2FAi_project_1_SentimentAnalysis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ozzy-ZY%2FAi_project_1_SentimentAnalysis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Ozzy-ZY","download_url":"https://codeload.github.com/Ozzy-ZY/Ai_project_1_SentimentAnalysis/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Ozzy-ZY%2FAi_project_1_SentimentAnalysis/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30226245,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T18:12:09.766Z","status":"ssl_error","status_checked_at":"2026-03-07T18:11:58.786Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ai","classification-model","deep-learning","neural-networks","nlp","sentiment-analysis","tensorflow"],"created_at":"2025-02-09T04:30:06.203Z","updated_at":"2026-03-07T18:31:14.702Z","avatar_url":"https://github.com/Ozzy-ZY.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Deep Learning Sentiment Analysis Model: A Comprehensive Explanation\n\n## Introduction to the System\n\nThis code implements a sophisticated deep learning model for sentiment analysis, specifically designed to analyze movie reviews from the IMDB dataset and determine whether they express positive or negative sentiment. The system combines several modern deep learning techniques, including word embeddings, bidirectional LSTMs, and various optimization strategies.\n\n## Library Imports and Their Purpose\n\nThe code begins with importing necessary libraries:\n\n```python\nimport tensorflow as tf\nfrom tensorflow.keras.models import Sequential\nfrom tensorflow.keras.layers import Embedding, LSTM, Dense, Dropout, Bidirectional\n```\n\nTensorFlow is the primary deep learning framework we're using. It provides both low-level operations and high-level APIs through Keras. The Sequential model allows us to build our neural network layer by layer, while the imported layers serve specific purposes:\n- Embedding: Converts words to dense vectors\n- LSTM: Processes sequences of data\n- Dense: Creates fully connected neural network layers\n- Dropout: Helps prevent overfitting\n- Bidirectional: Allows layers to process sequences in both directions\n\n```python\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\n```\n\nThese imports handle the training process:\n- Adam is an adaptive optimization algorithm\n- EarlyStopping prevents overfitting by monitoring training progress\n- ReduceLROnPlateau adjusts the learning rate during training\n\n```python\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences\nfrom tensorflow.keras.preprocessing.text import Tokenizer\nfrom sklearn.model_selection import train_test_split\nimport numpy as np\nimport pandas as pd\nimport pickle\n```\n\nThese utilities handle data preprocessing:\n- pad_sequences standardizes text length\n- Tokenizer converts text to numbers\n- train_test_split divides data into training and testing sets\n- numpy handles numerical operations\n- pandas manages data structures\n- pickle saves objects for later use\n\n## System Setup and GPU Detection\n\n```python\nprint(\"Num GPUs Available: \", len(tf.config.list_physical_devices('GPU')))\nprint(\"TensorFlow Version: \", tf.__version__)\n```\n\nThis code checks for available GPUs and displays the TensorFlow version. GPUs can dramatically speed up training by processing many calculations in parallel. Modern deep learning typically requires GPU acceleration for practical training times.\n\n## Data Loading and Preprocessing\n\n```python\ndata = pd.read_csv('IMDB Dataset.csv')\n```\n\nThe dataset is loaded from a CSV file. The IMDB dataset contains movie reviews labeled as positive or negative, making it perfect for binary sentiment classification.\n\n```python\ntexts = data['review'].values\nlabels = data['sentiment'].replace(['positive', 'negative'], [1, 0]).values\n```\n\nThis code separates the reviews (features) from their sentiment labels (targets). The labels are converted from text ('positive'/'negative') to binary values (1/0) for machine learning purposes.\n\n## Text Preprocessing Configuration\n\n```python\nmax_vocab_size = 20000\nmax_sequence_length = 150\n```\n\nThese parameters define crucial constraints:\n- max_vocab_size limits our vocabulary to the 20,000 most frequent words, balancing between coverage and computational efficiency\n- max_sequence_length standardizes all reviews to 150 words, either truncating longer reviews or padding shorter ones\n\n## Tokenization Process\n\n```python\ntokenizer = Tokenizer(num_words=max_vocab_size, oov_token=\"\u003cOOV\u003e\")\ntokenizer.fit_on_texts(texts)\n```\n\nThe Tokenizer converts text to numbers by:\n1. Creating a vocabulary from the training texts\n2. Assigning unique indices to each word\n3. Using \"\u003cOOV\u003e\" (Out Of Vocabulary) to handle unknown words\n\n```python\nsequences = tokenizer.texts_to_sequences(texts)\npadded_sequences = pad_sequences(sequences, maxlen=max_sequence_length, padding='post')\n```\n\nThis converts the text reviews into numerical sequences:\n1. Each word is replaced by its vocabulary index\n2. Sequences are padded to ensure uniform length\n3. Padding is added at the end ('post') of shorter sequences\n\n## Tokenizer Persistence\n\n```python\nwith open('the_better_tokenizer.pickle', 'wb') as handle:\n    pickle.dump(tokenizer, handle, protocol=pickle.HIGHEST_PROTOCOL)\n```\n\nThe tokenizer is saved to disk so it can be used later to process new reviews consistently. This is crucial for deployment, as any new text must be processed exactly like the training data.\n\n## Data Splitting\n\n```python\nX_train, X_test, y_train, y_test = train_test_split(\n    padded_sequences, labels, test_size=0.2, random_state=42\n)\n```\n\nThe data is split into training (80%) and testing (20%) sets. The random_state ensures reproducibility by using the same random split every time.\n\n## Word Embeddings\n\n```python\nembedding_dim = 50\n```\n\nThis sets the dimensionality of our word vectors to match the pre-trained GloVe embeddings we'll use.\n\n```python\nembedding_index = {}\nwith open('glove.6B.50d.txt', encoding='utf-8') as f:\n    for line in f:\n        values = line.split()\n        word = values[0]\n        coefficients = np.array(values[1:], dtype='float32')\n        embedding_index[word] = coefficients\n```\n\nThis loads pre-trained GloVe word embeddings, which capture semantic relationships between words based on their usage patterns in a large corpus of text. Each word is represented by a 50-dimensional vector.\n\n```python\nword_index = tokenizer.word_index\nembedding_matrix = np.zeros((max_vocab_size, embedding_dim))\n\nfor word, i in word_index.items():\n    if i \u003c max_vocab_size:\n        embedding_vector = embedding_index.get(word)\n        if embedding_vector is not None:\n            embedding_matrix[i] = embedding_vector\n```\n\nThis creates an embedding matrix for our vocabulary:\n1. Initialize an empty matrix of size (vocab_size × embedding_dim)\n2. For each word in our vocabulary, find its pre-trained embedding\n3. Copy the embedding vector into our matrix at the word's index\n\n## Model Architecture\n\nThe model is built as a sequential stack of layers:\n\n```python\nmodel = Sequential([\n    Embedding(max_vocab_size, embedding_dim,\n              input_length=max_sequence_length,\n              weights=[embedding_matrix],\n              trainable=False),\n```\n\nThe Embedding layer:\n- Converts word indices to dense vectors\n- Is initialized with our pre-trained embeddings\n- Keeps embeddings fixed during training (trainable=False)\n- Expects sequences of length max_sequence_length\n\n```python\n    Bidirectional(LSTM(128, return_sequences=True)),\n```\n\nFirst LSTM layer:\n- Uses 128 units to learn sequence patterns\n- Is bidirectional to process text in both directions\n- Returns full sequences for the next layer\n- Allows the model to capture long-range dependencies\n\n```python\n    Dropout(0.3),\n```\n\nFirst Dropout layer:\n- Randomly deactivates 30% of connections during training\n- Prevents overfitting by forcing redundant learning\n- Makes the model more robust\n\n```python\n    Bidirectional(LSTM(64)),\n```\n\nSecond LSTM layer:\n- Uses 64 units (smaller than first layer)\n- Creates a \"funnel\" architecture\n- Outputs only the final state\n- Further processes the sequence information\n\n```python\n    Dense(32, activation='relu'),\n```\n\nDense layer:\n- Fully connected layer with 32 neurons\n- Uses ReLU activation for non-linearity\n- Helps in final feature processing\n\n```python\n    Dropout(0.3),\n```\n\nSecond Dropout layer:\n- Provides additional regularization\n- Prevents overfitting in the dense layers\n\n```python\n    Dense(1, activation='sigmoid')\n])\n```\n\nOutput layer:\n- Single neuron for binary classification\n- Sigmoid activation outputs probability between 0 and 1\n- Above 0.5 indicates positive sentiment, below 0.5 negative\n\n## Model Compilation\n\n```python\nmodel.compile(optimizer=Adam(learning_rate=0.001),\n             loss='binary_crossentropy',\n             metrics=['accuracy'])\n```\n\nThis configures the training process:\n- Adam optimizer adapts learning rates for each parameter\n- Binary crossentropy is the standard loss function for binary classification\n- Accuracy metric provides intuitive performance measurement\n\n## Training Callbacks\n\n```python\nearly_stopping = EarlyStopping(\n    monitor='val_loss',\n    patience=3,\n    restore_best_weights=True\n)\n```\n\nEarly stopping prevents overfitting:\n- Monitors validation loss\n- Stops if no improvement for 3 epochs\n- Restores the best weights found\n\n```python\nreduce_lr = ReduceLROnPlateau(\n    monitor='val_loss',\n    factor=0.5,\n    patience=2\n)\n```\n\nLearning rate reduction:\n- Monitors validation loss\n- Halves learning rate if no improvement for 2 epochs\n- Helps fine-tune the model\n\n## Model Training\n\n```python\nepochs = 15\nbatch_size = 64\nhistory = model.fit(\n    X_train, y_train,\n    validation_data=(X_test, y_test),\n    epochs=epochs,\n    batch_size=batch_size,\n    callbacks=[early_stopping, reduce_lr]\n)\n```\n\nThe training process:\n- Runs for up to 15 epochs\n- Processes data in batches of 64 samples\n- Uses validation data to monitor progress\n- Applies early stopping and learning rate reduction\n- Returns training history for analysis\n\n## Model Persistence and Evaluation\n\n```python\nmodel.save(\"A_better_model.h5\")\n```\n\nSaves the entire model:\n- Architecture\n- Weights\n- Training configuration\n- Optimizer state\n\n```python\nloss, accuracy = model.evaluate(X_test, y_test)\nprint(f\"Test Accuracy: {accuracy * 100:.2f}%\")\n```\n\nFinal evaluation:\n- Tests model on held-out test data\n- Provides unbiased performance estimate\n- Reports accuracy as percentage\n\n## Practical Usage\n\nThis model can be used to analyze the sentiment of new movie reviews by:\n1. Loading the saved tokenizer\n2. Processing new text using the same tokenization steps\n3. Loading the saved model\n4. Getting predictions for the processed text\n\nThe system is designed to be both accurate and practical, with careful attention to preventing overfitting and ensuring consistent preprocessing for new data.\n## DataSet Used \n[Kaggle.com](https://www.kaggle.com/datasets/lakshmi25npathi/imdb-dataset-of-50k-movie-reviews)\n## Word Embedding Link\n[Kaggle.com](https://www.kaggle.com/datasets/adityajn105/glove6b50d)\n## References\n- [Embedding](https://keras.io/api/layers/core_layers/embedding/)\n- [Bidirectional LSTM \"Long Short Term Memory\"](https://www.geeksforgeeks.org/bidirectional-lstm-in-nlp/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fozzy-zy%2Fai_project_1_sentimentanalysis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fozzy-zy%2Fai_project_1_sentimentanalysis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fozzy-zy%2Fai_project_1_sentimentanalysis/lists"}