Exemplo n.º 1
0
        private SimpleLayer[] ComputeMiddleLayers(SimpleLayer[] lastLayers, SimpleLayer forwardLayer, SimpleLayer backwardLayer)
        {
            int numStates = lastLayers.Length;

            SimpleLayer[] mForward  = null;
            SimpleLayer[] mBackward = null;
            Parallel.Invoke(() =>
            {
                //Computing forward RNN
                forwardLayer.netReset(false);
                mForward = new SimpleLayer[lastLayers.Length];
                for (int curState = 0; curState < lastLayers.Length; curState++)
                {
                    forwardLayer.computeLayer(null, lastLayers[curState].cellOutput);
                    mForward[curState] = forwardLayer.GetHiddenLayer();
                }
            },
                            () =>
            {
                //Computing backward RNN
                backwardLayer.netReset(false);
                mBackward = new SimpleLayer[lastLayers.Length];
                for (int curState = lastLayers.Length - 1; curState >= 0; curState--)
                {
                    backwardLayer.computeLayer(null, lastLayers[curState].cellOutput);
                    mBackward[curState] = backwardLayer.GetHiddenLayer();
                }
            });

            //Merge forward and backward
            SimpleLayer[] mergedLayer = new SimpleLayer[numStates];
            Parallel.For(0, numStates, parallelOption, curState =>
            {
                mergedLayer[curState] = new SimpleLayer(forwardLayer.LayerSize);
                mergedLayer[curState].SparseFeature = null;
                mergedLayer[curState].DenseFeature  = lastLayers[curState].cellOutput;

                SimpleLayer forwardCells  = mForward[curState];
                SimpleLayer backwardCells = mBackward[curState];

                int i = 0;
                while (i < forwardLayer.LayerSize - Vector <double> .Count)
                {
                    Vector <double> v1 = new Vector <double>(forwardCells.cellOutput, i);
                    Vector <double> v2 = new Vector <double>(backwardCells.cellOutput, i);
                    Vector <double> v  = (v1 + v2) / vecConst2;

                    v.CopyTo(mergedLayer[curState].cellOutput, i);

                    i += Vector <float> .Count;
                }

                while (i < forwardLayer.LayerSize)
                {
                    mergedLayer[curState].cellOutput[i] = (forwardCells.cellOutput[i] + backwardCells.cellOutput[i]) / 2.0;
                    i++;
                }
            });

            return(mergedLayer);
        }
Exemplo n.º 2
0
        private SimpleLayer[] ComputeMiddleLayers(SimpleLayer[] lastLayers, SimpleLayer forwardLayer, SimpleLayer backwardLayer)
        {
            int numStates = lastLayers.Length;

            SimpleLayer[] mForward = null;
            SimpleLayer[] mBackward = null;
            Parallel.Invoke(() =>
            {
                //Computing forward RNN
                forwardLayer.netReset(false);
                mForward = new SimpleLayer[lastLayers.Length];
                for (int curState = 0; curState < lastLayers.Length; curState++)
                {
                    forwardLayer.computeLayer(null, lastLayers[curState].cellOutput);
                    mForward[curState] = forwardLayer.GetHiddenLayer();
                }
            },
             () =>
             {
                 //Computing backward RNN
                 backwardLayer.netReset(false);
                 mBackward = new SimpleLayer[lastLayers.Length];
                 for (int curState = lastLayers.Length - 1; curState >= 0; curState--)
                 {
                     backwardLayer.computeLayer(null, lastLayers[curState].cellOutput);
                     mBackward[curState] = backwardLayer.GetHiddenLayer();
                 }
             });

            //Merge forward and backward
            SimpleLayer[] mergedLayer = new SimpleLayer[numStates];
            Parallel.For(0, numStates, parallelOption, curState =>
            {
                mergedLayer[curState] = new SimpleLayer(forwardLayer.LayerSize);
                mergedLayer[curState].SparseFeature = null;
                mergedLayer[curState].DenseFeature = lastLayers[curState].cellOutput;

                SimpleLayer forwardCells = mForward[curState];
                SimpleLayer backwardCells = mBackward[curState];

                int i = 0;
                while (i < forwardLayer.LayerSize - Vector<double>.Count)
                {
                    Vector<double> v1 = new Vector<double>(forwardCells.cellOutput, i);
                    Vector<double> v2 = new Vector<double>(backwardCells.cellOutput, i);
                    Vector<double> v = (v1 + v2) / vecConst2;

                    v.CopyTo(mergedLayer[curState].cellOutput, i);

                    i += Vector<float>.Count;
                }

                while (i < forwardLayer.LayerSize)
                {
                    mergedLayer[curState].cellOutput[i] = (forwardCells.cellOutput[i] + backwardCells.cellOutput[i]) / 2.0;
                    i++;
                }
            });

            return mergedLayer;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Compute the output of bottom layer
        /// </summary>
        /// <param name="pSequence"></param>
        /// <param name="forwardLayer"></param>
        /// <param name="backwardLayer"></param>
        /// <returns></returns>
        private SimpleLayer[] ComputeBottomLayer(Sequence pSequence, SimpleLayer forwardLayer, SimpleLayer backwardLayer)
        {
            int numStates = pSequence.States.Length;

            SimpleLayer[] mForward  = null;
            SimpleLayer[] mBackward = null;
            Parallel.Invoke(() =>
            {
                //Computing forward RNN
                forwardLayer.netReset(false);
                mForward = new SimpleLayer[numStates];
                for (int curState = 0; curState < numStates; curState++)
                {
                    State state = pSequence.States[curState];
                    SetInputLayer(state, curState, numStates, null);
                    forwardLayer.computeLayer(state.SparseData, state.DenseData.CopyTo());
                    mForward[curState] = forwardLayer.GetHiddenLayer();
                }
            },
                            () =>
            {
                //Computing backward RNN
                backwardLayer.netReset(false);
                mBackward = new SimpleLayer[numStates];
                for (int curState = numStates - 1; curState >= 0; curState--)
                {
                    State state = pSequence.States[curState];
                    SetInputLayer(state, curState, numStates, null, false);
                    backwardLayer.computeLayer(state.SparseData, state.DenseData.CopyTo());       //compute probability distribution

                    mBackward[curState] = backwardLayer.GetHiddenLayer();
                }
            });

            SimpleLayer[] mergedLayer = new SimpleLayer[numStates];
            Parallel.For(0, numStates, parallelOption, curState =>
            {
                State state = pSequence.States[curState];

                mergedLayer[curState] = new SimpleLayer(forwardLayer.LayerSize);
                mergedLayer[curState].SparseFeature = state.SparseData;
                mergedLayer[curState].DenseFeature  = state.DenseData.CopyTo();

                SimpleLayer forwardCells  = mForward[curState];
                SimpleLayer backwardCells = mBackward[curState];

                int i = 0;
                while (i < forwardLayer.LayerSize - Vector <double> .Count)
                {
                    Vector <double> v1 = new Vector <double>(forwardCells.cellOutput, i);
                    Vector <double> v2 = new Vector <double>(backwardCells.cellOutput, i);
                    Vector <double> v  = (v1 + v2) / vecConst2;

                    v.CopyTo(mergedLayer[curState].cellOutput, i);

                    i += Vector <float> .Count;
                }

                while (i < forwardLayer.LayerSize)
                {
                    mergedLayer[curState].cellOutput[i] = (forwardCells.cellOutput[i] + backwardCells.cellOutput[i]) / 2.0;
                    i++;
                }
            });

            return(mergedLayer);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Compute the output of bottom layer
        /// </summary>
        /// <param name="pSequence"></param>
        /// <param name="forwardLayer"></param>
        /// <param name="backwardLayer"></param>
        /// <returns></returns>
        private SimpleLayer[] ComputeBottomLayer(Sequence pSequence, SimpleLayer forwardLayer, SimpleLayer backwardLayer)
        {
            int numStates = pSequence.States.Length;
            SimpleLayer[] mForward = null;
            SimpleLayer[] mBackward = null;
            Parallel.Invoke(() =>
            {
                //Computing forward RNN
                forwardLayer.netReset(false);
                mForward = new SimpleLayer[numStates];
                for (int curState = 0; curState < numStates; curState++)
                {
                    State state = pSequence.States[curState];
                    SetInputLayer(state, curState, numStates, null);
                    forwardLayer.computeLayer(state.SparseData, state.DenseData.CopyTo());
                    mForward[curState] = forwardLayer.GetHiddenLayer();
                }
            },
             () =>
             {
                 //Computing backward RNN
                 backwardLayer.netReset(false);
                 mBackward = new SimpleLayer[numStates];
                 for (int curState = numStates - 1; curState >= 0; curState--)
                 {
                     State state = pSequence.States[curState];
                     SetInputLayer(state, curState, numStates, null, false);
                     backwardLayer.computeLayer(state.SparseData, state.DenseData.CopyTo());      //compute probability distribution

                     mBackward[curState] = backwardLayer.GetHiddenLayer();
                 }
             });

            SimpleLayer[] mergedLayer = new SimpleLayer[numStates];
            Parallel.For(0, numStates, parallelOption, curState =>
            {
                State state = pSequence.States[curState];

                mergedLayer[curState] = new SimpleLayer(forwardLayer.LayerSize);
                mergedLayer[curState].SparseFeature = state.SparseData;
                mergedLayer[curState].DenseFeature = state.DenseData.CopyTo();

                SimpleLayer forwardCells = mForward[curState];
                SimpleLayer backwardCells = mBackward[curState];

                int i = 0;
                while (i < forwardLayer.LayerSize - Vector<double>.Count)
                {
                    Vector<double> v1 = new Vector<double>(forwardCells.cellOutput, i);
                    Vector<double> v2 = new Vector<double>(backwardCells.cellOutput, i);
                    Vector<double> v = (v1 + v2) / vecConst2;

                    v.CopyTo(mergedLayer[curState].cellOutput, i);

                    i += Vector<float>.Count;
                }

                while (i < forwardLayer.LayerSize)
                {
                    mergedLayer[curState].cellOutput[i] = (forwardCells.cellOutput[i] + backwardCells.cellOutput[i]) / 2.0;
                    i++;
                }
            });

            return mergedLayer;
        }