Example #1
0
        void Initialize(Array initialW = null, Array initialb = null)
        {
            this.Weight      = new NdArray(OutputCount, InputCount, this._kHeight, this._kWidth);
            this.Weight.Name = this.Name + " Weight";

            if (initialW == null)
            {
                Initializer.InitWeight(this.Weight);
            }
            else
            {
                this.Weight.Data = Real.ToRealArray(initialW);
            }

            this.Parameters[0] = this.Weight;

            if (!NoBias)
            {
                this.Bias      = new NdArray(OutputCount);
                this.Bias.Name = this.Name + " Bias";

                if (initialb != null)
                {
                    this.Bias.Data = Real.ToRealArray(initialb);
                }

                this.Parameters[1] = this.Bias;
            }
        }
Example #2
0
        public AddBias(int axis = 0, int[] biasShape = null, Array initialb = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null) : base(name, inputNames, outputNames)
        {
            this.Axis = axis;
            this.Bias = new NdArray(biasShape);

            if (initialb != null)
            {
                Bias.Data = Real.ToRealArray(initialb);
            }

            this.Parameters = new[] { Bias };
        }
Example #3
0
        public NdArray(Array data, Function parentFunc = null)
        {
            Real[] resultData = Real.ToRealArray(data);

            int[] resultShape = new int[data.Rank];

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

            this.Data       = resultData;
            this.Shape      = resultShape;
            this.Length     = Data.Length;
            this.ParentFunc = parentFunc;
        }
Example #4
0
        public MultiplyScale(int axis = 1, int[] wShape = null, bool biasTerm = false, Array initialW = null, Array initialb = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null) : base(name, inputNames, outputNames)
        {
            this.Axis     = axis;
            this.BiasTerm = biasTerm;

#if DEBUG
            if (wShape == null)
            {
                if (biasTerm)
                {
                    throw new Exception("Biasのみでの使用はAddBiasを使用してください");
                }
                else
                {
                    throw new Exception("パラメータの設定が正しくありません");
                }
            }
#endif
            this.Weight     = new NdArray(wShape);
            this.Parameters = new NdArray[biasTerm ? 2 : 1];

            if (initialW == null)
            {
                this.Weight.Data = Enumerable.Repeat((Real)1.0, Weight.Data.Length).ToArray();
            }
            else
            {
                this.Weight.Data = Real.ToRealArray(initialW);
            }

            this.Parameters[0] = this.Weight;

            if (biasTerm)
            {
                this.Bias = new NdArray(wShape);

                if (initialb != null)
                {
                    this.Bias.Data = Real.ToRealArray(initialb);
                }

                this.Parameters[1] = this.Bias;
            }

            SingleInputForward   = ForwardCpu;
            SingleOutputBackward = BackwardCpu;
        }
Example #5
0
        public BatchNormalization(int channelSize, double decay = 0.9, double eps = 1e-5, Array initialAvgMean = null, Array initialAvgVar = null, bool isTrain = true, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null) : base(name, inputNames, outputNames)
        {
            this.ChannelSize = channelSize;
            this.Decay       = decay;
            this.Eps         = eps;
            this.IsTrain     = isTrain;

            this.Gamma      = new NdArray(channelSize);
            this.Gamma.Data = Enumerable.Repeat((Real)1, channelSize).ToArray();
            this.Gamma.Name = this.Name + " Gamma";

            this.Beta      = new NdArray(channelSize);
            this.Beta.Name = this.Name + " Beta";

            this.Parameters = new NdArray[this.IsTrain ? 2 : 4];

            //学習対象のParameterを登録
            this.Parameters[0] = this.Gamma;
            this.Parameters[1] = this.Beta;

            this.AvgMean      = new NdArray(channelSize);
            this.AvgMean.Name = this.Name + " Mean";
            this.AvgVar       = new NdArray(channelSize);
            this.AvgVar.Name  = this.Name + " Variance";

            if (initialAvgMean != null)
            {
                this.AvgMean.Data = Real.ToRealArray(initialAvgMean);
            }

            if (initialAvgVar != null)
            {
                this.AvgVar.Data = Real.ToRealArray(initialAvgVar);
            }

            if (!this.IsTrain)
            {
                this.Parameters[2] = this.AvgMean;
                this.Parameters[3] = this.AvgVar;
            }

            SingleInputForward   = ForwardCpu;
            SingleOutputBackward = BackwardCpu;
        }
Example #6
0
        //アレイ配列をバッチとして登録する
        public static NdArray FromArrays(Array[] arrays, Function parentFunc = null)
        {
            int[] resultShape = new int[arrays[0].Rank];

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

            int length = arrays[0].Length;

            Real[] result = new Real[length * arrays.Length];

            for (int i = 0; i < arrays.Length; i++)
            {
                Array.Copy(Real.ToRealArray(arrays[i]), 0, result, length * i, length);
            }

            return(new NdArray(result, resultShape, arrays.Length, parentFunc));
        }
Example #7
0
        public EmbedID(int inputCount, int outputCount, Real[,] initialW = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null) : base(name, inputNames, outputNames)
        {
            this.InputCount  = inputCount;
            this.OutputCount = outputCount;

            this.Weight      = new NdArray(inputCount, outputCount);
            this.Weight.Name = this.Name + " Weight";

            if (initialW == null)
            {
                Initializer.InitWeight(this.Weight);
            }
            else
            {
                //単純に代入しないのはサイズのチェックを兼ねるため
                this.Weight.Data = Real.ToRealArray(initialW);
            }

            this.Parameters = new[] { this.Weight };
        }
Example #8
0
        public Linear(int inputCount, int outputCount, bool noBias = false, Array initialW = null, Array initialb = null, CompressibleActivation activation = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null, bool gpuEnable = false) : base(FUNCTION_NAME, activation, name, inputNames, outputNames, gpuEnable)
        {
            this.OutputCount = outputCount;
            this.InputCount  = inputCount;

            this.Weight      = new NdArray(outputCount, inputCount);
            this.Weight.Name = this.Name + " Weight";

            this.NoBias = noBias;

            this.Parameters = new NdArray[noBias ? 1 : 2];

            if (initialW == null)
            {
                Initializer.InitWeight(this.Weight);
            }
            else
            {
                this.Weight.Data = Real.ToRealArray(initialW);
            }

            this.Parameters[0] = this.Weight;

            if (!noBias)
            {
                this.Bias      = new NdArray(outputCount);
                this.Bias.Name = this.Name + " Bias";

                if (initialb != null)
                {
                    this.Bias.Data = Real.ToRealArray(initialb);
                }

                this.Parameters[1] = this.Bias;
            }
        }
Example #9
0
        public LSTM(int inSize, int outSize, Array lateralInit = null, Array upwardInit = null, Array biasInit = null, Array forgetBiasInit = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null, bool gpuEnable = false) : base(name, inputNames, outputNames)
        {
            this.InputCount  = inSize;
            this.OutputCount = outSize;

            List <NdArray> functionParameters = new List <NdArray>();

            Real[] lateralW = null;
            Real[] upwardW  = null;
            Real[] upwardb  = null;

            if (upwardInit != null)
            {
                upwardW = new Real[inSize * outSize * 4];

                Real[] tmpUpwardInit = Real.ToRealArray(upwardInit);

                for (int i = 0; i < 4; i++)
                {
                    Array.Copy(tmpUpwardInit, 0, upwardW, i * tmpUpwardInit.Length, tmpUpwardInit.Length);
                }
            }

            if (lateralInit != null)
            {
                lateralW = new Real[outSize * outSize * 4];

                Real[] tmpLateralInit = Real.ToRealArray(lateralInit);

                for (int i = 0; i < 4; i++)
                {
                    Array.Copy(tmpLateralInit, 0, lateralW, i * tmpLateralInit.Length, tmpLateralInit.Length);
                }
            }

            if (biasInit != null && forgetBiasInit != null)
            {
                upwardb = new Real[outSize * 4];

                Real[] tmpBiasInit = Real.ToRealArray(biasInit);

                for (int i = 0; i < biasInit.Length; i++)
                {
                    upwardb[i * 4 + 0] = tmpBiasInit[i];
                    upwardb[i * 4 + 1] = tmpBiasInit[i];
                    upwardb[i * 4 + 3] = tmpBiasInit[i];
                }

                Real[] tmpforgetBiasInit = Real.ToRealArray(forgetBiasInit);

                for (int i = 0; i < tmpforgetBiasInit.Length; i++)
                {
                    upwardb[i * 4 + 2] = tmpforgetBiasInit[i];
                }
            }

            this.upward = new Linear(inSize, outSize * 4, noBias: false, initialW: upwardW, initialb: upwardb, name: "upward");
            functionParameters.AddRange(this.upward.Parameters);

            //lateralはBiasは無し
            this.lateral = new Linear(outSize, outSize * 4, noBias: true, initialW: lateralW, name: "lateral");
            functionParameters.AddRange(this.lateral.Parameters);

            this.Parameters = functionParameters.ToArray();
        }
Example #10
0
        public Deconvolution2D(int inputChannels, int outputChannels, int[] kSize, int[] subSample = null, int[] trim = null, bool noBias = false, Array initialW = null, Array initialb = null, CompressibleActivation activation = null, string name = FUNCTION_NAME, string[] inputNames = null, string[] outputNames = null, bool gpuEnable = false) : base(FUNCTION_NAME, activation, name, inputNames, outputNames, gpuEnable)
        {
            if (subSample == null)
            {
                subSample = new[] { 1, 1 }
            }
            ;

            if (trim == null)
            {
                trim = new[] { 0, 0 }
            }
            ;

            this._kWidth  = kSize[0];
            this._kHeight = kSize[1];
            this._padX    = trim[0];
            this._padY    = trim[1];
            this.NoBias   = noBias;

            this._strideX = subSample[0];
            this._strideY = subSample[1];

            this.Parameters = new NdArray[noBias ? 1 : 2];

            this.OutputCount = outputChannels;
            this.InputCount  = inputChannels;

            this.Initialize(initialW, initialb);
        }

        void Initialize(Array initialW = null, Array initialb = null)
        {
            this.Weight      = new NdArray(InputCount, OutputCount, this._kHeight, this._kWidth);
            this.Weight.Name = this.Name + " Weight";

            if (initialW == null)
            {
                Initializer.InitWeight(this.Weight);
            }
            else
            {
                this.Weight.Data = Real.ToRealArray(initialW);
            }

            this.Parameters[0] = this.Weight;


            if (!NoBias)
            {
                this.Bias      = new NdArray(OutputCount);
                this.Bias.Name = this.Name + " Bias";

                if (initialb != null)
                {
                    this.Bias.Data = Real.ToRealArray(initialb);
                }

                this.Parameters[1] = this.Bias;
            }
        }