最近我一直试图编写一个神经网络程序。我将所有神经元连接存储在神经元的矢量中。然而,每当我将连接推回向量时,它似乎都没有存储(我可以通过调试模式告诉),当我尝试在for循环中添加向量的激活值时,我得到一个out_of_range错误。这是我的代码。
Main.cpp的
#include <iostream>
#include "neuron.h"
void displayboard(bool board [8][8]);
using namespace std;
int main()
{
int id = 2;
int inputids [] = {3};
int outputids [] = {4};
int inputweights [] = {5};
bool new_neuron = true;
neuron test (inputids, outputids, inputweights, new_neuron, id);
test.connections.at(0).value = 6;
// here is where the error is returned
test.activation();
cout << test.activationnumber;
return 0;
}
这里是Neuron.cpp:
#include "neuron.h"
#include <vector>
#include <random>
#include <ctime>
using namespace std;
neuron::neuron(int inputids [], int outputids [], int inputweights [],
bool new_neuron, int id)
{
this->id = id;
if (new_neuron==true) {
srand (time(0));
connection tempconnection;
for (int i = 0; i <=(sizeof (inputids)/sizeof (inputids [0])); i++)
{
tempconnection.type=false;
tempconnection.tofrom = inputids [i];
tempconnection.weight = rand ();
this->connections.push_back (tempconnection);
}
// this continues on for other options
}
void neuron::activation (){
for (int i=0; i<=this->connections.size (); i++) {
this->activationnumber += ((this->connections.at(i).value)
*(this->connections.at (i).weight));
}
}
答案 0 :(得分:3)
更新:阅读this将帮助您了解为什么您的“sizeof / sizeof”方法在C ++中效果不佳。
原始回答
items
的行为可能不符合您的预期。以下代码输出2,但您似乎期望4.对堆栈中的对象使用array,对堆中的对象使用vector。
if (toPaste !== -1) { // meaning its children were indeed rearranged
// inform Polymer of the change
var tempPaste = toPaste;
for (var i = toRearr.length - 1; i >= 0; i--) { // start from end to preserve order
// we're moving an item to the right of our 'paste' index, so decrease
// tempPaste by 1 so it stays consistent
if (tempPaste > toRearr[i]) tempPaste--;
// move element from old index to new index, using two array splices
// that essentially remove and reinsert the element in the array
target.splice('items', tempPaste, 0, target.splice('items', toRearr[i], 1)[0]);
}
}
更改
sizeof(array)/sizeof(array[0])
到
#include <iostream>
using namespace std;
void foo( int array[] )
{
wcout << sizeof( array ) / sizeof( array[ 0 ] );
}
int main()
{
int test[ 4 ];
foo( test );
return 0;
}
同时更改
int inputids [] = {3};
int outputids [] = {4};
到
vector< int > {3};
vector< int > {4};