Exemplo 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);
            }
        }