- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在编写一个基于图形的反向传播神经网络,作为个人项目。仍在前进的支柱步骤。它编译。一半时间成功运行,一半时间在最后一步崩溃。它似乎在某些垃圾收集步骤中快要死了。我是虚函数和 static_cast 的新手,所以我想知道是否应该归咎于这些部分。 GDB 说“程序收到信号 SIGABRT,已中止。__gnu_cxx::new_allocator::deallocate(double*, unsigned long) () 中的 0x00000000100404740”
构成代码前半部分的函数可能不是罪魁祸首,因为它们在我的神经网络(没有图表)的一个更简单的旧版本中工作。我敢打赌它在某处的结构中。
更新:如果我使用 123 而不是基于时间的随机种子生成随机数,它每次都会运行。 seed=124 每次都失败。去除随机性以支持恒定权重也允许它每次都运行。我很困惑!
#include <bits/stdc++.h>
using namespace std;
#define p(x) cout << #x << " = "<< x<< endl
#define min(a,b) a<b ? a : b
typedef vector<double> d1;
typedef vector<d1> d2;
typedef vector<d2> d3;
typedef vector<int> i1;
int argmax(d1 x){
p(x.size());
int maxIndex=0;
double maxValue=x[0];
for (int i=1; i<x.size(); i++){
if (x[i] > maxValue){
maxValue = x[i];
maxIndex = i;
}
}
return maxIndex;
}
d1 zeros(int n){
return d1(n);
}
d2 zeros(int rows, int cols){
return d2(rows, d1(cols, 0));
}
d3 zeros(int x, int rows, int cols){
return d3(x, d2(rows, d1(cols, 0)));
}
void print(d1 x){
for (double d: x)
cout << d << endl;
cout << endl;
}
void print(d2 x){
for (auto row: x){
for (double d: row){
cout << d << " ";
}
cout << endl;
}
cout << endl;
}
void print(d3 x){
for (d2 X: x)
print(X);
}
void toRank2(d1&x, int rows, d2& y){
for (int i=0; i<x.size()/rows; i++){
y.emplace_back();
for (int row=0; row<rows; row++){
y[i].push_back(x[i*rows+row]);
}
}
}
void toRank3(d1& x, int rows, int cols, d3& y){
for (int i=0; i<x.size()/rows/cols; i++){
y.emplace_back();
for (int row=0; row<rows; row++){
y[i].emplace_back();
for (int col=0; col<cols; col++){
y[i][row].push_back(x[i*rows*cols+row*cols+col]);
}
}
}
}
d1 getRandomDoubles(int size, double mean=0, double standard_deviation=1){
static normal_distribution<double> distribution(mean, standard_deviation);
int seed=time(NULL);
static default_random_engine generator(seed);
d1 data(size);
generate(data.begin(), data.end(), []() { return distribution(generator); });
return data;
}
d2 getRandomDoubles(int rows, int cols, double mean=0, double standard_deviation=1){
d1 d = getRandomDoubles(rows*cols, mean, standard_deviation);
d2 e;
toRank2(d, cols, e);
return e;
}
d3 getRandomDoubles(int depth, int rows, int cols, double mean=0, double standard_deviation=1){
d1 d = getRandomDoubles(depth*rows*cols, mean, standard_deviation);;
d3 e;
toRank3(d, rows, cols, e);
return e;
}
struct Node{
vector<Node*> parents, children;
bool ready=false;
//
// bool check_ready(){
// for (Node* n: parents)
// if (!n->check_ready())
// return false;
// return true;
// }
//
void add_child(Node& n){
children.push_back(&n);
n.parents.push_back(this);
}
void forward_propagate(){
cout << "starting r2 forward" <<endl;
// if (parents.size()==0 || updated_parents == parents.size()-1)
for (Node* n: children){
cout << "loop" << endl;
n->update_state();
// cout << "root child forward" << endl;
}
cout << "exiting r2 forward" << endl;
//updated_parents++;
}
virtual void update_state(){
//if (parents.size()==0 || updated_parents == parents.size() - 1)
forward_propagate();
}
};
struct r1:Node{
vector<double> state;
int r;
r1(){}
r1(int R){
r=R;
state = vector<double>(r);
}
};
struct r2:Node{
vector<vector<double>> state;
int r,c;
r2(){}
r2(int R, int C){
r=R;
c=C;
state = zeros(r, c);
}
};
struct r3:Node{
d3 state;
int r, c, d;
r3(){}
r3(int R, int C, int D){
r=R;
c=C;
d=D;
state = zeros(R,C,D);
}
};
struct MatrixProduct1_1: r1{
MatrixProduct1_1(int n):r1(n){}
void update_state() override{
cout << "mat11" << endl;
d2& W = static_cast<r2*>(parents[0])->state;
d1& x = static_cast<r1*>(parents[1])->state;
state = zeros(r);
for (int i=0; i<W.size(); i++)
for (int j=0; j<W[0].size(); j++)
state[i] += W[i][j]*x[j];
forward_propagate();
}
};
struct MatrixProduct2_1: r1{
MatrixProduct2_1(int n):r1(n){}
void update_state() override{
cout << "matt21" << endl;
d3& W = static_cast<r3*>(parents[0])->state;
d2& x = static_cast<r2*>(parents[1])->state;
state = zeros(r);
for (int i=0; i<W.size(); i++)
for (int j=0; j<W[0].size(); j++)
for (int k=0; k<W[0][0].size(); k++)
state[k] += W[i][j][k]*x[i][j];
forward_propagate();
}
};
struct Convolution: r2{
Convolution(int r, int c): r2(r, c){}
void update_state() override{
cout << "convolving" << endl;
state = zeros(r, c);
d2& W = static_cast<r2*>(parents[0])->state;
d2& x = static_cast<r2*>(parents[1])->state;
int wCenterX = W[0].size() / 2;
int wCenterY = W.size() / 2;
int rows = x.size(), cols = x[0].size();
int wRows = W.size(), wCols = W[0].size();
//#pragma omp parallel for
for(int i=0; i < rows; i++)
for(int j=0; j < cols; j++)
for(int m=0; m < W.size(); m++){
int mm = W.size() - 1 - m;
for(int n=0; n < wCols; n++){
int nn = wCols - 1 - n;
int ii = i + (m - wCenterY);
int jj = j + (n - wCenterX);
if (ii >= 0 && ii < rows && jj >= 0 && jj < cols)
state[i][j] += x[ii][jj] * W[mm][nn];
}
}
forward_propagate();
}
};
struct RELU: r2{
RELU(int r, int c):r2(r, c){}
void update_state() override{
cout << "relu2" << endl;
state = zeros(r,c);
d2& x = static_cast<r2*>(parents[0])->state;
for (int i=0; i<state.size(); i++)
for (int j=0; j<state[0].size(); j++)
if (x[i][j] > 0)
state[i][j] = x[i][j];
forward_propagate();
}
};
struct Softmax: r1{
Softmax(int r):r1(r){}
void update_state() override{
cout << "softmax" << endl;
state = zeros(r);
p(parents.size());
d1& x = static_cast<r1*>(parents[0])->state;
cout << "got state" << endl;
//p(x.size());
//print(x);
p(x.size());
cout << "argmax " << argmax(x) << endl;
double largest = x[argmax(x)];
double lndenom = largest;
double expsum = 0;
cout << "starting expsum" << endl;
for (int i=0; i<x.size(); i++)
//expsum += exp(x[i]-largest);
expsum += x[i] - largest;
cout << "next loop " << endl;
for (int i=0; i<x.size(); i++)
// state[i] = exp(x[i]-largest) / expsum;
state[i] = x[i]-largest;
cout << "forward proping" << endl;
cout << "weird" << endl;
// forward_propagate();
cout << "done with softmax" <<endl;
}
};
struct Add1: r1{
Add1(int r):r1(r){}
void update_state() override{
cout << "add1ing" << endl;
d1& x = static_cast<r1*>(parents[0])->state;
d1& y = static_cast<r1*>(parents[1])->state;
for (int i=0; i<r; i++)
state[i] = x[i]+y[i];
forward_propagate();
}
};
struct Add2: r2{
Add2(int r, int c): r2(r, c){}
void update_state() override{
d2& x = static_cast<r2*>(parents[0])->state;
d2& y = static_cast<r2*>(parents[1])->state;
for (int i=0; i<x.size(); i++)
for (int j=0; j<x[0].size(); j++)
state[i][j] = x[i][j] + y[i][j];
forward_propagate();
}
};
struct MaxPool: r2{
MaxPool(int r, int c): r2(r, c){}
void update_state() override{
d2& x = static_cast<r2*>(parents[0])->state;
for (int i=0; i<x.size(); i+=2)
for (int j=0; j<x[0].size(); j+=2)
state[i/2][j/2] = max(max(x[i][j], x[i+1][j]), max(x[i+1][j], x[i+1][j+1]));
forward_propagate();
}
};
int main(){
Node root;
r2 x;
x.state = getRandomDoubles(28,28);
r2 wConv;
wConv.state = getRandomDoubles(10, 10);
root.add_child(x);
root.add_child(wConv);
Convolution c(28,28);
wConv.add_child(c);
x.add_child(c);
Add2 a(28,28);
r2 bConv(28,28);
bConv.state = getRandomDoubles(28,28);
c.add_child(a);
bConv.add_child(a);
RELU r(28,28);
a.add_child(r);
MaxPool max(14, 14);
r.add_child(max);
r3 wFull(10,28,28);
wFull.state = getRandomDoubles(10,28,28);
// print(wFull.state);
// return 0;
MatrixProduct2_1 full(10);
wFull.add_child(full);
max.add_child(full);
r1 bFull(10);
bFull.state = getRandomDoubles(10);
Add1 aFull(10);
aFull.state[0] = 123;
full.add_child(aFull);
bFull.add_child(aFull);
Softmax s(10);
aFull.add_child(s);
// d1& x = static_cast<r1*>(parents[0])->state;
// d1& asdf = static_cast<r1*>(s.parents[0])->state;
// print(asdf);
//root.forward_propagate();
x.forward_propagate();
//print(s.state);
cout << "returning main";
}
最佳答案
static_cast
应该很少需要。这也不异常(exception)。你的节点真的应该知道他们的邻居有什么类型。
我无法立即发现具体问题,但我熟悉神经网络。像 struct MatrixProduct1_1: r1
这样的代码几乎是一个红色警报。为什么它是一个结构体,为什么它继承自 r1
?在神经网络理论中,矩阵乘积是表达两层节点之间完整连接的方式。话又说回来,节点通常具有标量激活。
可以使用继承来实现激活函数,但您需要从 Node
继承它。这意味着你不能同时拥有那些 r1
..r3
类型,但我还是不明白这些。
TLDR:类型被搞乱了,你用 static_cast
隐藏了它,但这只会让它编译,并不能让它正确。
关于c++ - 间歇性 "Aborted Core Dumped"。也许是 static_cast 的错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40624341/
我正在尝试从数组元素中形成最大数。下面给出的我的实现在某些情况下工作正常,而在其他一些情况下它给出错误“来自 abort(3) (SIGABRT) 的中止信号”。为什么?帮帮我! #include
我见过许多具有如下所示的线程过程的示例。 private void ThreadProc() { while (serviceStarted) {
以下代码用于基本的循环链表,但是当一个人输入一个较大的n(例如8位数字)值时,它将引发“abort(3)(sigabrt)中止信号”错误。我不确定这意味着什么,并且希望就我的代码解决此问题提供一些指导
这个问题在这里已经有了答案: Why exactly should I not call free() on variables not allocated by malloc()? (7 个答案)
此文档页面在突出通知中指出,Delphi 中有两个中止指令。一种称为abort,另一种称为Abort。 http://docwiki.embarcadero.com/RADStudio/Tokyo/e
我正在尝试获取/构建V8,并且需要首先安装depot_Tools。在运行UPDATE_DEPOT_TOOLS时,我不断收到从错误开始的错误。而且我永远也想不出为什么/找不到调试底层GO代码的源代码。
我正在尝试获取/构建V8,并且需要首先安装depot_Tools。在运行UPDATE_DEPOT_TOOLS时,我不断收到从错误开始的错误。而且我永远也想不出为什么/找不到调试底层GO代码的源代码。
在索引数据库中升级数据库版本时出错。“请求被中止,例如通过调用 IDBTransaction.abort。” ConstraintError:事务中的变异操作失败,因为不满足约束。例如,对象存储或索引
我对C++编程不是很在行,但我真的解决不了这个问题。我的项目如下:使用TCP协议连接到WiFi服务器。服务器继续发送文本行:没问题。它系统地连接在一起。专用线程正在循环接收文本并将其显示在编辑窗口中。
每次用户输入内容时,我都会使用 $.get jquery 函数。我的函数如下所示 function checkField(va) { $.get( '/admin-tool',
这是我的代码,它接受一个整数 s 作为输入,它是我希望它处理的字符串数量,然后它接受 s 个字符串作为输入。对于它们中的每一个,它应该输出更大的字母字典排列,即最小的字母排列。问题是它编译得很好,但在
这个问题在这里已经有了答案: C++ terminate called without an active exception (6 个答案) 关闭 6 年前。 让我们谈谈下一个代码示例: #inc
如果输入字段的长度为空,我会尝试取消所有剩余的 AJAX 请求。但是,我看到这个错误: Uncaught TypeError: Cannot read property 'abort' of unde
代码(我的原始代码使用assert,但这是一个更短的sscce) #include int main() { abort(); return 0; } 编译器版本: $ g++ --
我有一个以前从未见过的东西的回溯。请参阅这些线程中的框架 2: Thread 31 (process 8752): #0 0x00faa410 in __kernel_vsyscall () #1
前言: 有时候,连接mysql的会话经常会异常退出,错误日志里会看到"got an error reading communication packets"类型的告警。本篇文章
我正在尝试调用C++ Java 中的方法编码。 我收到了Android NDK : Aborting. Stop当定义 Android.mk文件如下: LOCAL_PATH := $(call my-
这更多是出于个人好奇心/兴趣,而不是我试图解决的特定问题。 假设您有一个程序正在对用户提供的信息(例如搜索字符串)执行一些操作,这些信息会随着用户键入而改变。假设您想向用户显示与他们在任何给定时间输入
我们需要将我们的一些 UI 翻译成英文,问题出现了:我什么时候使用“Abort”,什么时候使用“Cancel”作为按钮文本?它们似乎可以互换,并且或多或少地翻译成德语(我们的来源)中的同一个词....
当使用从 jQuery.ajax 函数传回的 xhr 中止 ajax 请求时,firebug 显示它仍在尝试加载该请求。它不会阻止发出的任何更多请求,但也不会显示它曾经完成过请求。 这只是一个 Fir
我是一名优秀的程序员,十分优秀!