/// <summary /// >Creates a NdArray with the specified diagonal along the given axes. /// </summary> /// <param name="axis1">The first dimension of the diagonal.</param> /// <param name="axis2">The seconds dimension of the diagonal.</param> /// <param name="source">The values for the diagonal.</param> /// <returns>A NdArray having the values <paramref name="a"/> on the diagonal specified by the axes public static NdArray <T> DiagMatAxis(int axis1, int axis2, NdArray <T> source) { if (axis1 == axis2) { var errorMessage = string.Format("axes [axis1={0}, axis2={1}] to use for diagonal must be different", axis1, axis2); throw new ArgumentException(errorMessage, "axis2"); } var ax1 = axis1 < axis2 ? axis1 : axis2; var ax2 = axis1 < axis2 ? axis2 : axis1; NdArray <T> .CheckAxis(ax1, source); if (!(ax2 >= 0 && ax2 <= source.NumDimensions)) { var errorMessage = string.Format("Cannot insert axis at position {0} into array of shape {1}.", ax2, ErrorMessage.ShapeToString(source.Shape)); throw new ArgumentException(errorMessage, "axis2"); } var shape = List.Insert(ax2, source.Shape[ax1], source.Shape); var result = NdArray <T> .Zeros(source.Storage.Device, shape); var diag = DiagAxis(ax1, ax2, result); FillFrom(diag, source); return(result); }
public override NdArray create_state(int index, NdArray weight) { if (Math.Abs(this._momentum) < float.Epsilon) { return(null); } return(NdArray.Zeros(weight.GetShape(), weight.GetContext(), weight.GetDtype())); }
public void Type() { void core <T>(NdArray <T> array, string expSubstr) => Assert.Contains(expSubstr, array.ToString("T")); core(NdArray.Zeros <int>(new[] { 4 }), "Type=Int32,"); core(NdArray.Zeros <float>(new[] { 8, 8 }), "Type=Single,"); core(NdArray.Zeros <double>(new[] { 2, 3, 4, 5 }), "Type=Double,"); }
public static IEnumerable <object[]> TestShapeArgs() { yield return(new object[] { NdArray.Zeros <int>(new[] { 4 }), "Shape=(4,)," }); yield return(new object[] { NdArray.Zeros <int>(new[] { 8, 8 }), "Shape=(8, 8,)," }); yield return(new object[] { NdArray.Zeros <int>(new[] { 2, 3, 4, 5 }), "Shape=(2, 3, 4, 5,)," }); }
public void Concat_AxisOutOfrangeCase1_ThrowException() { // arrange const int ConcatAxis = 3; var inputs = new NdArray <int>[] { NdArray <int> .Zeros(HostDevice.Instance, new[] { 1, 1 }) }; // action NdArrayOperator <int> .Concat(ConcatAxis, inputs); }
/// <summary> /// Creates a new identity matrix. /// </summary> /// <param name="device">The device to create the NdArray on.</param> /// <param name="size">The size of the square identity matrix.</param> /// <returns>The new NdArray.</returns> public static NdArray <T> Identity(IDevice device, int size) { var newArray = NdArray <T> .Zeros(device, new[] { size, size }); var diagView = NdArrayOperator <T> .Diag(newArray); diagView.FillConst(Primitives.One <T>()); return(newArray); }
public void BroadCastTo() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 1, 7, 1 }); // action var output = ShapeFunction <int> .BroadCastTo(new[] { 2, 7, 3 }, input); // assert CollectionAssert.AreEqual(new[] { 2, 7, 3 }, output.Shape); }
public void BroadCastDim() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 3, 1, 5 }); // action var output = ShapeFunction <int> .BroadCastDim(1, 9, input); // assert CollectionAssert.AreEqual(new[] { 3, 9, 5 }, output.Shape); }
public void Negate() { // arrange var input = NdArray <bool> .Zeros(HostDevice.Instance, new int[] { 10 }); // action var output = LogicalFunction <bool> .Negate(input); // assert Assert.IsTrue(NdArray <bool> .All(output)); }
public void BroadcastToSameInDimsMany_BigDim_ThrowException() { // arrange var dims = new int[] { 2, 8, 1 }; var array = NdArray <int> .Zeros(HostDevice.Instance, new[] { 1, 2, 3 }); var layouts = new Layout[] { array.Layout, array.Layout, array.Layout }; // action var _ = Layout.BroadcastToSameInDimsMany(dims, layouts); }
public void IsBroadcasted_WithoutBroadCastedNdArray_ReturnFalse() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4 }); // action var output = ShapeFunction <int> .IsBroadcasted(input); // assert Assert.IsFalse(output); }
public void SwapDim() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4 }); // action var output = ShapeFunction <int> .SwapDim(0, 2, input); // assert CollectionAssert.AreEqual(new[] { 4, 3, 2 }, output.Shape); }
public void CutLeft() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 1, 2, 3 }); // action var output = ShapeFunction <int> .CutLeft(input); // assert CollectionAssert.AreEqual(new[] { 2, 3 }, output.Shape); }
public void InsertAxis() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4 }); // action var output = ShapeFunction <int> .InsertAxis(1, input); // assert CollectionAssert.AreEqual(new[] { 2, 1, 3, 4 }, output.Shape); }
public void PadRight() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4 }); // action var output = ShapeFunction <int> .PadRight(input); // assert CollectionAssert.AreEqual(new[] { 2, 3, 4, 1 }, output.Shape); }
public void PermuteAxes() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4, 5 }); // action var output = ShapeFunction <int> .PermuteAxes(new[] { 1, 0, 3, 2 }, input); // assert CollectionAssert.AreEqual(new[] { 3, 2, 5, 4 }, output.Shape); }
public void IsFinite() { // arrange var device = HostDevice.Instance; var source = NdArray <int> .Zeros(device, new[] { 2, 3, 4 }); // action var result = ComparisonFunction <int> .IsFinite(source); // assert Assert.IsTrue(NdArray <int> .All(result)); }
public void ApplyLayoutFn3Dim_InvalidFuncReturn_ThrowException() { // arrange var dummy = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4 }); Layout[] invalidFunc(int[] dim, Layout[] _) => new Layout[] { }; // action var output = ShapeFunction <int> .ApplyLayoutFn(invalidFunc, new int[] { }, dummy, dummy, dummy); }
public void DiagAxis() { // arrange var device = HostDevice.Instance; var input = NdArray <int> .Zeros(device, new[] { 4, 3, 3, 5 }); // action var diag = NdArrayOperator <int> .DiagAxis(1, 2, input); // assert CollectionAssert.AreEqual(new[] { 4, 3, 5 }, diag.Shape); }
public void AtLeastNd_GreaterDimThanInput_ReturnNewNdArray() { // arrange const int MinNumDim = 3; var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 2 }); // action var output = ShapeFunction <int> .AtLeastNd(MinNumDim, input); // assert CollectionAssert.AreEqual(new[] { 1, 2, 2 }, output.Shape); }
public void Concat_DifferntShapes_ThrowException() { // arrange const int ConcatAxis = 1; var inputs = new NdArray <int>[] { NdArray <int> .Zeros(HostDevice.Instance, new[] { 1, 1 }), NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 1 }) }; // action NdArrayOperator <int> .Concat(ConcatAxis, inputs); }
public void IsBroadcasted_WithBroadCastedNdArray_ReturnTrue() { // arrange var input = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 1, 4 }); var broadCasted = NdArray <int> .BroadCastDim(1, 2, input); // action var output = ShapeFunction <int> .IsBroadcasted(broadCasted); // assert Assert.IsTrue(output); }
public void BroadcastToSameInDimsMany_UnableToBroadCast_ThrowException() { // arrange var dims = new int[] { 0 }; var array1 = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 3, 4 }); var array2 = NdArray <int> .Zeros(HostDevice.Instance, new[] { 3, 3, 4 }); var layouts = new Layout[] { array2.Layout, array1.Layout, array2.Layout }; // action var _ = Layout.BroadcastToSameInDimsMany(dims, layouts); }
public void Any() { // arrange var input = NdArray <bool> .Zeros(HostDevice.Instance, new int[] { 2, 4 }); input[new[] { 0, 1 }] = true; // action var output = LogicalFunction <bool> .Any(input); // assert Assert.AreEqual(true, output); }
public void AlmostEqual_DifferentIntVectors_ReturnFalse() { // arrange var device = HostDevice.Instance; var sourceA = NdArray <int> .Zeros(device, new[] { 2, 3, 4 }); var sourceB = NdArray <int> .Zeros(device, new[] { 2, 3, 4 }) + 1; // action var almostEqual = ComparisonFunction <int> .AlmostEqual(sourceA, sourceB); // assert Assert.IsFalse(almostEqual); }
public void PadToSame_Two() { // arrange var input1 = NdArray <int> .Zeros(HostDevice.Instance, new[] { 4, 5 }); var input2 = NdArray <int> .Zeros(HostDevice.Instance, new[] { 3, 4, 5 }); // action var(output1, output2) = ShapeFunction <int> .PadToSame(input1, input2); // assert CollectionAssert.AreEqual(new[] { 1, 4, 5 }, output1.Shape); CollectionAssert.AreEqual(new[] { 3, 4, 5 }, output2.Shape); }
public void BroadCastToSameInDims_Two() { // arrange var input1 = NdArray <int> .Zeros(HostDevice.Instance, new[] { 1, 7, 1 }); var input2 = NdArray <int> .Zeros(HostDevice.Instance, new[] { 3, 4, 5 }); // action var(output1, output2) = ShapeFunction <int> .BroadCastToSameInDims(new[] { 0, 2 }, input1, input2); // assert CollectionAssert.AreEqual(new[] { 3, 7, 5 }, output1.Shape); CollectionAssert.AreEqual(new[] { 3, 4, 5 }, output2.Shape); }
public void Minimum() { // arrange var srcArray1 = NdArray <double> .Zeros(HostDevice.Instance, new[] { 3 }); var srcArray2 = NdArray <double> .Ones(HostDevice.Instance, new[] { 3 }); // action var newArray = ElementWiseMathFunction <double> .Minimum(srcArray1, srcArray2); // assert Assert.AreEqual(0.0, newArray[0].Value); Assert.AreEqual(0.0, newArray[1].Value); Assert.AreEqual(0.0, newArray[2].Value); }
public void AllIndex() { // arrange var source = NdArray <int> .Zeros(HostDevice.Instance, new[] { 2, 1, 3 }); // action var output = IndexFunction <int> .AllIndex(source); // assert CollectionAssert.AreEqual(new[] { 0, 0, 0 }, output[0]); CollectionAssert.AreEqual(new[] { 0, 0, 1 }, output[1]); CollectionAssert.AreEqual(new[] { 0, 0, 2 }, output[2]); CollectionAssert.AreEqual(new[] { 1, 0, 0 }, output[3]); CollectionAssert.AreEqual(new[] { 1, 0, 1 }, output[4]); CollectionAssert.AreEqual(new[] { 1, 0, 2 }, output[5]); }
public void FillNotEqual() { // arrange var device = HostDevice.Instance; var sourceA = NdArray <int> .Arange(device, 0, 10, 1); var sourceB = NdArray <int> .Arange(device, 0, 10, 1); var result = NdArray <bool> .Zeros(device, new[] { 10 }); // action ComparisonFunction <bool> .FillNotEqual(result, sourceA, sourceB); // assert CollectionAssert.AreEqual(new[] { 10 }, result.Shape); }