Esempio n. 1
0
        public Vector <float> OutputCoefficients(int output)
        {
            var coefficients = CreateVector.Sparse <float>(outputCount);

            coefficients[output] = 1f;
            return(coefficients);
        }
Esempio n. 2
0
        public Vector <float> Step(Vector <float> inputVector, Vector <float> resultVector = null)
        {
            var combinedInputVector = CreateVector.Sparse <float>(inputCount);

            combinedInputVector[BiasOffset] = 1f;
            for (var ii = 0; ii < randomElements; ii++)
            {
                combinedInputVector[RandomOffset + ii] = (float)random.NextDouble();
            }
            inputVector.CopySubVectorTo(combinedInputVector, 0, inputInputOffset, inputElements);
            for (var ii = 0; ii < states.Count; ii++)
            {
                combinedInputVector[stateInputOffset + ii] = (float)states[ii];
            }
            fetchResult.CopySubVectorTo(combinedInputVector, 0, contentsInputOffset, fetchResult.Count);
            resultVector = stepper.Step(combinedInputVector, resultVector);
            for (var ii = 0; ii < states.Count; ii++)
            {
                states[ii] = resultVector[stateOutputOffset + ii];
            }
            fetchResult = memory.IncrementAndFetch(
                resultVector.SubVector(positionOutputOffset, memory.positionElements),
                resultVector.SubVector(sizeOutputOffset, memory.positionElements),
                resultVector.SubVector(incrementOutputOffset, memory.contentElements));

            return(resultVector);
        }
Esempio n. 3
0
        /// <summary>
        /// Sets a series of values simultaneously.
        /// </summary>
        public void Set(
            Vector <float> position, Vector <float> size, int axis, Vector <float>[] values)
        {
            var pow2Size = CreateVector.Sparse <float>(positionElements, 2f).PointwisePower(size);

            root = root.MaybeExpand(position, pow2Size).Set(position, pow2Size, axis, values, true);
        }
Esempio n. 4
0
        public Terminal Constant(float value)
        {
            var inputCoefficients = CreateVector.Sparse <float>(inputCount);

            inputCoefficients[BiasOffset] = value;
            return(new Terminal(inputCoefficients, () => $"Constant({value}f)"));
        }
Esempio n. 5
0
        private void Init(
            int randomElements, int inputElements, int outputElements,
            List <double> states, int positionElements = 1, int contentElements = 1,
            bool simplifyExpressions = true)
        {
            this.randomElements   = randomElements;
            this.inputElements    = inputElements;
            this.outputElements   = outputElements;
            this.states           = states;
            inputCount            = 1 + randomElements + inputElements + states.Count + contentElements;
            outputCount           = outputElements + states.Count + positionElements * 2 + contentElements;
            inputInputOffset      = 1 + randomElements;
            stateInputOffset      = inputInputOffset + inputElements;
            contentsInputOffset   = stateInputOffset + states.Count;
            stateOutputOffset     = outputElements;
            positionOutputOffset  = stateOutputOffset + states.Count;
            sizeOutputOffset      = positionOutputOffset + positionElements;
            incrementOutputOffset = sizeOutputOffset + positionElements;

            memory      = new Memory(positionElements, contentElements);
            fetchResult = CreateVector.Sparse <float>(contentElements);

            outputs = new Expression[outputCount];
            for (var output = 0; output < outputCount; output++)
            {
                outputs[output] = Constant(0f);
            }

            this.simplifyExpressions = simplifyExpressions;

            stepper = new LeafStepper(this);
        }
Esempio n. 6
0
        private static Vector <float> ActivationCoefficients(Activation activation)
        {
            var coefficients = CreateVector.Sparse <float>(Enum.GetValues(typeof(Activation)).Length);

            coefficients[(int)activation] = 1f;
            return(coefficients);
        }
Esempio n. 7
0
        public override Vector <float> Step(
            Vector <float> inputVector, Vector <float> resultVector = null)
        {
            resultVector = resultVector ?? CreateVector.Sparse <float>(model.outputCount);

            var leftVector = CreateVector.Sparse <float>(model.outputCount);

            if (left != null)
            {
                var outputVector = left.Step(inputVector);
                for (var output = 0; output < model.outputCount; output++)
                {
                    leftVector[output] = leftCoefficients[output].DotProduct(outputVector);
                }
            }
            var rightVector = CreateVector.Sparse <float>(model.outputCount);

            if (right != null)
            {
                var outputVector = right.Step(inputVector);
                for (var output = 0; output < model.outputCount; output++)
                {
                    rightVector[output] = rightCoefficients[output].DotProduct(outputVector);
                }
            }

            var productVector  = leftVector.PointwiseMultiply(rightVector);
            var constantVector = CreateVector.Sparse <float>(model.outputCount, 1);

            productVector.PointwiseMultiply(opCoefficients[0], productVector);
            leftVector.PointwiseMultiply(opCoefficients[1], leftVector);
            rightVector.PointwiseMultiply(opCoefficients[2], rightVector);
            constantVector.PointwiseMultiply(opCoefficients[3], constantVector);

            var identityVector = productVector.Add(leftVector);

            identityVector.Add(rightVector, identityVector);
            identityVector.Add(constantVector, identityVector);

            var binaryStepVector = identityVector.PointwiseSign();

            binaryStepVector.PointwiseMaximum(0f, binaryStepVector);

            var activatedVectors = new [] { identityVector, binaryStepVector };

            var activatedVector = CreateVector.Sparse <float>(activatedVectors.Length);

            for (int output = 0; output < model.outputCount; output++)
            {
                for (int activation = 0; activation < activatedVectors.Length; activation++)
                {
                    activatedVector[activation] = activatedVectors[activation][output];
                }
                resultVector[output] = activationCoefficients[output].DotProduct(activatedVector) +
                                       inputCoefficients[output].DotProduct(inputVector);
            }

            return(resultVector);
        }
Esempio n. 8
0
 private void Init(int positionElements, int contentElements)
 {
     this.positionElements = positionElements;
     this.contentElements  = contentElements;
     root = new LeafNode(
         CreateVector.Sparse <float>(positionElements),
         CreateVector.Sparse <float>(positionElements, 1f),
         CreateVector.Sparse <float>(contentElements));
 }
Esempio n. 9
0
 public override Vector <float> Step(Vector <float> inputVector, Vector <float> resultVector)
 {
     resultVector = resultVector ?? CreateVector.Sparse <float>(model.outputCount);
     for (var output = 0; output < model.outputCount; output++)
     {
         resultVector[output] = inputCoefficients[output].DotProduct(inputVector);
     }
     return(resultVector);
 }
Esempio n. 10
0
        /// <summary>
        /// Increments the values at the specified address by the provided amounts in the supplied sizes.
        /// <summary>
        public Vector <float> IncrementAndFetch(
            Vector <float> position, Vector <float> size, Vector <float> increment)
        {
            var result   = CreateVector.Sparse <float>(contentElements);
            var pow2Size = CreateVector.Sparse <float>(positionElements, 2f).PointwisePower(size);

            root = root.MaybeExpand(position, pow2Size)
                   .IncrementAndFetch(position, pow2Size, increment, result, true);
            return(result);
        }
Esempio n. 11
0
        public Terminal Value(
            int input, float scale = 1f, float offset = 0f, Func <string> stringify = null)
        {
            var inputCoefficients = CreateVector.Sparse <float>(inputCount);

            inputCoefficients[input]      = scale;
            inputCoefficients[BiasOffset] = offset;
            return(new Terminal(inputCoefficients,
                                stringify ?? (() => $"Value({input}, {scale}f, {offset}f)")));
        }
Esempio n. 12
0
        /// <summary>Reads a vector of preestablished length.</summary>
        public static Vector <float> ReadVector(BinaryReader reader, int elements)
        {
            var value = CreateVector.Sparse <float>(elements);

            for (var ii = 0; ii < value.Count; ii++)
            {
                value[ii] = reader.ReadSingle();
            }
            return(value);
        }
Esempio n. 13
0
        public Memory(BinaryReader reader)
        {
            var stream = reader.BaseStream;

            if (stream.Position == stream.Length)
            {
                Init(1, 1);
                return;
            }
            positionElements = reader.ReadInt32();
            contentElements  = reader.ReadInt32();
            var size = BinaryUtil.ReadVector(reader, positionElements);

            root = Node.Read(reader, CreateVector.Sparse <float>(size.Count), size, contentElements);
        }
Esempio n. 14
0
 public Stepper(Model model, Vector <float>[] inputCoefficients = null)
 {
     this.model = model;
     if (inputCoefficients != null)
     {
         this.inputCoefficients = inputCoefficients;
     }
     else
     {
         this.inputCoefficients = new Vector <float> [model.outputCount];
         for (var output = 0; output < model.outputCount; output++)
         {
             this.inputCoefficients[output] = CreateVector.Sparse <float>(model.inputCount);
         }
     }
 }
Esempio n. 15
0
        public IndexedVector(int[] index, float[] values, int numOfDimensions, string label = null)
        {
            var tuples = new Tuple <int, float> [Math.Min(index.Length, numOfDimensions)];

            for (int i = 0; i < index.Length; i++)
            {
                if (i == numOfDimensions)
                {
                    break;
                }

                tuples[i] = new Tuple <int, float>(index[i], values[i]);
            }

            Value = CreateVector.Sparse(
                SparseVectorStorage <float> .OfIndexedEnumerable(numOfDimensions, tuples));

            Label = label;
        }
Esempio n. 16
0
        public InternalStepper(Model model, Vector <float>[] inputCoefficients)
            : base(model, inputCoefficients)
        {
            opCoefficients = new [] {
                CreateVector.Sparse <float>(model.outputCount), // lr
                CreateVector.Sparse <float>(model.outputCount), // l
                CreateVector.Sparse <float>(model.outputCount), // r
                CreateVector.Sparse <float>(model.outputCount), // c
            };
            leftCoefficients       = new Vector <float> [model.outputCount];
            rightCoefficients      = new Vector <float> [model.outputCount];
            activationCoefficients = new Vector <float> [model.outputCount];

            for (var output = 0; output < model.outputCount; output++)
            {
                leftCoefficients[output]       = model.OutputCoefficients(output);
                rightCoefficients[output]      = model.OutputCoefficients(output);
                activationCoefficients[output] = ActivationCoefficients(Activation.Identity);
            }
        }
Esempio n. 17
0
        public IndexedVector(int[] index, float[] values, int vectorWidth, Memory <char>?data = null)
        {
            var tuples = new Tuple <int, float> [Math.Min(index.Length, vectorWidth)];

            for (int i = 0; i < index.Length; i++)
            {
                if (i == vectorWidth)
                {
                    break;
                }

                tuples[i] = new Tuple <int, float>(index[i], values[i]);
            }

            Value = CreateVector.Sparse(
                SparseVectorStorage <float> .OfIndexedEnumerable(vectorWidth, tuples));

            ComponentCount = tuples.Length;

            Data = data;
        }
Esempio n. 18
0
        public IndexedVector(IList <float> values, int vectorWidth, Memory <char>?data = null)
        {
            var tuples = new Tuple <int, float> [Math.Min(values.Count, vectorWidth)];
            var i      = 0;

            foreach (var x in values)
            {
                if (i == (vectorWidth))
                {
                    break;
                }

                tuples[i] = new Tuple <int, float>(i, x);

                i++;
            }

            Value = CreateVector.Sparse(
                SparseVectorStorage <float> .OfIndexedEnumerable(vectorWidth, tuples));

            ComponentCount = tuples.Length;

            Data = data;
        }
Esempio n. 19
0
 public IndexedVector(IEnumerable <float> values, string label = null)
 {
     Value = CreateVector.Sparse(SparseVectorStorage <float> .OfEnumerable(values));
     Label = label;
 }
Esempio n. 20
0
            public Node MaybeExpand(Vector <float> position, Vector <float> size)
            {
                // see if we have to expand on any axis
                var maxDiff  = 0f;
                var maxIndex = -1;

                for (var ii = 0; ii < size.Count; ii++)
                {
                    var pointCenter = position[ii];
                    var pointExtent = size[ii] * 0.5f;
                    var pointMin    = pointCenter - pointExtent;
                    var pointMax    = pointCenter + pointExtent;
                    var thisCenter  = this.position[ii];
                    var thisExtent  = this.size[ii] * 0.5f;
                    var thisMin     = thisCenter - thisExtent;
                    var thisMax     = thisCenter + thisExtent;
                    var diff        = Math.Max(thisMin - pointMin, pointMax - thisMax);
                    if (diff > maxDiff)
                    {
                        (maxDiff, maxIndex) = (diff, ii);
                    }
                }
                if (maxIndex < 0)
                {
                    return(this);
                }

                var maxIndexSize = this.size[maxIndex];

                var expandedSize = this.size.Clone();

                expandedSize[maxIndex] = maxIndexSize * 2f;
                var contractedSize = this.size.Clone();

                contractedSize[maxIndex] = maxIndexSize * 0.5f;

                var leftPosition = this.position.Clone();

                leftPosition[maxIndex] -= maxIndexSize * 0.5f;
                var leftLeftPosition = leftPosition.Clone();

                leftLeftPosition[maxIndex] -= maxIndexSize * 0.25f;
                var left = new InternalNode(
                    leftPosition, this.size, value.Multiply(0.5f), maxIndex,
                    new LeafNode(leftLeftPosition, contractedSize, CreateVector.Sparse <float>(value.Count)),
                    SplitLeft(maxIndex));

                var rightPosition = this.position.Clone();

                rightPosition[maxIndex] += maxIndexSize * 0.5f;
                var rightRightPosition = rightPosition.Clone();

                rightRightPosition[maxIndex] += maxIndexSize * 0.25f;
                var right = new InternalNode(
                    rightPosition, this.size, value.Multiply(0.5f), maxIndex,
                    SplitRight(maxIndex),
                    new LeafNode(rightRightPosition, contractedSize,
                                 CreateVector.Sparse <float>(value.Count)));

                return(new InternalNode(
                           this.position, expandedSize, value.Clone(),
                           maxIndex, left, right)
                       .MaybeExpand(position, size));
            }
Esempio n. 21
0
 public IndexedVector(int numOfDimensions, object label = null)
 {
     Value = CreateVector.Sparse <float>(numOfDimensions);
     Label = label;
 }
Esempio n. 22
0
        public override Expression Compact()
        {
            var emptyVector = CreateVector.Sparse <float>(inputCoefficients.Count);

            return(inputCoefficients.Equals(emptyVector) ? null : this);
        }
Esempio n. 23
0
 public IndexedVector(int numOfDimensions)
 {
     Value = CreateVector.Sparse(SparseVectorStorage <float> .OfEnumerable(new float[numOfDimensions]));
 }