https://github.com/hatim001/guess-me-with-neural-networks

https://github.com/hatim001/guess-me-with-neural-networks

Science Score: 13.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
  • DOI references
  • Academic publication links
  • Academic email domains
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.1%) to scientific vocabulary
Last synced: 8 months ago · JSON representation

Repository

Basic Info
  • Host: GitHub
  • Owner: Hatim001
  • Language: Python
  • Default Branch: main
  • Size: 2.27 MB
Statistics
  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • Open Issues: 0
  • Releases: 0
Created almost 2 years ago · Last pushed over 1 year ago
Metadata Files
Readme

README.md

20 Questions Game with Neural Network

Overview

This project implements a 20 Questions game using a neural network to predict the animal the user is thinking of. The game interacts with the user through a Streamlit web app, asking up to 20 yes/no questions to guess the animal. The neural network is trained on a dataset of animals and their characteristics, and it learns to make better guesses over time as it is updated with new data.

Tech Stack

  • Python: The main programming language used for implementing the game logic and neural network.
  • NumPy: For numerical operations, particularly for the neural network's calculations.
  • Streamlit: For creating the interactive web application.
  • CSV: The dataset of animals and their characteristics is stored in a CSV file.
  • JSON: Used to save and load the trained neural network model.
  • Poetry: For managing dependencies and project configuration.

Files

  • main.py: Contains the main functions for training, validating, and testing the neural network.
  • game.py: Defines the Q20Game class, which handles the game logic and interaction with the neural network.
  • neural.py: Defines the DualModeNeuralNetwork class, implementing the neural network used in the game.
  • zoo.py: Defines the Zoo class, which loads and handles the dataset of animals and their characteristics.
  • app.py: The Streamlit app that provides the interactive interface for playing the game.
  • data/zoo.csv: The dataset of animals and their characteristics.
  • pyproject.toml: The configuration file for Poetry.

How It Works

Neural Network

The neural network used in this project is a dual-mode neural network with two hidden layers. It is designed to handle both forward and backward propagation for training and making predictions.

Architecture

  • Input Layer: Receives the input vector representing the answers to the questions asked so far.
  • Hidden Layers: Two hidden layers that process the inputs using an activation function (ReLU, Sigmoid, Tanh, or Softmax).
  • Output Layer: Produces the output vector representing the probability distribution over the possible animals.

Activation Functions

  • ReLU (Rectified Linear Unit): relu(x) = max(0, x)
  • Sigmoid: sigmoid(x) = 1 / (1 + exp(-x))
  • Tanh (Hyperbolic Tangent): tanh(x) = (exp(x) - exp(-x)) / (exp(x) + exp(-x)))
  • Softmax: softmax(x) = exp(x - max(x)) / sum(exp(x - max(x)))

Training

The network is trained using backpropagation, where it adjusts the weights and biases based on the error between the predicted output and the actual target. The learning rate determines the step size for the weight updates.

Saving and Loading

The trained model can be saved to a JSON file and loaded later to avoid retraining.

Neural Network Architecture

Neural Network Architecture

Game Mechanics

The game interacts with the user by asking yes/no questions about the animal they are thinking of. It uses the neural network to make predictions based on the user's answers and tries to guess the animal within a limited number of questions.

Steps

  1. Initialize: Load the dataset and initialize the neural network.
  2. Ask Questions: The game selects the best question to ask based on the current state of the neural network and the answers given so far.
  3. Update State: Update the input vector with the user's answer and recalculate the neural network's output.
  4. Make a Guess: After a certain number of questions, the game makes a final guess about the animal.
  5. Feedback: The user provides feedback on whether the guess was correct. If not, the correct animal is provided, and the neural network is updated accordingly.

Training and Validation Techniques

The training and validation of the neural network follow a structured workflow:

Training Workflow

  1. Start Training: Initialize parameters and the game.
  2. Loop Through Targets: Iterate over each target in the dataset.
  3. Simulate Game: Call the autoplay method to simulate the game for each target.
  4. Update Input Vector: Update the input vector based on the answers.
  5. Backpropagation: Adjust the weights and biases of the neural network.
  6. End Training: Check if all targets are processed and finalize the training.

Validation Workflow

  1. Start Validation: Initialize parameters and the game.
  2. Simulate Games: Run simulations for validation.
  3. Use Smart Question Selection: Call the autoplay_smartq method to use smart question selection.
  4. Update Input Vector: Update inputs based on answers.
  5. Feed Forward: Log the current best guess.
  6. Calculate Metrics: Compute error and accuracy.
  7. Log Results: Log the validation results.
  8. End Validation: Complete the validation process.

Training and Validation Strategy Diagram

  • Training Strategy Training Strategy

  • Validation Strategy Validation Strategy

Dataset

The dataset used in this project is the zoo.csv file, which contains a list of animals and their characteristics. Each row represents an animal, and each column represents a characteristic (e.g., hair, feathers, eggs, etc.). The dataset is used to train the neural network to recognize patterns and make predictions based on user inputs.

Running the Application

  1. Install Poetry:

bash pip install poetry

  1. Install Dependencies:

bash poetry install

  1. Prepare Dataset: Ensure the data/zoo.csv file is in the correct location and contains the data for the game.

  2. Run the Streamlit App:

bash poetry run streamlit run app.py

  1. Interact with the App: The app will ask you questions, and you can answer with "Yes" or "No". The app will then try to guess the animal you are thinking of.

Training and Testing

The neural network can be trained, validated, and tested using different modes in main.py:

  • Training: bash poetry run python main.py train
  • Testing: bash poetry run python main.py test
  • Hyperparameter Tuning: bash poetry run python main.py hyperparameter

References

Future Improvements

  • Enhance the dataset with more animals and characteristics.
  • Implement more sophisticated question selection algorithms.
  • Improve the neural network architecture for better accuracy.

Conclusion

This project demonstrates a practical application of neural networks in a classic 20 Questions game. The integration with Streamlit provides an interactive and user-friendly interface, making it easy to interact with the game and observe the neural network's performance.

Owner

  • Login: Hatim001
  • Kind: user

GitHub Events

Total
  • Push event: 2
  • Public event: 1
Last Year
  • Push event: 2
  • Public event: 1

Dependencies

poetry.lock pypi
  • altair 5.3.0
  • attrs 23.2.0
  • blinker 1.8.2
  • cachetools 5.4.0
  • certifi 2024.7.4
  • charset-normalizer 3.3.2
  • click 8.1.7
  • colorama 0.4.6
  • gitdb 4.0.11
  • gitpython 3.1.43
  • idna 3.7
  • jinja2 3.1.4
  • joblib 1.4.2
  • jsonschema 4.23.0
  • jsonschema-specifications 2023.12.1
  • markdown-it-py 3.0.0
  • markupsafe 2.1.5
  • mdurl 0.1.2
  • numpy 2.0.1
  • packaging 24.1
  • pandas 2.2.2
  • pillow 10.4.0
  • protobuf 5.27.2
  • pyarrow 17.0.0
  • pydeck 0.9.1
  • pygments 2.18.0
  • python-dateutil 2.9.0.post0
  • pytz 2024.1
  • referencing 0.35.1
  • requests 2.32.3
  • rich 13.7.1
  • rpds-py 0.19.1
  • scikit-learn 1.5.1
  • scipy 1.14.0
  • six 1.16.0
  • smmap 5.0.1
  • streamlit 1.37.0
  • tenacity 8.5.0
  • threadpoolctl 3.5.0
  • toml 0.10.2
  • toolz 0.12.1
  • tornado 6.4.1
  • typing-extensions 4.12.2
  • tzdata 2024.1
  • urllib3 2.2.2
  • watchdog 4.0.1
pyproject.toml pypi
  • numpy ^2.0.1
  • python ^3.11
  • scikit-learn ^1.5.1
  • streamlit ^1.37.0