public void tf_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 TensorFlowBackend())
            {
                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);

                long[] tf_shape_a  = K.In(a).TF_Shape;
                long[] tf_shape_b  = K.In(b).TF_Shape;
                long[] tf_shape_ab = K.In(ab).TF_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);

                AssertEx.AreEqual(new long[] { -1, 2 }, tf_shape_a);
                AssertEx.AreEqual(new long[] { 2, 3 }, tf_shape_b);
                AssertEx.AreEqual(new long[] { -1, 3 }, tf_shape_ab);
            }
        }
        public void tf_toString_with_names()
        {
            using (var K = new TensorFlowBackend())
            {
                var input = K.placeholder(shape: new int?[] { 2, 4, 5 }, name: "a");
                double[,] val = new double[, ] {
                    { 1, 2 }, { 3, 4 }
                };
                var kvar = K.variable(array: (Array)val, dtype: DataType.Double, name: "b");

                string a = input.ToString();
                string b = kvar.ToString();

                Assert.AreEqual("KerasSharp.Engine.Topology.Tensor 'a_0' shape=[2, 4, 5] dtype=Float", a);
                Assert.AreEqual("KerasSharp.Engine.Topology.Tensor 'b_0' shape=[2, 2] dtype=Double", b);
            }
        }
Пример #3
0
        public void toString()
        {
            using (var K = new TensorFlowBackend())
            {
                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.AreEqual("KerasSharp.Engine.Topology.Tensor 'Placeholder0_0' shape=[2, 4, 5] dtype=Float", a);
                Assert.AreEqual("KerasSharp.Engine.Topology.Tensor 'Const0_0' shape=[2, 2] dtype=Double", b);
            }
        }
        public void tf_int_shape()
        {
            using (var K = new TensorFlowBackend())
            {
                #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 tf_ndim_test()
        {
            // https://github.com/fchollet/keras/blob/f65a56fb65062c8d14d215c9f4b1015b97cc5bf3/keras/backend/tensorflow_backend.py#L508
            using (var K = new TensorFlowBackend())
            {
                #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);
            }
        }