Ejemplo n.º 1
0
        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);
            }
        }
Ejemplo n.º 2
0
        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);
            }
        }
Ejemplo n.º 3
0
        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);
            }
        }
Ejemplo n.º 4
0
        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);
            }
        }