Thread overview | |||||
---|---|---|---|---|---|
|
February 26, 2015 3 dimension arrays | ||||
---|---|---|---|---|
| ||||
HI guys!!! Have a problem with 3d array memory allocation in next code: ////////////////////////////////////////////////////////// class NeuronNet(T, const uint layersNum) { T[] _inputs; T[][layersNum - 2] _hidden; T[] _outputs; T[] _target; //waights [col][row][dimension] T[][][layersNum - 1] _waightsArray; this(in uint[] nodesArr) { //values init _inputs = new T[nodesArr[0]]; _outputs = new T[nodesArr[$-1]]; for(uint j = 0; j < layersNum - 2; j++) { _hidden[][j] = new T[nodesArr[j + 1]]; } //waights init for(uint i = 0; i < layersNum - 1; i++) { _waightsArray[][][i] = new T[][nodesArr[1+i]]; for(uint ii = 0; ii < nodesArr[1+i]; ii++) { _waightsArray[][ii][i] = new T[nodesArr[i]]; } } } } ////////////////////////////////////////////////////////// debuger shows array range boundary error in line _waightsArray[][ii][i] = new T[nodesArr[i]]; Help me please if any one knows |
February 26, 2015 Re: 3 dimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andrey | On 02/26/2015 12:01 PM, Andrey wrote:> HI guys!!! > > Have a problem with 3d array memory allocation in next code: > > ////////////////////////////////////////////////////////// > class NeuronNet(T, const uint layersNum) > { > T[] _inputs; > T[][layersNum - 2] _hidden; > T[] _outputs; > T[] _target; > > //waights [col][row][dimension] > T[][][layersNum - 1] _waightsArray; (Unrelated: That may be a typo if its actually "weights".) > this(in uint[] nodesArr) > { > //values init > _inputs = new T[nodesArr[0]]; > _outputs = new T[nodesArr[$-1]]; > > for(uint j = 0; j < layersNum - 2; j++) Better: foreach (j; 0 .. layersNum - 2) > { > _hidden[][j] = new T[nodesArr[j + 1]]; You have extra [] up there. This is equivalent: _hidden[j] = new T[nodesArr[j + 1]]; The reason is, _hidden[] means "the entire _hidden slice" anyway. So, the empty [] has no meaning at all. > } > //waights init > for(uint i = 0; i < layersNum - 1; i++) > { > _waightsArray[][][i] = new T[][nodesArr[1+i]]; Same there: _waightsArray[i] = new T[][nodesArr[1+i]]; > for(uint ii = 0; ii < nodesArr[1+i]; ii++) > { > _waightsArray[][ii][i] = new T[nodesArr[i]]; You want to set ii'th element of the i'th element, right? This is the right way to do it in D: _waightsArray[i][ii] = ... Unlike C and C++, the array syntax is natural in D. In other words, it is not "inside out". T[] --> A slice of Ts T[][] --> A slice of T slices T[][][layersNum - 1] --> A number of those So, _waightsArray[i] is one of the slices that you've just set in the previous for loop. Ali |
February 27, 2015 Re: 3 dimension arrays | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Thursday, 26 February 2015 at 23:25:22 UTC, Ali Çehreli wrote:
> On 02/26/2015 12:01 PM, Andrey wrote:> HI guys!!!
> >
> > Have a problem with 3d array memory allocation in next code:
> >
> > //////////////////////////////////////////////////////////
> > class NeuronNet(T, const uint layersNum)
> > {
> > T[] _inputs;
> > T[][layersNum - 2] _hidden;
> > T[] _outputs;
> > T[] _target;
> >
> > //waights [col][row][dimension]
> > T[][][layersNum - 1] _waightsArray;
>
> (Unrelated: That may be a typo if its actually "weights".)
>
> > this(in uint[] nodesArr)
> > {
> > //values init
> > _inputs = new T[nodesArr[0]];
> > _outputs = new T[nodesArr[$-1]];
> >
> > for(uint j = 0; j < layersNum - 2; j++)
>
> Better:
>
> foreach (j; 0 .. layersNum - 2)
>
> > {
> > _hidden[][j] = new T[nodesArr[j + 1]];
>
> You have extra [] up there. This is equivalent:
>
> _hidden[j] = new T[nodesArr[j + 1]];
>
> The reason is, _hidden[] means "the entire _hidden slice" anyway. So, the empty [] has no meaning at all.
>
> > }
> > //waights init
> > for(uint i = 0; i < layersNum - 1; i++)
> > {
> > _waightsArray[][][i] = new T[][nodesArr[1+i]];
>
> Same there:
>
> _waightsArray[i] = new T[][nodesArr[1+i]];
>
> > for(uint ii = 0; ii < nodesArr[1+i]; ii++)
> > {
> > _waightsArray[][ii][i] = new T[nodesArr[i]];
>
> You want to set ii'th element of the i'th element, right? This is the right way to do it in D:
>
> _waightsArray[i][ii] = ...
>
> Unlike C and C++, the array syntax is natural in D. In other words, it is not "inside out".
>
> T[] --> A slice of Ts
> T[][] --> A slice of T slices
> T[][][layersNum - 1] --> A number of those
>
> So, _waightsArray[i] is one of the slices that you've just set in the previous for loop.
>
> Ali
I very much appreciate. The author of D-book respond to me. THANKS VERY MUCH!!!!!
|
Copyright © 1999-2021 by the D Language Foundation