# hwwang55/kgcn architecture

> Architecture diagram of the GitHub repository hwwang55/kgcn, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-18). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hwwang55/kgcn
- Repository: https://github.com/hwwang55/kgcn

## Overview

##### Project Type and Purpose
- This is an open‐source machine‐learning tool implementing Knowledge Graph Convolutional Networks (KGCN) for recommender systems.
- It is not a traditional client–server or full‑stack app, but a data‑processing + model‑training pipeline that:
– Loads raw rating and knowledge‐graph data (MovieLens, Last.FM)
– Preprocesses inputs into graph structures
– Builds and trains a GCN‐based recommendation model
– Outputs evaluation metrics and recommendation results

##### High‑Level File‑Structure Analysis
- data/ – raw datasets and KG files (movie/, music/)
– item_index2entity_id.txt, kg.txt, user_artists.dat
- src/ – pure‐Python implementation of the pipeline
– preprocess.py – data cleaning & graph construction
– data_loader.py – dataset batching & sampling
– aggregators.py – GCN neighborhood aggregation logic
– model.py – KGCN model class (layers, forward pass)
– train.py – training loop, loss computation, evaluation
– main.py – parameter settings & end‑to‑end orchestration

##### Key Architecture Insights from README
- The included “framework.png” shows a three‑stage flow:
1) Neighbor sampling from the KG
2) Multi‑layer graph convolution (aggregating entity + relation embeddings)
3) Score prediction for user–item pairs
- Dependencies are pure Python (NumPy/TensorFlow/PyTorch unspecified), no web servers or microservices

– MovieLens ratings.csv, Last.FM user_artists.dat
– Knowledge graph files (kg.txt)
2) Preprocessing Module (rectangle)
– preprocess.py
– Outputs: user–item train/test splits, adjacency lists, entity/relation mappings
3) Data Loader Module (rectangle)
– data_loader.py
– Performs negative sampling, mini‑batch construction
4) KGCN Model Module (rectangle)
– model.py & aggregators.py
– Graph convolutional layers, neighbor aggregator functions
5) Trainer Module (rectangle)
– train.py
– Loss calculation, optimization (e.g., Adam), early stopping, metrics
6) Orchestrator / CLI (rectangle with rounded corners)
– main.py
– Parses command‑line flags (–d movie/music), connects modules in sequence
– Recommendation lists, AUC/Recall metrics

- Raw Data → Preprocessing → (Preprocessed Files) → Data Loader → Trainer
- Data Loader ↔ KGCN Model (batches in, embeddings out)
- Trainer → Evaluation Outputs
- Orchestrator calls Preprocessing → Data Loader → Trainer in sequence

c. Architectural Patterns & Principles
- Pipeline Pattern: clear stages (Preprocess → Load → Train → Eval)
- Modular Separation: one file per responsibility (single‑responsibility principle)
- Graph‑computation submodule (aggregators) isolated from I/O code
- Parameterization via main.py for domain‑specific configs (movie vs. music)

d. Technologies & Frameworks to Call Out
- Python 3.x
- (Implicit) GCN backend – e.g., TensorFlow or PyTorch for tensor ops
- Data storage: local filesystem (flat text files)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Orchestrator
    Orchestrator(("Orchestrator / CLI\n(main.py)")):::orchestrator
    click Orchestrator "https://github.com/hwwang55/kgcn/blob/master/src/main.py"

    %% Raw Data Storage
    subgraph "Raw Data Storage"
        direction TB
        MovieData[(Movie Data)]:::data
        MusicData[(Music Data)]:::data
    end
    click MovieData "https://github.com/hwwang55/kgcn/tree/master/data/movie/"
    click MusicData "https://github.com/hwwang55/kgcn/tree/master/data/music/"

    %% Preprocessing Module
    Preprocessing["Preprocessing Module\n(preprocess.py)"]:::io
    click Preprocessing "https://github.com/hwwang55/kgcn/blob/master/src/preprocess.py"

    %% Data Loader Module
    DataLoader["Data Loader Module\n(data_loader.py)"]:::io
    click DataLoader "https://github.com/hwwang55/kgcn/blob/master/src/data_loader.py"

    %% KGCN Model Module
    subgraph "KGCN Model Module"
        direction TB
        ModelCore["model.py"]:::model
        Aggregators["aggregators.py"]:::model
    end
    click ModelCore "https://github.com/hwwang55/kgcn/blob/master/src/model.py"
    click Aggregators "https://github.com/hwwang55/kgcn/blob/master/src/aggregators.py"

    %% Trainer Module
    Trainer["Trainer Module\n(train.py)"]:::train
    click Trainer "https://github.com/hwwang55/kgcn/blob/master/src/train.py"

    %% Outputs / Evaluation
    Outputs[(Outputs / Evaluation)]:::data

    %% Connections
    Orchestrator -->|"config flags"| Preprocessing
    MovieData -->|raw ratings, KG files| Preprocessing
    MusicData -->|raw ratings, KG files| Preprocessing
    Preprocessing -->|"adjacency lists, splits"| DataLoader
    DataLoader -->|"mini-batches"| ModelCore
    ModelCore -->|"entity embeddings"| DataLoader
    ModelCore -->|"forward pass"| Trainer
    DataLoader -->|"batch loader"| Trainer
    Trainer -->|"AUC, Recall, rec lists"| Outputs

    %% Styles
    classDef data fill:#FFD6F0,stroke:#333,stroke-width:1px;
    classDef io fill:#D6EFFF,stroke:#333,stroke-width:1px;
    classDef model fill:#E0FFD6,stroke:#333,stroke-width:1px;
    classDef train fill:#FFE0B2,stroke:#333,stroke-width:1px;
    classDef orchestrator fill:#F0F0F0,stroke:#333,stroke-width:1px;
```
