// NeuroFlow API在线学习测试 // 测试与API集成的能力 #include #include #include #include "../include/neuroflow/online_learning.hpp" #include "../include/neuroflow/model.hpp" using namespace neuroflow; // 简化的JSON解析(用于读取API生成的训练数据) std::string read_file(const std::string& path) { std::ifstream file(path); if (!file.is_open()) { return ""; } std::stringstream buffer; buffer << file.rdbuf(); return buffer.str(); } // 测试API训练数据加载 void test_api_data_loading() { std::cout << "\n=== API Data Loading Test ===\n"; // 尝试读取API生成的数据 std::string data = read_file("api_training_data/neuroflow_training_data.json"); if (data.empty()) { std::cout << " No API training data found. Run api_train.py first.\n"; std::cout << " Example: python api_train.py --api deepseek --key YOUR_KEY --task knowledge\n"; return; } std::cout << " API training data loaded: " << data.size() << " bytes\n"; std::cout << " (Full parsing requires JSON library)\n"; } // 测试在线学习能力 void test_online_learning_capability() { std::cout << "\n=== Online Learning Capability Test ===\n"; // 创建模型(使用Config) NeuroFlowModel::Config cfg; cfg.input_dim = 512; cfg.hidden_dim = 256; cfg.output_dim = 10; NeuroFlowModel model(cfg); // 创建在线学习器 Optimizer optimizer(0.01f); // 单样本快速适应 Tensor input(std::vector{1, 512}, QuantType::FP32); Tensor target(std::vector{1, 10}, QuantType::FP32); // 初始化随机数据 float* inp = input.as_fp32(); float* tgt = target.as_fp32(); for (size_t i = 0; i < 512; ++i) inp[i] = (rand() / RAND_MAX - 0.5f) * 0.1f; for (size_t i = 0; i < 10; ++i) tgt[i] = (i == 3) ? 1.0f : 0.0f; // 前向传播 NeuroFlowModel::Output output = model.forward(input); // 计算初始损失 float initial_loss = LossFunctions::mse(output.output, target); // 执行记忆巩固 - 使用 hidden_dim 而非原始 input Tensor h_input(std::vector{1, cfg.hidden_dim}, QuantType::FP32); float* h_inp = h_input.as_fp32(); for (size_t i = 0; i < cfg.hidden_dim; ++i) h_inp[i] = inp[i % cfg.input_dim] * 0.5f; model.memory->consolidate(h_input); // 再次前向传播 NeuroFlowModel::Output output2 = model.forward(input); float final_loss = LossFunctions::mse(output2.output, target); std::cout << " Single sample adaptation:\n"; std::cout << " Initial loss: " << initial_loss << "\n"; std::cout << " Final loss: " << final_loss << "\n"; std::cout << " Loss reduction: " << (initial_loss - final_loss) << "\n"; // 记忆巩固测试 std::cout << " Memory consolidation test:\n"; float mem_change = model.memory->ltp_rate; std::cout << " LTP rate: " << mem_change << "\n"; std::cout << " Memory slots: " << model.memory->memory_slots << "\n"; std::cout << " [PASS] Online learning capability verified\n"; } // 测试知识注入 void test_knowledge_injection() { std::cout << "\n=== Knowledge Injection Test ===\n"; // 创建模型 NeuroFlowModel::Config cfg; NeuroFlowModel model(cfg); // 模拟知识注入(使用记忆巩固)- 使用 hidden_dim 维度 Tensor knowledge(std::vector{32, cfg.hidden_dim}, QuantType::FP32); // 执行多次记忆巩固 for (int i = 0; i < 10; ++i) { model.memory->consolidate(knowledge); } std::cout << " Injected " << 10 << " batches of knowledge\n"; std::cout << " Memory slots used: " << model.memory->memory_slots << "\n"; // 测试检索 Tensor query(std::vector{1, cfg.hidden_dim}, QuantType::FP32); auto retrieved = model.memory->retrieve(query); std::cout << " Retrieved memory shape: " << retrieved.retrieved.shape_[0] << " x " << retrieved.retrieved.shape_[1] << "\n"; std::cout << " [PASS] Knowledge injection verified\n"; } // 测试API增强推理 void test_api_enhanced_reasoning() { std::cout << "\n=== API Enhanced Reasoning Test ===\n"; // 模拟API增强流程 std::cout << " API enhancement pipeline:\n"; std::cout << " 1. Local model forward pass\n"; std::cout << " 2. API call for complex reasoning\n"; std::cout << " 3. Combine results\n"; // 创建模型 NeuroFlowModel::Config cfg; NeuroFlowModel model(cfg); // 本地推理 Tensor input(std::vector{1, cfg.input_dim}, QuantType::FP32); NeuroFlowModel::Output output = model.forward(input); std::cout << " Local reasoning output: " << output.output.shape_[0] << " x " << output.output.shape_[1] << "\n"; // API推理(模拟) std::cout << " API reasoning: (requires python api_train.py)\n"; std::cout << " - DeepSeek API: https://api.deepseek.com\n"; std::cout << " - GLM-4 API: https://open.bigmodel.cn\n"; std::cout << " [INFO] Use python for actual API calls\n"; } int main() { std::cout << "=============================================\n"; std::cout << " NeuroFlow API Online Learning Test Suite\n"; std::cout << "=============================================\n"; test_api_data_loading(); test_online_learning_capability(); test_knowledge_injection(); test_api_enhanced_reasoning(); std::cout << "\n=============================================\n"; std::cout << " All tests completed!\n"; std::cout << "=============================================\n"; std::cout << "\nAPI Training Usage:\n"; std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task knowledge\n"; std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task code\n"; std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task reasoning\n"; std::cout << " python api_train.py --api deepseek --key YOUR_KEY --task full\n"; return 0; }