Пример #1
0
        private static void createWeightedSE()
        {
            NDShape shape     = new int[] { 15 };
            var     actual    = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
            var     predicted = new float[] { -5, -2, 1, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37 };
            var     weights   = new NDArrayView(shape, new float[] { 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 }, device);
            var     aValue    = Value.CreateBatch(shape, actual, device, false);
            var     pValue    = Value.CreateBatch(shape, predicted, device, false);
            var     pWeights  = new Constant(weights); //Value.CreateBatch(shape, predicted, device, false);

            //define variables
            var vactual    = Variable.InputVariable(shape, DataType.Float);
            var vpredicted = Variable.InputVariable(shape, DataType.Float);

            //calculate weighted squared error
            var loss = StatMetrics.WeightedSE(vpredicted, vactual, pWeights);

            //evaluate function
            var inMap = new Dictionary <Variable, Value>()
            {
                { vpredicted, pValue }, { vactual, aValue }
            };

            var outMap = new Dictionary <Variable, Value>()
            {
                { loss, null }
            };

            loss.Evaluate(inMap, outMap, device);

            //
            var result = outMap[loss].GetDenseData <float>(loss);

            Assert.Equal((double)result[0][0], (double)7680.0, 2);
        }
Пример #2
0
 public Value(NDArrayView data, NDMask mask) : this(CNTKLibPINVOKE.new_Value__SWIG_1(NDArrayView.getCPtr(data), NDMask.getCPtr(mask)), true)
 {
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Пример #3
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);
        }
Пример #4
0
        public static NDArrayView SafeCreate(NDShape shape, float[] data, DeviceDescriptor device)
        {
            if (device == null)
            {
                device = DeviceDescriptor.UseDefaultDevice();
            }

            if (device == DeviceDescriptor.CPUDevice)
            {
                unsafe
                {
                    fixed(float *f = data)
                    {
                        using (var a = new NDArrayView(shape, data, DeviceDescriptor.CPUDevice, false))
                        {
                            // The NDArrayview constructor with float[] data does not copy nor hold the reference to the source data.
                            // To make the object keep track of its data by itself, make a copy of it by calling DeepClone().
                            return(a.DeepClone(device, false));
                        }
                    }
                }
            }

            // Allocating in GPU memory inevitably causes copying.
            return(new NDArrayView(shape, data, device, false));
        }
Пример #5
0
        static void Main(string[] args)
        {
            int[]   shape = new int[] { 6, 3 };
            float[] data  = new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            NDArrayView array       = new NDArrayView(shape, data, DeviceDescriptor.CPUDevice);
            Variable    variable    = new Variable(shape, VariableKind.Parameter, CNTK.DataType.Float, array, false, new AxisVector(), false, "", "");
            var         slicedData  = CNTKLib.Slice(variable, AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat(1, 1), IntVector.Repeat(3, 1));
            var         resultArray = GetArray(slicedData);


            Global.UseEngine(SiaNet.Backend.CNTKLib.SiaNetBackend.Instance, DeviceType.CPU);
            var K = Global.CurrentBackend;

            var a = K.CreateVariable(new float[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, new long[] { 6, 3 });

            a.Print();

            var sliced = K.SliceRows(a, 1, 2);

            //var d = K.CreateVariable(new float[] { 1 }, new long[] { 1, 1 });
            //c = c + d;

            Console.ReadLine();
        }
Пример #6
0
 public void CopyFrom(NDArrayView source)
 {
     CNTKLibPINVOKE.NDArrayView_CopyFrom(swigCPtr, NDArrayView.getCPtr(source));
     if (CNTKLibPINVOKE.SWIGPendingException.Pending)
     {
         throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
     }
 }
Пример #7
0
        private NDArrayView _SliceView(SizeTVector startOffset, SizeTVector extent)
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.NDArrayView__SliceView__SWIG_1(swigCPtr, SizeTVector.getCPtr(startOffset), SizeTVector.getCPtr(extent));
            NDArrayView           ret  = (cPtr == global::System.IntPtr.Zero) ? null : new NDArrayView(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #8
0
        public NDArrayView AsShape(NDShape newShape)
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.NDArrayView_AsShape(swigCPtr, NDShape.getCPtr(newShape));
            NDArrayView           ret  = (cPtr == global::System.IntPtr.Zero) ? null : new NDArrayView(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #9
0
        public NDArrayView DeepClone()
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.NDArrayView_DeepClone__SWIG_3(swigCPtr);
            NDArrayView           ret  = (cPtr == global::System.IntPtr.Zero) ? null : new NDArrayView(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #10
0
        private NDArrayView _Alias()
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.NDArrayView__Alias__SWIG_1(swigCPtr);
            NDArrayView           ret  = (cPtr == global::System.IntPtr.Zero) ? null : new NDArrayView(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #11
0
        public NDArrayView DeepClone(DeviceDescriptor device, bool readOnly)
        {
            global::System.IntPtr cPtr = CNTKLibPINVOKE.NDArrayView_DeepClone__SWIG_0(swigCPtr, DeviceDescriptor.getCPtr(device), readOnly);
            NDArrayView           ret  = (cPtr == global::System.IntPtr.Zero) ? null : new NDArrayView(cPtr, true);

            if (CNTKLibPINVOKE.SWIGPendingException.Pending)
            {
                throw CNTKLibPINVOKE.SWIGPendingException.Retrieve();
            }
            return(ret);
        }
Пример #12
0
        public Tensor variable(Array array, DataType?dtype = null, string name = null)
        {
            // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py#L133

            NDArrayView v = In(array, dtype);

            var p = new Parameter(v, name: _prepare_name(name, "variable"));

            Tensor t = Out(p);

            return(t);
        }
Пример #13
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);
            }
Пример #14
0
        public void TestDataIngegrityAfterGarbageCollection()
        {
            for (var i = 0; i < 100; ++i)
            {
                var data = new float[] { 1, 2, 3, 4, 5, 6 };

                MinibatchData m;
                IntPtr sharedPtrAddress;
                IntPtr valueAddress;
                int c1;
                unsafe
                {
                    fixed (float* d = data)
                    {
                        var a = new NDArrayView(new int[] { 3, 2 }, data, DeviceDescriptor.CPUDevice);
                        var a2 = a.DeepClone();
                        var value = new Value(a2);
                        c1 = SwigMethods.GetSharedPtrUseCount(value);
                        sharedPtrAddress = SwigMethods.GetSwigPointerAddress(value);
                        valueAddress = SwigMethods.GetSharedPtrElementPointer(value);

                        m = new MinibatchData(value);
                    }
                }

                var c2 = SwigMethods.GetSharedPtrUseCount(m.data);
                var sharedPtrAddress2 = SwigMethods.GetSwigPointerAddress(m.data);
                var valueAddress2 = SwigMethods.GetSharedPtrElementPointer(m.data);
                var c3 = SwigMethods.GetSharedPtrUseCount(m.data);

                GC.Collect();
                GC.Collect();
                GC.Collect();

                var c4 = SwigMethods.GetSharedPtrUseCount(m.data);
                var sharedPtrAddress3 = SwigMethods.GetSwigPointerAddress(m.data);
                var valueAddress3 = SwigMethods.GetSharedPtrElementPointer(m.data);

                var ds = DataSourceFactory.FromValue(m.data);
                Assert.AreEqual(6, ds.Data.Count);
                CollectionAssert.AreEqual(new int[] { 3, 2 }, ds.Shape.Dimensions);
                CollectionAssert.AreEqual(new float[] { 1, 2, 3, 4, 5, 6 }, ds.TypedData);
            }
        }
Пример #15
0
        public static void TestElementTimes()
        {
            var device = DeviceDescriptor.GPUDevice(0);
            // todo put in different values
            CNTKDictionary testInitializer = new CNTKDictionary();

            Parameter   leftOperand = new Parameter(new int[] { 2, 2 }, 1f, device, "left");
            NDArrayView initValues  = new NDArrayView(new int[] { 2, 2 }, new float[] { 0f, 1f, 2f, 3f }, device);

            leftOperand.SetValue(initValues);
            // leftOperand looks like:
            // 0  1
            // 4  9

            Parameter rightOperand = new Parameter(new int[] { 2, 2, 2 }, 1f, device, "right");

            initValues = new NDArrayView(new int[] { 2, 2, 2 }, new float[] { 0f, 1f, 2f, 3f, 4f, 5f, 6f, 7f }, device);
            rightOperand.SetValue(initValues);
            // rightOperand looks like:
            // 0  1 |  4  5
            // 2  3 |  6  7

            Function model = CNTKLib.ElementTimes(leftOperand, rightOperand);

            var inputVariable = model.Inputs.First();
            var inputMap      = new Dictionary <Variable, Value>();

            var outputVariable = model.Output;
            var outputDataMap  = new Dictionary <Variable, Value>()
            {
                { outputVariable, null }
            };

            model.Evaluate(inputMap, outputDataMap, device);

            var output      = outputDataMap[outputVariable];
            var outputArray = output.GetDenseData <float>(outputVariable).First();
            // output looks like:
            // 0  1 |  0  5
            // 4  9 | 12 21

            // conclusion of this test: CNTKLib.ElementTimes works as espected :-)
        }
Пример #16
0
        public Tensor variable(Array array, string name = null)
        {
            // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/cntk_backend.py#L133

            //if isinstance(value, C.variables.Constant)
            //    or isinstance(value, C.variables.Parameter):
            //    value = value.value

            // we don't support init parameter with symbolic op, so eval it first as
            // workaround
            //if isinstance(value, C.cntk_py.Function):
            //    value = eval(value)

            NDArrayView v = In(array);

            var p = new Parameter(v, name: _prepare_name(name, "variable"));

            Tensor t = Out(p);

            return(t);
        }
Пример #17
0
        public void TestDataIngegrityAfterGarbageCollection()
        {
            for (var i = 0; i < 100; ++i)
            {
                NDArrayView a2;
                using (var a = new NDArrayView(new int[] { 3, 2 }, new float[] { 1, 2, 3, 4, 5, 6 }, DeviceDescriptor.CPUDevice))
                {
                    a2 = a.DeepClone();
                }

                GC.Collect();

                var value = new Value(a2);

                GC.Collect();

                var ds = DataSourceFactory.FromValue(value);
                Assert.AreEqual(6, ds.Data.Count);
                CollectionAssert.AreEqual(new int[] { 3, 2 }, ds.Shape.Dimensions);
                CollectionAssert.AreEqual(new float[] { 1, 2, 3, 4, 5, 6 }, ds.TypedData);
            }
        }
Пример #18
0
        private Constant _constant <T>(T value, int?[] shape, DataType?dtype, string name)
        {
            if (value is Array)
            {
                NDArrayView x = In((value as Array).Convert(dtype.ToType()));
                if (name != null)
                {
                    return(new Constant(x, name));
                }
                else
                {
                    return(new Constant(x));
                }
            }

            if (value is double)
            {
                return(Constant.Scalar <double>(MatrixEx.To <double>(value), device: DeviceDescriptor.CPUDevice));
            }
            if (value is float)
            {
                return(Constant.Scalar <double>(MatrixEx.To <float>(value), device: DeviceDescriptor.CPUDevice));
            }

            if (name == null)
            {
                return(new Constant(shape: InShape(shape),
                                    dataType: In(dtype.Value),
                                    initValue: (dynamic)value,
                                    device: DeviceDescriptor.CPUDevice));
            }

            return(new Constant(shape: InShape(shape),
                                dataType: In(dtype.Value),
                                initValue: (dynamic)value,
                                device: DeviceDescriptor.CPUDevice,
                                name: name));
        }