-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNeuralNetwork.h
More file actions
48 lines (40 loc) · 1.34 KB
/
Copy pathNeuralNetwork.h
File metadata and controls
48 lines (40 loc) · 1.34 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
#ifndef NEURALNETWORK_H
#define NEURALNETWORK_H
#include <string>
#include <vector>
// 简单的前馈神经网络类,用于一维回归
class NeuralNetwork {
public:
// hiddenLayers: 隐藏层结构,每个元素表示该层神经元数量
// learningRate: 学习率
NeuralNetwork(const std::vector<int>& hiddenLayers, double learningRate);
// 从文件加载数据(每行两个浮点数,tab或空格分隔)
bool loadData(const std::string& filename);
// 训练网络
void train(int epochs);
// 预测输出
double predict(double x) const;
// 输出网络参数
void printParameters() const;
private:
// 归一化与反归一化
double normalize(double value, double minVal, double maxVal) const;
double denormalize(double value, double minVal, double maxVal) const;
// 训练数据
std::vector<std::vector<double> > data;
// 权重与偏置
std::vector<std::vector<std::vector<double> > > weights;
std::vector<std::vector<double> > biases;
// 网络结构(含输入与输出层)
std::vector<int> layers;
double learningRate;
// 归一化所需的最大最小值
double xMin;
double xMax;
double yMin;
double yMax;
// 激活函数与导数
double activate(double x) const;
double activateDerivative(double y) const;
};
#endif