How luyabs/literary-works-recommendation-algorithm works
Project Type & Purpose
• This project implements the algorithmic part of a hybrid literary works recommendation system. It combines collaborative filtering (e.g., LFM), factorization machines (FM/DeepFM), and deep learning (with BERT for semantic feature extraction) to generate recommendations.
• The README explains that the recommendation process involves a dual-stage prediction (recall using LFM and re-ranking with FM or DeepFM) along with an additional “基于Embedding”的近似向量推荐流程, plus mechanisms for incremental training and index mapping.
Major Components
a. API/Controller Layer:
– Represented by the Flask-based controller (controller/recommed_controller.py).
– This is the interaction endpoint that triggers predictions.
b. Database Access & Mapping Layer:
– Include the mapper directory (dataset_mapper, id_mapping_mapper, train_and_predictor_mapper) which encapsulates the logic for database communication.
– Also include the mysql_connector for wrapping and managing MySQL connections.
c. Model & Training Modules:
– The model_and_train directory includes multiple recommendation models and their respective training, prediction, and evaluation scripts. Break these out into:
• LFM: Contains lfm_model.py and lfm_trainer_and_predictor.py.
• FM & DeepFM: Represented by fm and deep_fm directories. Emphasize that these come with associated training logs and model definitions.
• Multi-feature FM: Incorporates additional components for text extraction using BERT (with supporting configuration and weight files).
• id_mapping.py: This is key to translating raw (possibly non-sequential) database IDs into continuous indices for the models’ embedding layers.
d. Data Ingestion & Preprocessing:
– The raw_data directory includes data files (and subdirectories like Dou Ban Books Dataset), a script for data cleaning (crawler.py), and a script for importing data into the database (save_into_db.py).
e. Model Parameters:
– The model_params directory stores pre-trained model weights that can be loaded without retraining.
Relationships & Interactions
– Show that these modules fetch and update data in the underlying MySQL database.
– For the Model & Training Modules, indicate the training pipeline flow: raw data ➔ preprocessing (via raw_data scripts) ➔ mapping (index translations through id_mapping.py) ➔ model training (LFM/FM/DeepFM) ➔ predictions.
– Emphasize the dual prediction stages: first a recall stage using LFM, then a re-ranking stage using FM/DeepFM, with additional incorporation of embedding-based similarity.
– Highlight how incremental training is managed by dual index mapping objects and atomic model updates (using locks).
Architectural Patterns & Design Principles
– Modularization: Clearly separate core concerns (API, database access, model training, raw data ingestion) into distinct components.
– Flexibility & Extensibility: Make note of the design for switching models (controller can be configured to choose between lfm and fm/deep_fm) and additional support for BERT-based features.
– Data Flow: Clearly detail the data flow from raw input (dat files and crawled data) through ETL into the MySQL backend; then on-demand retrieval by mappers for recommendation inference.
– Concurrency & Atomicity: Include components representing scheduling and concurrency control (mutex locks during incremental training).