public AxisVector(AxisVector other) : this(CNTKLibPINVOKE.new_AxisVector__SWIG_1(AxisVector.getCPtr(other)), true) { if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } }
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(); }
public AxisVectorEnumerator(AxisVector collection) { collectionRef = collection; currentIndex = -1; currentObject = null; currentSize = collectionRef.Count; }
public QNetworkSimple(int stateSize, int actionSize, int numLayers, int hiddenSize, DeviceDescriptor device, float initialWeightScale = 0.01f) { Device = device; StateSize = stateSize; ActionSize = actionSize; //create actor network part var inputA = new InputLayerDense(stateSize); var outputA = new OutputLayerDense(hiddenSize, null, OutputLayerDense.LossFunction.None); outputA.HasBias = false; outputA.InitialWeightScale = initialWeightScale; SequentialNetworkDense qNetwork = new SequentialNetworkDense(inputA, LayerDefineHelper.DenseLayers(numLayers, hiddenSize, false, NormalizationMethod.None, 0, initialWeightScale, new ReluDef()), outputA, device); //seperate the advantage and value part. It is said to be better var midStream = outputA.GetOutputVariable(); var advantageStream = CNTKLib.Slice(midStream, AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat(0, 1), IntVector.Repeat(hiddenSize / 2, 1)); var valueStream = CNTKLib.Slice(midStream, AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat(hiddenSize / 2, 1), IntVector.Repeat(hiddenSize, 1)); var adv = Layers.Dense(advantageStream, actionSize, device, false, "QNetworkAdvantage", initialWeightScale); var value = Layers.Dense(valueStream, 1, device, false, "QNetworkValue", initialWeightScale); InputState = inputA.InputVariable; //OutputQs = outputA.GetOutputVariable(); OutputQs = value.Output + CNTKLib.Minus(adv, CNTKLib.ReduceMean(adv, Axis.AllStaticAxes())).Output; }
protected override IEnumerable <DxfPoint> GetExtentsPoints() { var radiusX = new DxfVector(Radius, 0.0, 0.0); var radiusY = new DxfVector(0.0, Radius, 0.0); // base of the helix yield return(StartPoint + radiusX); yield return(StartPoint - radiusX); yield return(StartPoint + radiusY); yield return(StartPoint - radiusY); // other side of the helix var endPoint = StartPoint + AxisVector.Normalize() * NumberOfTurns * TurnHeight; yield return(endPoint + radiusX); yield return(endPoint - radiusX); yield return(endPoint + radiusY); yield return(endPoint - radiusY); }
public void SetRange(int index, AxisVector values) { CNTKLibPINVOKE.AxisVector_SetRange(swigCPtr, index, AxisVector.getCPtr(values)); if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } }
public static AxisVector UnknownDynamicAxes() { AxisVector ret = new AxisVector(CNTKLibPINVOKE.Axis_UnknownDynamicAxes(), false); if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public static AxisVector DefaultInputVariableDynamicAxes() { AxisVector ret = new AxisVector(CNTKLibPINVOKE.Axis_DefaultInputVariableDynamicAxes(), false); if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
private AxisVector _DynamicAxes() { AxisVector ret = new AxisVector(CNTKLibPINVOKE.Variable__DynamicAxes(swigCPtr), false); if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public AxisVector GetRange(int index, int count) { global::System.IntPtr cPtr = CNTKLibPINVOKE.AxisVector_GetRange(swigCPtr, index, count); AxisVector ret = (cPtr == global::System.IntPtr.Zero) ? null : new AxisVector(cPtr, true); if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public static AxisVector Repeat(Axis value, int count) { global::System.IntPtr cPtr = CNTKLibPINVOKE.AxisVector_Repeat(Axis.getCPtr(value), count); AxisVector ret = (cPtr == global::System.IntPtr.Zero) ? null : new AxisVector(cPtr, true); if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } return(ret); }
public static Function Upsample2D2(Variable input, int xOffset = 0, int yOffset = 0, string name = "") { var xr = CNTKLib.Reshape(input, new int[] { 1, input.Shape[0], 1, input.Shape[1], input.Shape[2] }); var xx = CNTKLib.Splice(VariableVector.Repeat(xr, 2), new Axis(0)); var xy = CNTKLib.Splice(VariableVector.Repeat(xx, 2), new Axis(2)); var r = CNTKLib.Reshape(xy, new int[] { input.Shape[0] * 2, input.Shape[1] * 2, input.Shape[2] }); var sliceAxis = new AxisVector(); sliceAxis.Add(new Axis(0)); sliceAxis.Add(new Axis(1)); r = CNTKLib.Slice(r, sliceAxis, new IntVector(new int[] { xOffset, yOffset }), new IntVector(new int[] { input.Shape[0] * 2, input.Shape[1] * 2 })); //name if (!string.IsNullOrEmpty(name)) { r.SetName(name); } return(r); }
protected override Function BuildNetwork(Variable input, DeviceDescriptor device, string name) { var c1 = UnityCNTK.Layers.Dense(input, HiddenSize, device, true, name + ".Dense", InitialWeightScale); resultOutput = CNTKLib.Slice(c1, AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat(0, 1), IntVector.Repeat(HiddenSize - 1, 1)); varianceOutput = CNTKLib.Square(CNTKLib.Slice(c1, AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat(HiddenSize - 1, 1), IntVector.Repeat(HiddenSize, 1))); targetInput = CNTKLib.InputVariable(resultOutput.Shape, resultOutput.DataType, name + ".TargetInput"); var squareErrorRoot = CNTKLib.Sqrt(CNTKLib.SquaredError(resultOutput, targetInput)); var l = CNTKLib.ElementDivide(squareErrorRoot, CNTKLib.ElementTimes(Constant.Scalar(DataType.Float, 2), varianceOutput)); lossOutput = l.Output + CNTKLib.ElementTimes(CNTKLib.Log(varianceOutput), Constant.Scalar(DataType.Float, 1)); //lossOutput = squareError;//test //add parameters to list ParameterNames.Add(ParamTypeToName(DenseParamType.Weight)); ParameterNames.Add(ParamTypeToName(DenseParamType.Bias)); return(lossOutput); }
private static double[][] sum(params int[] axes) { var arr = new[] { /* total: * /* */1.0, 2.0, 3.0, /* 6.0 */ /* */ 4.0, 5.0, 6.0, /* 15.0 */ /* total: 5.0, 7.0, 9.0 21.0 */ }; var shape = NDShape.CreateNDShape(new[] { 2, 3 }); Value vx = Value.CreateBatch(shape, arr, DeviceDescriptor.CPUDevice, readOnly: true); Variable x = Variable.InputVariable(shape, CNTK.DataType.Double, name: "input"); CNTK.Function f; if (axes == null) { f = CNTKLib.Alias(x); } else { var axisVector = new AxisVector(axes.Select(ax => new Axis(ax)).ToArray()); f = CNTKLib.ReduceSum(x, axis: axisVector); } var inputs = new Dictionary <Variable, Value>() { { x, vx } }; var outputs = new Dictionary <Variable, Value>() { { f, null } }; f.Evaluate(inputs, outputs, DeviceDescriptor.CPUDevice); var r = outputs[f].GetDenseData <double>((Variable)f); return(r.Select(ri => ri.ToArray()).ToArray()); }
public Tensor SliceRows(Tensor x, long start, long end) { return(Out(C.Slice(In(x), AxisVector.Repeat(new Axis(0), 1), IntVector.Repeat((int)start, 1), IntVector.Repeat((int)end, 1)))); }
public Variable(NDShape shape, VariableKind varType, DataType dataType, NDArrayView value, bool needsGradient, AxisVector dynamicAxes, bool isSparse, string name, string uid) : this(CNTKLibPINVOKE.new_Variable__SWIG_3(NDShape.getCPtr(shape), (int)varType, (int)dataType, NDArrayView.getCPtr(value), needsGradient, AxisVector.getCPtr(dynamicAxes), isSparse, name, uid), true) { if (CNTKLibPINVOKE.SWIGPendingException.Pending) { throw CNTKLibPINVOKE.SWIGPendingException.Retrieve(); } }
internal static global::System.Runtime.InteropServices.HandleRef getCPtr(AxisVector obj) { return((obj == null) ? new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero) : obj.swigCPtr); }
public static void ValueCopyToSparseCSCTest <T>(DeviceDescriptor device) { try { // uint numAxes = 2; int dimension = 3; IEnumerable <int> dims = new List <int>() { dimension }; var sampleShape = NDShape.CreateNDShape(dims); int seqLen = 1; int[] colStarts = { 0, 1 }; int[] rowIndices = { 2 }; T[] nonZeroValues = { (typeof(T) == typeof(float)) ? (T)(object)(0.5F) : (T)(object)(0.5) }; bool sequenceStartFlag = true; bool readOnly = false; var sparseValue = Value.CreateSequence <T>(sampleShape, seqLen, colStarts, rowIndices, nonZeroValues, sequenceStartFlag, device, readOnly); NDArrayView value = null; bool needsGradient = false; var dynamicAxes = new AxisVector(0); bool isSparse = true; var sampleVariable = new Variable(sampleShape, VariableKind.Output, (typeof(T) == typeof(float)) ? DataType.Float : DataType.Double, value, needsGradient, dynamicAxes, isSparse, "sampleVariable", "sampleVariableUid"); int output_seqLen; IList <int> output_colsStarts; IList <int> output_rowIndices; IList <T> output_nonZeroValues; int output_numNonZeroValues; sparseValue.GetSparseData <T>(sampleVariable, out output_seqLen, out output_colsStarts, out output_rowIndices, out output_nonZeroValues, out output_numNonZeroValues); if (output_seqLen != seqLen) { throw new ApplicationException("CopyVariableValueTo returns incorrect sequenceLength"); } if (!output_colsStarts.SequenceEqual(colStarts)) { throw new ApplicationException("CopyVariableValueTo returns incorrect colStarts"); } if (!output_rowIndices.SequenceEqual(rowIndices)) { throw new ApplicationException("CopyVariableValueTo returns incorrect rowIndices"); } if (!output_nonZeroValues.SequenceEqual(nonZeroValues)) { throw new ApplicationException("CopyVariableValueTo returns incorrect nonZeroValues"); } if (output_numNonZeroValues != nonZeroValues.Count()) { throw new ApplicationException("CopyVariableValueTo returns incorrect numNonZeroValues"); } } catch (Exception ex) { Console.WriteLine("Error: {0}\nCallStack: {1}\n Inner Exception: {2}", ex.Message, ex.StackTrace, ex.InnerException != null ? ex.InnerException.Message : "No Inner Exception"); throw ex; } }
void createNDArrayViewObjects() { NDShape shape = new int[] { 4 }; var v1 = Variable.InputVariable(shape, DataType.Float); //create NDArray by setting the values manually var dna1 = new NDArrayView(DataType.Float, shape, device); dna1.SetValue(1); dna1.SetValue(2); dna1.SetValue(3); dna1.SetValue(4); dna1.SetValue(5); //create NDArray var value = new Value(dna1); var dd = value.GetDenseData <float>(v1); Assert.Equal(5f, dd[0][0], 1); Assert.Equal(5f, dd[0][1], 1); Assert.Equal(5f, dd[0][2], 1); Assert.Equal(5f, dd[0][3], 1); Console.WriteLine("NDArrayView SetValue"); print(dd, v1.Shape); //2 var array = new float[] { 10f, 11f, 12f, 13f }; var dna2 = new NDArrayView(shape, array, device); value = new Value(dna2); dd = value.GetDenseData <float>(v1); Assert.Equal(10f, dd[0][0], 1); Assert.Equal(11f, dd[0][1], 1); Assert.Equal(12f, dd[0][2], 1); Assert.Equal(13f, dd[0][3], 1); Console.WriteLine("NDArrayView add array of value"); print(dd, v1.Shape); //3 var dna3 = new NDArrayView(shape, array, 5 /* must be greater or equal then array length*/, device); value = new Value(dna3); dd = value.GetDenseData <float>(v1); Assert.Equal(10f, dd[0][0], 1); Assert.Equal(11f, dd[0][1], 1); Assert.Equal(12f, dd[0][2], 1); Assert.Equal(13f, dd[0][3], 1); Console.WriteLine("NDArrayView add array and number of elements"); print(dd, v1.Shape); //4 var axes = new AxisVector() { { Axis.DefaultBatchAxis() }, { Axis.DefaultDynamicAxis() } }; NDShape shape2 = new int[] { 2, 2 }; var v2 = Variable.InputVariable(shape2, DataType.Float, "", axes); var dna4 = new NDArrayView(shape, array, device); var dna5 = dna4.AsShape(shape2); value = new Value(dna5); Assert.Equal(10f, dd[0][0], 1); Assert.Equal(11f, dd[0][1], 1); Assert.Equal(12f, dd[0][2], 1); Assert.Equal(13f, dd[0][3], 1); dd = value.GetDenseData <float>(v2); print(dd, v2.Shape); }