Пример #1
0
 public NdArray(int[] shape, int batchCount, IFunction <T> parentFunc = null)
 {
     this.Shape      = shape.ToArray();
     this.Length     = NdArray.ShapeToLength(this.Shape);
     this.BatchCount = batchCount;
     this.Data       = new T[this.Length * batchCount];
     this.ParentFunc = parentFunc;
 }
Пример #2
0
        //全データを読み込む
        public TestDataSet <T> GetAllDataSet()
        {
            int batchCount = Data.Length;

            T[] data = new T[NdArray.ShapeToLength(Shape) * Data.Length];

            for (int i = 0; i < batchCount; i++)
            {
                T[] labeledData = Get(i);
                Array.Copy(labeledData, 0, data, i * labeledData.Length, labeledData.Length);
            }

            TestDataSet <T> result = new TestDataSet <T>(NdArray.Convert(data, Shape, batchCount), NdArray.Convert(DataLabel, new[] { 1 }, batchCount));

            return(result);
        }
Пример #3
0
        public void Reshape(params int[] shape)
        {
            int val       = 0;
            int dimension = Length;

            //-1指定を算出
            if (shape.Contains(-1))
            {
                int minusIndex = -1;

                for (int i = 0; i < shape.Length; i++)
                {
                    if (shape[i] != -1)
                    {
                        val += Length % shape[i];

                        if (val == 0)
                        {
                            dimension /= shape[i];
                        }
                        else
                        {
                            throw new Exception("要素の指定が間違っています");
                        }
                    }
                    else
                    {
                        if (minusIndex != -1)
                        {
                            throw new Exception("-1が二つ以上指定されています");
                        }

                        minusIndex = i;
                    }
                }

                shape[minusIndex] = dimension;
            }
#if DEBUG
            else if (Length != NdArray.ShapeToLength(shape))
            {
                throw new Exception("指定されたShapeのサイズが現在のData.Lengthと等しくありません");
            }
#endif

            Shape = shape.ToArray();
        }
Пример #4
0
        private TestDataSet <T> GetRandomData(int batchCount, Func <int> getIndexFunc)
        {
            T[]   data  = new T[NdArray.ShapeToLength(Shape) * batchCount];
            int[] label = new int[batchCount];

            for (int i = 0; i < batchCount; i++)
            {
                int index = getIndexFunc();

                T[] labeledData = Get(index);
                Array.Copy(labeledData, 0, data, i * labeledData.Length, labeledData.Length);

                label[i] = DataLabel[index];
            }

            TestDataSet <T> result = new TestDataSet <T>(NdArray.Convert(data, Shape, batchCount), NdArray.Convert(label, new[] { 1 }, batchCount));

            return(result);
        }
Пример #5
0
        public NdArray(Array array, bool asBatch = false, IFunction <T> parentFunc = null)
        {
            this.Data = array.FlattenEx <T>();

            //1次元目をバッチとして扱うか?
            if (asBatch)
            {
                this.BatchCount = array.Rank > 1 ? array.GetLength(0) : array.Length;
                int[] resultShape = array.Rank > 1 ? new int[array.Rank - 1] : new[] { 1 };

                if (array.Rank > 1)
                {
                    for (int i = 0; i < resultShape.Length; i++)
                    {
                        resultShape[i] = array.GetLength(i + 1);
                    }
                }

                this.Shape  = resultShape;
                this.Length = NdArray.ShapeToLength(this.Shape);
            }
            else
            {
                int[] resultShape = new int[array.Rank];

                for (int i = 0; i < array.Rank; i++)
                {
                    resultShape[i] = array.GetLength(i);
                }

                this.Shape  = resultShape;
                this.Length = Data.Length;
            }

            this.ParentFunc = parentFunc;
        }
Пример #6
0
 //範囲指定の配列を作る
 public static Array Range(int[] shape, int start = 0)
 {
     return(Enumerable.Range(start, NdArray.ShapeToLength(shape)).ToNdArray(shape));
 }
Пример #7
0
 public NdArray(params int[] shape)
 {
     this.Data   = new T[NdArray.ShapeToLength(shape)];
     this.Shape  = shape.ToArray();
     this.Length = Data.Length;
 }