Example #1
0
 public ModifierCell(RecurrentCell base_cell)
     : base(base_cell.Prefix + base_cell.Alias(), base_cell.Params)
 {
     BaseCell = base_cell;
 }
Example #2
0
                public override Batch <double[]> Forward(Batch <double[]> inputs, bool isTraining)
                {
                    var length1  = this.inputs * this.outputs;
                    var length2  = this.outputs * this.outputs;
                    var length3  = this.timesteps * this.outputs;
                    var xWeights = new double[length1];
                    var hWeights = new double[length2];
                    var outputs  = new double[inputs.Size][];

                    for (int i = 0; i < length1; i++)
                    {
                        xWeights[i] = this.weights[i];
                    }

                    for (int i = 0, j = length1; i < length2; i++, j++)
                    {
                        hWeights[i] = this.weights[j];
                    }

                    for (int i = 0; i < inputs.Size; i++)
                    {
                        outputs[i] = new double[length3];
                    }

                    this.layerList = new List <RecurrentCell>();

                    if (!this.stateful || this.h == null)
                    {
                        this.h = new Batch <double[]>(new double[inputs.Size][]);

                        for (int i = 0; i < inputs.Size; i++)
                        {
                            this.h[i] = new double[this.outputs];

                            for (int j = 0; j < this.outputs; j++)
                            {
                                this.h[i][j] = 0.0;
                            }
                        }
                    }
                    else if (this.h.Size < inputs.Size)
                    {
                        var batch = new Batch <double[]>(new double[inputs.Size][]);

                        for (int i = 0; i < this.h.Size; i++)
                        {
                            batch[i] = this.h[i];
                        }

                        for (int i = this.h.Size; i < inputs.Size; i++)
                        {
                            batch[i] = new double[this.outputs];

                            for (int j = 0; j < this.outputs; j++)
                            {
                                batch[i][j] = 0.0;
                            }
                        }

                        this.h = batch;
                    }

                    for (int t = 0; t < this.timesteps; t++)
                    {
                        var layer = new RecurrentCell(this.inputs, this.outputs, xWeights, hWeights, this.biases, this.tanhActivationFunction);
                        var x     = new Batch <double[]>(new double[inputs.Size][]);

                        for (int i = 0; i < inputs.Size; i++)
                        {
                            var vector = new double[this.inputs];

                            for (int j = 0, k = this.inputs * t; j < this.inputs; j++, k++)
                            {
                                vector[j] = inputs[i][k];
                            }

                            x[i] = vector;
                        }

                        this.h = layer.Forward(x, this.h);

                        for (int i = 0; i < inputs.Size; i++)
                        {
                            for (int j = 0, k = this.outputs * t; j < this.outputs; j++, k++)
                            {
                                outputs[i][k] = this.h[i][j];
                            }
                        }

                        this.layerList.Add(layer);
                    }

                    return(new Batch <double[]>(outputs));
                }