C++14 Implementation of Neural Network Simulation using BFS
#include
struct Node { int index; double state; Node(int i, double s) : index(i), state(s) {} };
struct Connection { int from; int to; double weight; Connection(int f, int t, double w) : from(f), to(t), weight(w) {} };
boolean compare(const pair<int, double>& a, const pair<int, double>& b) { return a.first < b.first; }
void neural_network(int n, int p, vector
// 初始化神经元的状态
vector<double> C(n, 0);
for (int i = 0; i < initial_states.size(); i++) {
C[i] = initial_states[i];
}
// BFS 模拟神经网络的运行
vector<pair<int, double>> output;
queue<Node> q;
for (int i = 0; i < n; i++) {
if (i < initial_states.size()) {
q.push(Node(i, initial_states[i]));
}
}
while (!q.empty()) {
Node cur = q.front();
q.pop();
for (auto& next : graph[cur.index]) {
C[next.first] += next.second * cur.state;
if (C[next.first] > 0) {
if (next.first >= n - p) {
output.push_back(make_pair(next.first + 1, C[next.first]));
}
q.push(Node(next.first, C[next.first]));
}
}
}
// 对结果进行排序并输出
sort(output.begin(), output.end(), compare);
if (!output.empty()) {
for (auto& result : output) {
cout << result.first << " " << result.second << endl;
}
} else {
cout << "NULL" << endl;
}
}
int main() {
int n, p;
cin >> n >> p;
vector
neural_network(n, p, initial_states, thresholds, connections);
return 0;
}
原文地址: https://www.cveoy.top/t/topic/vlX 著作权归作者所有。请勿转载和采集!