public void cntk_equal_test() { using (var K = new CNTKBackend()) { var x = K.variable(array: new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }); var y = K.variable(array: new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }); Assert.AreEqual(new bool[, ] { { true, true, true }, { true, true, true } }, K.equal(x, y).eval <bool>()); x = K.variable(array: new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }); y = K.variable(array: new double[, ] { { 1, 2, 3 }, { 4, 5, 0 } }); Assert.AreEqual(new bool[, ] { { true, true, true }, { true, true, false } }, (bool[, ])K.equal(x, y).eval <bool>()); } }
public void cntk_sum_test() { using (var K = new CNTKBackend()) { var x = K.variable(array: new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }); { double a = (double)K.sum(x, axis: new[] { 0, 1 }).eval(); Assert.AreEqual(21, a); double[] b = (double[])K.sum(x, axis: new[] { 0 }).eval(); Assert.AreEqual(new[] { 5.0, 7.0, 9.0 }, b); double[] c = (double[])K.sum(x, axis: new[] { 1 }).eval(); Assert.AreEqual(new[] { 6.0, 15.0 }, c); double[,] d = (double[, ])K.sum(x, axis: new int[] { }).eval(); Assert.AreEqual(new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }, d); } { double[] a = (double[])K.sum(x, axis: -1).eval(); Assert.AreEqual(new[] { 6.0, 15.0 }, a); double[] b = (double[])K.sum(x, axis: 0).eval(); Assert.AreEqual(new[] { 5.0, 7.0, 9.0 }, b); double[] c = (double[])K.sum(x, axis: 1).eval(); Assert.AreEqual(new[] { 6.0, 15.0 }, c); } } }
public void cntk_mean_test() { using (var K = new CNTKBackend()) { var x = K.variable(array: new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }); { double a = (double)K.mean(x, axis: new[] { 0, 1 }).eval(); Assert.AreEqual(3.5, a); double[] b = (double[])K.mean(x, axis: new[] { 0 }).eval(); Assert.AreEqual(new[] { 2.5, 3.5, 4.5 }, b); double[] c = (double[])K.mean(x, axis: new[] { 1 }).eval(); Assert.AreEqual(new[] { 2.0, 5.0 }, c); double[,] d = (double[, ])K.mean(x, axis: new int[] { }).eval(); Assert.AreEqual(new double[, ] { { 1, 2, 3 }, { 4, 5, 6 } }, d); } { double[] a = (double[])K.mean(x, axis: -1).eval(); Assert.AreEqual(new[] { 2, 5 }, a); double[] b = (double[])K.mean(x, axis: 0).eval(); Assert.AreEqual(new[] { 2.5, 3.5, 4.5 }, b); double[] c = (double[])K.mean(x, axis: 1).eval(); Assert.AreEqual(new[] { 2.0, 5.0 }, c); } } }
public void cntk_partial_shape_test() { // Note: Keras/TensorFlow represent unknown dimensions // as None, whereas TensorFlowSharp represents as -1: /* * import keras * * from keras.models import Sequential * from keras.layers import Dense * from keras import backend as K * import numpy as np * * a = K.placeholder(shape = (None, 2)) * b = K.variable(np.matrix([[1, 2, 3], [4, 5, 6]])) * ab = K.dot(a, b) * * shape_a = K.int_shape(a) * shape_b = K.int_shape(b) * shape_ab = K.int_shape(ab) * * print(shape_a) * print(shape_b) * print(shape_ab) * * >>> Using TensorFlow backend. * (None, 2) * (2, 3) * (None, 3) */ using (var K = new CNTKBackend()) { Tensor a = K.placeholder(shape: new int?[] { null, 2 }); Tensor b = K.variable(array: new float[, ] { { 1, 2, 3 }, { 4, 5, 6 } }); var ab = K.dot(a, b); int?[] shape_a = K.int_shape(a); int?[] shape_b = K.int_shape(b); int?[] shape_ab = K.int_shape(ab); NDShape tf_shape_a = K.In(a).CNTK_Shape; NDShape tf_shape_b = K.In(b).CNTK_Shape; NDShape tf_shape_ab = K.In(ab).CNTK_Shape; AssertEx.AreEqual(new int?[] { null, 2 }, shape_a); AssertEx.AreEqual(new int?[] { 2, 3 }, shape_b); AssertEx.AreEqual(new int?[] { null, 3 }, shape_ab); Assert.AreEqual(NDShape.CreateNDShape(new[] { NDShape.FreeDimension, 2 }), tf_shape_a); Assert.AreEqual(NDShape.CreateNDShape(new[] { 2, 3 }), tf_shape_b); Assert.AreEqual(NDShape.CreateNDShape(new[] { NDShape.FreeDimension, 3 }), tf_shape_ab); } }
public void cntk_argmax_test() { using (var K = new CNTKBackend()) { var x = K.variable(array: new double[, ] { { 1, 9, 3 }, { 9, 5, 6 } }); Assert.AreEqual(new double[] { 1, 0, 1 }, K.argmax(x, axis: 0).eval()); Assert.AreEqual(new double[] { 1, 0 }, K.argmax(x, axis: 1).eval()); Assert.AreEqual(new double[] { 1, 0 }, K.argmax(x).eval()); } }
public void cntk_reshape_test() { using (var K = new CNTKBackend()) { double[,] input_array = new double[, ] { { 1, 2 }, { 3, 4 } }; Tensor variable = K.variable(array: input_array); Tensor variable_new_shape = K.reshape(variable, new int[] { 1, 4 }); double[,] output = (double[, ])variable_new_shape.eval(); Assert.AreEqual(new double[, ] { { 1, 2, 3, 4 } }, output); } }
public void cntk_toString() { using (var K = new CNTKBackend()) { var input = K.placeholder(shape: new int?[] { 2, 4, 5 }); double[,] val = new double[, ] { { 1, 2 }, { 3, 4 } }; var kvar = K.variable(array: (Array)val); string a = input.ToString(); string b = kvar.ToString(); Assert.IsTrue(Regex.Match(a, "KerasSharp\\.Engine.Topology\\.Tensor 'CompositeFunction\\d+' shape=\\[2, 4, 5\\] dtype=Float").Success); Assert.IsTrue(Regex.Match(b, "KerasSharp\\.Engine.Topology\\.Tensor 'CompositeFunction\\d+' shape=\\[2, 2\\] dtype=Double").Success); } }
public void cntk_int_shape() { using (var K = new CNTKBackend()) { #region doc_int_shape var input = K.placeholder(shape: new int?[] { 2, 4, 5 }); var a = K.int_shape(input); // (2, 4, 5) var val = new[, ] { { 1, 2 }, { 3, 4 } }; var kvar = K.variable(array: val); var b = K.int_shape(kvar); //(2, 2) #endregion Assert.AreEqual(new[] { 2, 4, 5 }, a); Assert.AreEqual(new[] { 2, 2 }, b); } }
public void cntk_ndim_test() { // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L508 using (var K = new CNTKBackend()) { #region doc_ndim var input = K.placeholder(shape: new int?[] { 2, 4, 5 }); double[,] val = new double[, ] { { 1, 2 }, { 3, 4 } }; var kvar = K.variable(array: (Array)val); int?a = K.ndim(input); // 3 int?b = K.ndim(kvar); // 2 #endregion Assert.AreEqual(3, a); Assert.AreEqual(2, b); } }
public void cntk_variable_test() { using (var K = new CNTKBackend()) { #region doc_variable double[,] val = new double[, ] { { 1, 2 }, { 3, 4 } }; var kvar = K.variable(array: (Array)val, name: "example_var"); var a = K.dtype(kvar); // 'float64' var b = kvar.eval(); // { { 1, 2 }, { 3, 4 } } #endregion Assert.AreEqual(DataType.Double, a); Assert.AreEqual(new double[, ] { { 1, 2 }, { 3, 4 } }, b); } }
public void cntk_softmax_test() { using (var K = new CNTKBackend()) { double[,] a = new double[, ] { { -4, 2 }, { 0.02, 0.3 } }; var ta = K.variable(array: (Array)a, name: "example_var"); var tr = K.softmax(ta); double[,] r = (double[, ])tr.eval(); AssertEx.AreEqual(new double[, ] { { 0.0024726231566347748, 0.99752737684336534 }, { 0.430453776060771, 0.56954622393922893 } }, r, 1e-8); AssertEx.AreEqual(r.GetRow(0), Accord.Math.Special.Softmax(a.GetRow(0)), 1e-8); AssertEx.AreEqual(r.GetRow(1), Accord.Math.Special.Softmax(a.GetRow(1)), 1e-8); } }