-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
81 lines (68 loc) · 2.13 KB
/
Copy pathmain.cpp
File metadata and controls
81 lines (68 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "NeuralNetwork.h"
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
int main() {
// 读取配置并训练模型
std::cout << "简单神经网络回归演示" << std::endl;
int hiddenLayerCount = 0;
std::cout << "请输入隐藏层数量(0-3):";
std::cin >> hiddenLayerCount;
if (hiddenLayerCount < 0) {
hiddenLayerCount = 0;
}
if (hiddenLayerCount > 3) {
hiddenLayerCount = 3;
}
std::vector<int> hiddenLayers;
for (int i = 0; i < hiddenLayerCount; ++i) {
int neurons = 1;
std::cout << "请输入第 " << (i + 1) << " 个隐藏层的神经元数量(1-10):";
std::cin >> neurons;
if (neurons < 1) {
neurons = 1;
}
if (neurons > 10) {
neurons = 10;
}
hiddenLayers.push_back(neurons);
}
double learningRate = 0.01;
std::cout << "请输入学习率:";
std::cin >> learningRate;
if (learningRate <= 0.0) {
learningRate = 0.01;
}
int epochs = 200;
std::cout << "请输入训练轮次(>=200):";
std::cin >> epochs;
if (epochs < 200) {
epochs = 200;
}
// 创建神经网络对象
NeuralNetwork network(hiddenLayers, learningRate);
std::string filename;
std::cout << "请输入数据文件路径:";
std::cin >> filename;
if (!network.loadData(filename)) {
std::cout << "读取数据文件失败。" << std::endl;
return 1;
}
// 训练并输出参数
network.train(epochs);
network.printParameters();
// 预测与误差输出
double inputX = 0.0;
std::cout << "请输入新的输入 x:";
std::cin >> inputX;
double predictedY = network.predict(inputX);
std::cout << "预测的 y 值为:" << std::fixed << std::setprecision(6) << predictedY << std::endl;
double realY = 0.0;
std::cout << "请输入真实的 y 值:";
std::cin >> realY;
double error = std::fabs(predictedY - realY);
std::cout << "绝对误差为:" << std::fixed << std::setprecision(6) << error << std::endl;
return 0;
}