cwenzi/neuroflow-cpp
1
1// NeuroFlow API在线学习测试2// 测试与API集成的能力3 4#include <iostream>5#include <fstream>6#include <sstream>7#include "../include/neuroflow/online_learning.hpp"8#include "../include/neuroflow/model.hpp"9 10using namespace neuroflow;11 12// 简化的JSON解析(用于读取API生成的训练数据)13std::string read_file(const std::string& path) {14 std::ifstream file(path);15 if (!file.is_open()) {16 return "";17 }18 std::stringstream buffer;19 buffer << file.rdbuf();20 return buffer.str();21}22 23// 测试API训练数据加载24void test_api_data_loading() {25 std::cout << "\n=== API Data Loading Test ===\n";26 27 // 尝试读取API生成的数据28 std::string data = read_file("api_training_data/neuroflow_training_data.json");29 30 if (data.empty()) {31 std::cout << " No API training data found. Run api_train.py first.\n";32 std::cout << " Example: python api_train.py --api deepseek --key YOUR_KEY --task knowledge\n";33 return;34 }35 36 std::cout << " API training data loaded: " << data.size() << " bytes\n";37 std::cout << " (Full parsing requires JSON library)\n";38}39 40// 测试在线学习能力41void test_online_learning_capability() {42 std::cout << "\n=== Online Learning Capability Test ===\n";43 44 // 创建模型(使用Config)45 NeuroFlowModel::Config cfg;46 cfg.input_dim = 512;47 cfg.hidden_dim = 256;48 cfg.output_dim = 10;49 NeuroFlowModel model(cfg);50 51 // 创建在线学习器52 Optimizer optimizer(0.01f);53 54 // 单样本快速适应55 Tensor input(std::vector<size_t>{1, 512}, QuantType::FP32);56 Tensor target(std::vector<size_t>{1, 10}, QuantType::FP32);57 58 // 初始化随机数据59 float* inp = input.as_fp32();60 float* tgt = target.as_fp32();61 for (size_t i = 0; i < 512; ++i) inp[i] = (rand() / RAND_MAX - 0.5f) * 0.1f;62 for (size_t i = 0; i < 10; ++i) tgt[i] = (i == 3) ? 1.0f : 0.0f;63 64 // 前向传播65 NeuroFlowModel::Output output = model.forward(input);66 67 // 计算初始损失68 float initial_loss = LossFunctions::mse(output.output, target);69 70 // 执行记忆巩固 - 使用 hidden_dim 而非原始 input71 Tensor h_input(std::vector<size_t>{1, cfg.hidden_dim}, QuantType::FP32);72 float* h_inp = h_input.as_fp32();73 for (size_t i = 0; i < cfg.hidden_dim; ++i) h_inp[i] = inp[i % cfg.input_dim] * 0.5f;74 model.memory->consolidate(h_input);75 76 // 再次前向传播77 NeuroFlowModel::Output output2 = model.forward(input);78 float final_loss = LossFunctions::mse(output2.output, target);79 80 std::cout << " Single sample adaptation:\n";81 std::cout << " Initial loss: " << initial_loss << "\n";82 std::cout << " Final loss: " << final_loss << "\n";83 std::cout << " Loss reduction: " << (initial_loss - final_loss) << "\n";84 85 // 记忆巩固测试86 std::cout << " Memory consolidation test:\n";87 float mem_change = model.memory->ltp_rate;88 std::cout << " LTP rate: " << mem_change << "\n";89 std::cout << " Memory slots: " << model.memory->memory_slots << "\n";90 91 std::cout << " [PASS] Online learning capability verified\n";92}93 94// 测试知识注入95void test_knowledge_injection() {96 std::cout << "\n=== Knowledge Injection Test ===\n";97 98 // 创建模型99 NeuroFlowModel::Config cfg;100 NeuroFlowModel model(cfg);101 102 // 模拟知识注入(使用记忆巩固)- 使用 hidden_dim 维度103 Tensor knowledge(std::vector<size_t>{32, cfg.hidden_dim}, QuantType::FP32);104 105 // 执行多次记忆巩固106 for (int i = 0; i < 10; ++i) {107 model.memory->consolidate(knowledge);108 }109 110 std::cout << " Injected " << 10 << " batches of knowledge\n";111 std::cout << " Memory slots used: " << model.memory->memory_slots << "\n";112 113 // 测试检索114 Tensor query(std::vector<size_t>{1, cfg.hidden_dim}, QuantType::FP32);115 auto retrieved = model.memory->retrieve(query);116 117 std::cout << " Retrieved memory shape: " << retrieved.retrieved.shape_[0] 118 << " x " << retrieved.retrieved.shape_[1] << "\n";119 120 std::cout << " [PASS] Knowledge injection verified\n";121}122 123// 测试API增强推理124void test_api_enhanced_reasoning() {125 std::cout << "\n=== API Enhanced Reasoning Test ===\n";126 127 // 模拟API增强流程128 std::cout << " API enhancement pipeline:\n";129 std::cout << " 1. Local model forward pass\n";130 std::cout << " 2. API call for complex reasoning\n";131 std::cout << " 3. Combine results\n";132 133 // 创建模型134 NeuroFlowModel::Config cfg;135 NeuroFlowModel model(cfg);136 137 // 本地推理138 Tensor input(std::vector<size_t>{1, cfg.input_dim}, QuantType::FP32);139 NeuroFlowModel::Output output = model.forward(input);140 141 std::cout << " Local reasoning output: " << output.output.shape_[0] 142 << " x " << output.output.shape_[1] << "\n";143 144 // API推理(模拟)145 std::cout << " API reasoning: (requires python api_train.py)\n";146 std::cout << " - DeepSeek API: https://api.deepseek.com\n";147 std::cout << " - GLM-4 API: https://open.bigmodel.cn\n";148 149 std::cout << " [INFO] Use python for actual API calls\n";150}151 152int main() {153 std::cout << "=============================================\n";154 std::cout << " NeuroFlow API Online Learning Test Suite\n";155 std::cout << "=============================================\n";156 157 test_api_data_loading();158 test_online_learning_capability();159 test_knowledge_injection();160 test_api_enhanced_reasoning();161 162 std::cout << "\n=============================================\n";163 std::cout << " All tests completed!\n";164 std::cout << "=============================================\n";165 166 std::cout << "\nAPI Training Usage:\n";167 std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task knowledge\n";168 std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task code\n";169 std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task reasoning\n";170 std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task full\n";171 172 return 0;173}