Exemplo n.º 1
0
        // Todo: move it to separate unit tests.
        public void NDArrayViewTest(DeviceDescriptor device)
        {
            Console.WriteLine("Test creating NDArrayView on devices.");

            var data       = new float[10];
            var shape      = new NDShape(1, 10);
            var n1         = new NDArrayView(shape, data, device);
            var n1Clone    = n1.DeepClone(device);
            var n1CloneCPU = n1.DeepClone(DeviceDescriptor.CPUDevice);

            Console.WriteLine("n1: on " + n1.Device.AsString() + ", Storage:" + n1.StorageFormat + ", Shape:" + n1.Shape.AsString());
            Console.WriteLine("n1Clone: on " + n1Clone.Device.AsString() + ", Storage:" + n1Clone.StorageFormat + ", Shape:" + n1Clone.Shape.AsString());
            Console.WriteLine("n1CloneCPU: on " + n1CloneCPU.Device.AsString() + ", Storage:" + n1CloneCPU.StorageFormat + ", Shape:" + n1CloneCPU.Shape.AsString());

            int[] dimensions = { 4, 5 };
            var   shape2     = NDShape.CreateNDShape(dimensions);

            float[] nonZeroValues = { 1, 5, 4, 2, 3, 9, 7, 8, 6 };
            int[]   rowIndices    = { 0, 2, 0, 1, 1, 3, 2, 2, 3 };
            int[]   colStarts     = { 0, 2, 4, 6, 7, 9 };
            var     s1            = new NDArrayView(shape2, colStarts, rowIndices, nonZeroValues, device, true);
            var     s1Clone       = s1.DeepClone(device);
            var     s1DenseCPU    = new NDArrayView(DataType.Float, StorageFormat.Dense, shape2, DeviceDescriptor.CPUDevice);

            s1DenseCPU.CopyFrom(s1);

            Console.WriteLine("s1: on " + s1.Device.AsString() + ", Storage:" + s1.StorageFormat + ", Shape:" + s1.Shape.AsString());
            Console.WriteLine("s1Clone: on " + s1Clone.Device.AsString() + ", Storage:" + s1Clone.StorageFormat + ", Shape:" + s1Clone.Shape.AsString());
            Console.WriteLine("s1DenseCPU: on " + s1DenseCPU.Device.AsString() + ", Storage:" + s1DenseCPU.StorageFormat + ", Shape:" + s1DenseCPU.Shape.AsString());
        }
Exemplo n.º 2
0
        //add zeros to the input so that input has a larger width
        public static Function AddDummy(Variable input, int numToAdd, DeviceDescriptor device)
        {
            int inputSize = input.Shape[0];

            //create the dummy data
            float[] addValues = new float[numToAdd];
            for (int i = 0; i < numToAdd; i++)
            {
                addValues[i] = 0;
            }

            var addArray = new NDArrayView(DataType.Float, new int[] { numToAdd }, device);

            addArray.CopyFrom(new NDArrayView(new int[] { numToAdd }, addValues, (uint)addValues.Length, device));
            var dummyData = new Constant(addArray);

            //append the dummy data to the input
            var vars = new VariableVector();

            vars.Add(input);
            vars.Add(dummyData);
            var added = CNTKLib.Splice(vars, new Axis(0));

            return(added);
        }
Exemplo n.º 3
0
            private static Constant GetProjectionMap(int outputDim, int inputDim, DeviceDescriptor device)
            {
                if (inputDim > outputDim)
                    throw new Exception("Can only project from lower to higher dimensionality");

                float[] projectionMapValues = new float[inputDim * outputDim];
                for (int i = 0; i < inputDim * outputDim; i++)
                    projectionMapValues[i] = 0;
                for (int i = 0; i < inputDim; ++i)
                    projectionMapValues[(i * (int)inputDim) + i] = 1.0f;

                var projectionMap = new NDArrayView(DataType.Float, new int[] { 1, 1, inputDim, outputDim }, device);
                projectionMap.CopyFrom(new NDArrayView(new int[] { 1, 1, inputDim, outputDim }, projectionMapValues, (uint)projectionMapValues.Count(), device));

                return new Constant(projectionMap);
            }
Exemplo n.º 4
0
        public static void EvaluateUsingCreateValue()
        {
            // Load the model
            var myFunc = Function.LoadModel("01_OneHidden");

            // Ouput funciton info.
            OutputFunctionInfo(myFunc);

            // prepare input for evaluation
            uint numOfSamples = 1;

            const string inputNodeName = "features";
            var          inputVar      = myFunc.Arguments.Where(variable => string.Equals(variable.Name, inputNodeName)).Single();
            // Todo: get size directly from inputVar.
            uint numOfInputData = inputVar.Shape.TotalSize;
            var  inputData      = new List <float>();

            for (uint i = 0; i < numOfInputData; ++i)
            {
                inputData.Add(i % 255);
            }

            var inputVector = new FloatVector(inputData);
            var data        = new FloatVectorVector()
            {
                inputVector
            };
            // Create value directly from data.
            var inputValue = Value.CreateDenseFloat(inputVar.Shape, data, DeviceDescriptor.CPUDevice);

            // Create input map
            // Todo: create a Dictionary wrapper?
            var inputMap = new UnorderedMapVariableValuePtr();

            inputMap.Add(inputVar, inputValue);

            // Prepare output
            const string outputNodeName = "out.z_output";
            var          outputVar      = myFunc.Outputs.Where(variable => string.Equals(variable.Name, outputNodeName)).Single();

            // Create ouput map. Using null as Value to indicate using system allocated memory.
            var outputMap = new UnorderedMapVariableValuePtr();

            outputMap.Add(outputVar, null);

            // Evalaute
            // Todo: test on GPUDevice()?
            myFunc.Evaluate(inputMap, outputMap, DeviceDescriptor.CPUDevice);

            // Get output value after evaluation
            var outputValue       = outputMap[outputVar];
            var outputNDArrayView = outputValue.Data();

            var dynamicAxisShape = new SizeTVector()
            {
                1, numOfSamples
            };
            var outputShape = outputVar.Shape.AppendShape(new NDShape(dynamicAxisShape));

            // Copy the data from the output buffer.
            // Todo: directly access the data in output buffer if it is on CPU?
            uint numOfOutputData = outputNDArrayView.Shape().TotalSize;

            float[] outputData           = new float[numOfOutputData];
            var     cpuOutputNDArrayView = new NDArrayView(outputShape, outputData, numOfOutputData, DeviceDescriptor.CPUDevice);

            cpuOutputNDArrayView.CopyFrom(outputNDArrayView);

            // Output results
            Console.WriteLine("Evaluation results:");
            for (uint i = 0; i < numOfOutputData; ++i)
            {
                Console.WriteLine(outputData[i]);
            }
        }
Exemplo n.º 5
0
        public static void EvaluateUsingSystemAllocatedMemory()
        {
            // Load the model
            var myFunc = Function.LoadModel("z.model");

            // Ouput funciton info.
            OutputFunctionInfo(myFunc);

            // prepare input for evaluation
            uint numOfSamples = 1;

            // Only one input for the model.
            var inputVar = myFunc.Arguments.First();
            // Todo: get size directly from inputVar.
            uint numOfInputData = inputVar.Shape.TotalSize * numOfSamples;

            float[] inputData = new float[numOfInputData];
            for (uint i = 0; i < numOfInputData; ++i)
            {
                inputData[i] = (float)(i % 255);
            }

            // Todo: create value directly from data.
            var dynamicAxisShape = new SizeTVector()
            {
                1, numOfSamples
            };
            var inputShape       = inputVar.Shape.AppendShape(new NDShape(dynamicAxisShape));
            var inputNDArrayView = new NDArrayView(inputShape, inputData, numOfInputData, DeviceDescriptor.CPUDevice);
            var inputValue       = new Value(inputNDArrayView);

            // Create input map
            // Todo: create a Dictionary wrapper?
            var inputMap = new UnorderedMapVariableValuePtr();

            inputMap.Add(inputVar, inputValue);

            // Prepare output. The model has only one output.
            var outputVar = myFunc.Output;

            // Create ouput map. Using null as Value to indicate using system allocated memory.
            var outputMap = new UnorderedMapVariableValuePtr();

            outputMap.Add(outputVar, null);

            // Evalaute
            // Todo: test on GPUDevice()?
            myFunc.Evaluate(inputMap, outputMap, DeviceDescriptor.CPUDevice);

            // Get output value after evaluation
            var outputValue       = outputMap[outputVar];
            var outputNDArrayView = outputValue.Data();
            var outputShape       = outputVar.Shape.AppendShape(new NDShape(dynamicAxisShape));

            // Copy the data from the output buffer.
            // Todo: directly access the data in output buffer if it is on CPU?
            uint numOfOutputData = outputNDArrayView.Shape().TotalSize;

            float[] outputData           = new float[numOfOutputData];
            var     cpuOutputNDArrayView = new NDArrayView(outputShape, outputData, numOfOutputData, DeviceDescriptor.CPUDevice);

            cpuOutputNDArrayView.CopyFrom(outputNDArrayView);

            // Output results
            Console.WriteLine("Evaluation results:");
            for (uint i = 0; i < numOfOutputData; ++i)
            {
                Console.WriteLine(outputData[i]);
            }
        }