Header-only graph-based neural network library with template activation functions, backpropagation learning, and JSON save/load.
README
ofxNeuralGraph
openFrameworks用のグラフベースニューラルネットワークライブラリ
特徴
- グラフベースアーキテクチャ - 層構造に制限されない自由な接続
- ヘッダーオンリー実装 - ビルドが簡単、配布しやすい
- 外部依存なし - STLライブラリのみ使用
- テンプレート活性化関数 - Sigmoid、ReLU、Linear、Tanh対応
- シンプルAPI - 1行でニューロン作成可能
- 学習機能 - バックプロパゲーション、重み更新
- 可視化対応 - 重みの強さと正負を色と太さで表現
- JSON保存/読み込み - 学習済みネットワークの永続化
使用方法
#include "ofxNeuralGraph.h"
// ニューラルグラフを作成
ofxNeuralGraph::ofxNeuralGraph graph;
// 入力ニューロンを作成(Linear活性化関数)
vector<int> inputs;
inputs.push_back(graph.makeNeuron<ofxNeuralGraph::Linear>({}));
inputs.push_back(graph.makeNeuron<ofxNeuralGraph::Linear>({}));
// 隠れ層を作成(Sigmoid活性化関数)
vector<int> hidden;
graph.makeManyNeurons<ofxNeuralGraph::Sigmoid>(inputs, 3, hidden);
// 出力ニューロンを作成(Sigmoid活性化関数)
int output = graph.makeNeuron<ofxNeuralGraph::Sigmoid>(hidden);
// 重みとバイアスを初期化
graph.randomizeWeights(-0.5f, 0.5f);
graph.randomizeBiases(0.0f, 0.0f);
// 学習率を設定
graph.setLearningRate(0.1f);
// 学習ループ
graph.setInputs({{inputs[0], 0.0f}, {inputs[1], 1.0f}});
graph.forward();
float result = graph.getOutput(output);
graph.backward({{output, targetValue}});
graph.updateWeights();
// ネットワークを保存/読み込み
graph.save("network.json");
graph.load("network.json");活性化関数
Linear- 線形関数(入力層に推奨)Sigmoid- シグモイド関数(隠れ層・出力層)ReLU- ReLU関数(隠れ層)Tanh- tanh関数(隠れ層)
サンプル
example-basic: XOR問題の学習と可視化
- ネットワーク構造: 入力2→隠れ層3→出力1
- 学習データ: XOR論理演算(4パターン)
- 可視化機能:
- ニューロンとエッジの表示
- 重みの強さを線の太さで表現
- 正の重みは青、負の重みは赤で表示
- 学習進行のリアルタイム表示
操作方法
SPACE: 学習開始/停止T: 基本XORテスト(0,1入力)C: カスタム入力テスト(0.5等の中間値)A: 網羅的テスト(グリッド分析)S: ネットワーク保存L: ネットワーク読み込みR: ネットワークリセット
インストール
1. このリポジトリをopenFrameworksの
addonsフォルダに配置 2. プロジェクトジェネレーターでexample-basicをインポート 3. ofxNeuralGraphアドオンを追加 4. ビルド・実行ファイル構造
ofxNeuralGraph/
├── src/
│ ├── ofxNeuralGraph.h # メインクラス
│ ├── ofxNeuron.h # ニューロンクラス
│ └── ofxActivationFunctions.h # 活性化関数群
├── example-basic/ # XORサンプル
│ ├── addons.make
│ ├── src/
│ │ ├── main.cpp
│ │ ├── ofApp.h
│ │ └── ofApp.cpp
│ └── bin/data/
├── README.md
└── CLAUDE.md # 開発指示書技術仕様
- メモリ管理: shared_ptrで安全性確保
- 学習アルゴリズム: バックプロパゲーション
- 重み初期化: Xavier初期化
- トポロジカルソート: 依存関係を考慮した順伝播・逆伝播
- JSON形式: ニューロンの種類、重み、バイアスを保存
ライセンス
MIT
ofxAddons