コード例 #1
0
        static void Main(string[] args)
        {
            FileStream fs = new FileStream("E:\\SVN\\DeepLearning\\MNIST\\train-images.idx3-ubyte", FileMode.Open);

            fs.Position = 16;
            byte[] content = new byte[fs.Length - 16];
            fs.Read(content, 0, content.Length);
            fs.Close();

            var loaded = TFTensor.FromBuffer(new TFShape(60000, 28, 28, 1), content, 0, content.Length);

            using (var session = new TFSession())
            {
                var      graph = session.Graph;
                TFOutput input;
                TFOutput output;
                var(handle, flow) = graph.TensorArrayV3(graph.Const(1), TFDataType.Float, new TFShape(28, 28, 1), true);
                input             = graph.Placeholder(TFDataType.UInt8, new TFShape(60000, 28, 28, 1));
                output            = graph.TensorArraySplitV3(handle, graph.Div(graph.Cast(input, TFDataType.Float), graph.Const(255f)), graph.Const(new long[] { 60000L }), flow);     //output = handle;
                //output = graph.Cast(input, TFDataType.Float);
                var result = session.Run(new TFOutput[] { input }, new TFTensor[] { loaded }, new TFOutput[] { output });

                output = graph.TensorArrayReadV3(handle, graph.Const(0), flow, TFDataType.Float);
                result = session.Run(new TFOutput[] { }, new TFTensor[] { }, new TFOutput[] { output });
            }
        }
コード例 #2
0
        public void Should_ReduceMean(double [,] input, int?axis, object expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    var tinput = graph.Placeholder(TFDataType.Double, new TFShape(2, 2));

                    TFTensor [] result;
                    if (axis != null)
                    {
                        var      taxis = graph.Const(axis.Value);
                        TFOutput y     = graph.ReduceMean(tinput, taxis);
                        result = session.Run(new [] { tinput, taxis }, new TFTensor [] { input, axis }, new [] { y });

                        double [] actual = (double [])result [0].GetValue();
                        TestUtils.MatrixEqual(expected, actual, precision: 8);
                    }
                    else
                    {
                        TFOutput y = graph.ReduceMean(tinput, axis: null);
                        result = session.Run(new [] { tinput }, new TFTensor [] { input }, new [] { y });

                        double actual = (double)result [0].GetValue();
                        TestUtils.MatrixEqual(expected, actual, precision: 8);
                    }
                }
        }
コード例 #3
0
        public void Should_ReduceProd2(int?axis, object expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    double [,] test =
                    {
                        {  2,  5,  7 },
                        { 11, 13, 17 },
                    };

                    var x = graph.Const(test);

                    if (axis == null || axis >= -2 && axis < 2)
                    {
                        TFOutput y = graph.ReduceProd(x, axis: axis == null ? (TFOutput?)null : graph.Const(axis));

                        TFTensor [] result = session.Run(new TFOutput [] { }, new TFTensor [] { }, new [] { y });

                        object actual = result [0].GetValue();
                        TestUtils.MatrixEqual(expected, actual, precision: 8);
                    }
                    else
                    {
                        Assert.Throws <TFException> (() => {
                            TFOutput y = graph.ReduceProd(x, axis: axis == null ? (TFOutput?)null : graph.Const(axis));
                            session.Run(new TFOutput [] { }, new TFTensor [] { }, new [] { y });
                        });
                    }
                }
        }
コード例 #4
0
        public void Should_Range(object start, object limit, object delta, object expected)
        {
            // Examples from https://www.tensorflow.org/api_docs/python/tf/range

            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    TFOutput tstart = graph.Placeholder(start == null ? TFDataType.Int32 : TensorTypeFromType(start.GetType()));
                    TFOutput tlimit = graph.Placeholder(limit == null ? TFDataType.Int32 : TensorTypeFromType(limit.GetType()));
                    TFOutput tdelta = graph.Placeholder(delta == null ? TFDataType.Int32 : TensorTypeFromType(delta.GetType()));

                    TFTensor[] result;
                    if (start == null && delta == null)
                    {
                        TFOutput r = graph.Range(tlimit);
                        result = session.Run(new[] { tlimit }, new TFTensor[] { TensorFromObject(limit) }, new[] { r });
                    }
                    else
                    {
                        TFOutput r = graph.Range(tstart, (Nullable <TFOutput>)tlimit, (Nullable <TFOutput>)tdelta);
                        result = session.Run(new[] { tstart, tlimit, tdelta },
                                             new TFTensor[] { TensorFromObject(start), TensorFromObject(limit), TensorFromObject(delta) },
                                             new[] { r });
                    }

                    Array actual = (Array)result[0].GetValue();
                    TestUtils.MatrixEqual((Array)expected, actual, precision: 10);
                }
        }
コード例 #5
0
        public static TFTensor CreateTensorFromBitmap(Bitmap bitmap, TFDataType destinationDataType = TFDataType.Float)
        {
            ImageConverter converter = new ImageConverter();
            var            contents  = (byte[])converter.ConvertTo(bitmap, typeof(byte[]));

            // DecodeJpeg uses a scalar String-valued tensor as input.
            var tensor = TFTensor.CreateString(contents);

            TFOutput input, output;

            // Construct a graph to normalize the image
            using (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType))
            {
                // Execute that graph to normalize this one image
                using (var session = new TFSession(graph))
                {
                    var normalized = session.Run(
                        inputs: new[] { input },
                        inputValues: new[] { tensor },
                        outputs: new[] { output });

                    return(normalized[0]);
                }
            }
        }
コード例 #6
0
        public void Should_MultidimensionalAndJaggedBeEqual(Array jagged, Array multidimensional, bool expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    var tjagged           = graph.Const(new TFTensor(jagged));
                    var tmultidimensional = graph.Const(new TFTensor(multidimensional));

                    TFOutput y = graph.Equal(tjagged, tmultidimensional);
                    TFOutput r;
                    if (multidimensional.Rank == 2)
                    {
                        r = graph.All(y, graph.Const(new [] { 0, 1 }));
                    }
                    else if (multidimensional.Rank == 3)
                    {
                        r = graph.All(y, graph.Const(new [] { 0, 1, 2 }));
                    }
                    else
                    {
                        throw new System.Exception("If you want to test Ranks > 3 please handle this extra case manually.");
                    }

                    TFTensor [] result = session.Run(new TFOutput [] { }, new TFTensor [] { }, new [] { r });

                    bool actual = (bool)result [0].GetValue();
                    Assert.Equal(expected, actual);
                }
        }
コード例 #7
0
        // Convert the image in filename to a Tensor suitable as input to the Inception model.
        public static TFTensor CreateTensorFromImageFile(string file, TFDataType destinationDataType = TFDataType.Float)
        {
            var contents = File.ReadAllBytes(file);

            // DecodeJpeg uses a scalar String-valued tensor as input.
            var tensor = TFTensor.CreateString(contents);

            TFOutput input;
            TFOutput output;

            // Construct a graph to normalize the image
            using (var graph = ConstructGraphToNormalizeImage(out input, out output, destinationDataType))
            {
                // Execute that graph to normalize this one image
                using (var session = new TFSession(graph))
                {
                    var normalized = session.Run(
                        inputs: new[] { input },
                        inputValues: new[] { tensor },
                        outputs: new[] { output });

                    return(normalized[0]);
                }
            }
        }
コード例 #8
0
 public static void Main(string[] args)
 {
     using (var graph = new TFGraph())
     {
         graph.Import(File.ReadAllBytes("model.pb"));
         using (var session = new TFSession(graph))
         {
             var charset       = Constants.AlphabetLower;
             var opInput       = graph["input"][0];
             var opDenseDecode = graph["dense_decoded"][0];
             var imageTensor   = ImageUtil.CreateTensorFromImageFile("demo.bmp", 3);
             var output        = session.Run(
                 inputs: new TFOutput[] { opInput },
                 inputValues: new TFTensor[] { imageTensor },
                 outputs: new TFOutput[] { opDenseDecode }
                 );
             var result       = output[0];
             var stringBuffer = new StringBuilder();
             foreach (int s in (long[, ])result.GetValue())
             {
                 if (s > charset.Count - 1)
                 {
                     Console.WriteLine("Current character set do not match the model.");
                     break;
                 }
                 stringBuffer.Append(charset[s]);
             }
             Console.WriteLine(stringBuffer.ToString());
         }
     }
     Console.ReadKey();
 }
コード例 #9
0
        public void StringTestWithMultiDimStringTensorAsInputOutput()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var W         = graph.Placeholder(TFDataType.String, new TFShape(-1, 2));
                    var identityW = graph.Identity(W);

                    var dataW = new string[, ] {
                        { "This is fine.", "That's ok." }, { "This is fine.", "That's ok." }
                    };
                    var bytes = new byte[2 * 2][];
                    bytes[0] = Encoding.UTF8.GetBytes(dataW[0, 0]);
                    bytes[1] = Encoding.UTF8.GetBytes(dataW[0, 1]);
                    bytes[2] = Encoding.UTF8.GetBytes(dataW[1, 0]);
                    bytes[3] = Encoding.UTF8.GetBytes(dataW[1, 1]);
                    var tensorW = TFTensor.CreateString(bytes, new TFShape(2, 2));

                    var outputTensor = session.Run(new TFOutput[] { W }, new TFTensor[] { tensorW }, new[] { identityW });

                    var outputW = TFTensor.DecodeMultiDimensionString(outputTensor[0]);
                    Assert.Equal(dataW[0, 0], Encoding.UTF8.GetString(outputW[0]));
                    Assert.Equal(dataW[0, 1], Encoding.UTF8.GetString(outputW[1]));
                    Assert.Equal(dataW[1, 0], Encoding.UTF8.GetString(outputW[2]));
                    Assert.Equal(dataW[1, 1], Encoding.UTF8.GetString(outputW[3]));
                }
        }
コード例 #10
0
        public void DevicePlacementTest()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var X = graph.Placeholder(TFDataType.Float, new TFShape(-1, 784));
                    var Y = graph.Placeholder(TFDataType.Float, new TFShape(-1, 10));

                    int numGPUs  = 4;
                    var Xs       = graph.Split(graph.Const(0), X, numGPUs);
                    var Ys       = graph.Split(graph.Const(0), Y, numGPUs);
                    var products = new TFOutput[numGPUs];
                    for (int i = 0; i < numGPUs; i++)
                    {
                        using (var device = graph.WithDevice("/device:GPU:" + i))
                        {
                            var W = graph.Constant(0.1f, new TFShape(784, 500), TFDataType.Float);
                            var b = graph.Constant(0.1f, new TFShape(500), TFDataType.Float);
                            products[i] = graph.Add(graph.MatMul(Xs[i], W), b);
                        }
                    }
                    var   stacked = graph.Concat(graph.Const(0), products);
                    Mnist mnist   = new Mnist();
                    mnist.ReadDataSets("/tmp");
                    int batchSize = 1000;
                    for (int i = 0; i < 100; i++)
                    {
                        var reader = mnist.GetTrainReader();
                        (var trainX, var trainY) = reader.NextBatch(batchSize);
                        var outputTensors = session.Run(new TFOutput[] { X }, new TFTensor[] { new TFTensor(trainX) }, new TFOutput[] { stacked });
                        Assert.Equal(1000, outputTensors[0].Shape[0]);
                        Assert.Equal(500, outputTensors[0].Shape[1]);
                    }
                }
        }
コード例 #11
0
        static TFTensor Mat2Tensor(Mat mat)
        {
            // 将图片读取为Tensor
            Bitmap bitmap = mat.Bitmap;

            byte[]   contents = Bitmap2Bytes(bitmap);
            TFTensor tensor   = TFTensor.CreateString(contents);

            TFGraph  graph = new TFGraph();
            TFOutput input, output;

            input = graph.Placeholder(TFDataType.String);

            output = graph.DecodeJpeg(input, 3);
            output = graph.Cast(output, TFDataType.UInt8);
            output = graph.ExpandDims(output, graph.Const(0, "make_batch"));

            // 执行图
            using (var session = new TFSession(graph))
            {
                var normalized = session.Run(
                    inputs: new[] { input },
                    inputValues: new[] { tensor },
                    outputs: new[] { output });

                return(normalized[0]);
            }
        }
コード例 #12
0
 public static TFTensor[] Run(this TFGraph graph, TFOutput inputNodes, TFTensor inputValue, TFOutput outputNode)
 {
     using (TFSession sess = new TFSession(graph))
     {
         return(sess.Run(new TFOutput[] { inputNodes }, new TFTensor[] { inputValue }, new TFOutput[] { outputNode }));
     }
 }
コード例 #13
0
 public static TFTensor[] Run(this TFGraph graph, TFOutput[] inputNodes, TFTensor[] inputValue, TFOutput[] outputNode)
 {
     using (TFSession sess = new TFSession(graph))
     {
         return(sess.Run(inputNodes, inputValue, outputNode));
     }
 }
コード例 #14
0
        public void Run()
        {
            List <object> jList = new List <object>();
            List <object> rList = new List <object>();

            using (TFSession session = new TFSession(graf))
            {
                session.Run()
            }
        }
コード例 #15
0
        public void Should_ShapeAutomaticallyConvertToTensor()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    var x = graph.Const(new TFShape(2, 3));

                    TFTensor [] result = session.Run(new TFOutput [] { }, new TFTensor [] { }, new TFOutput [] { x });

                    int[] actual = (int[])result [0].GetValue();
                    Assert.Equal(new [] { 2, 3 }, actual);
                }
        }
コード例 #16
0
        public void TestSession()
        {
            var status = new TFStatus();

            using (var graph = new TFGraph()) {
                var feed = Placeholder(graph, status);
                var two  = ScalarConst(2, graph, status);
                var add  = Add(feed, two, graph, status);
                Assert(status);

                // Create a session for this graph
                using (var session = new TFSession(graph, status)) {
                    Assert(status);

                    // Run the graph
                    var inputs = new TFOutput [] {
                        new TFOutput(feed, 0)
                    };
                    var input_values = new TFTensor [] {
                        3
                    };
                    var add_output = new TFOutput(add, 0);
                    var outputs    = new TFOutput [] {
                        add_output
                    };

                    var results = session.Run(runOptions: null,
                                              inputs: inputs,
                                              inputValues: input_values,
                                              outputs: outputs,
                                              targetOpers: null,
                                              runMetadata: null,
                                              status: status);
                    Assert(status);
                    var res = results [0];
                    Assert(res.TensorType == TFDataType.Int32);
                    Assert(res.NumDims == 0);                      // Scalar
                    Assert(res.TensorByteSize == (UIntPtr)4);
                    Assert(Marshal.ReadInt32(res.Data) == 3 + 2);

                    // Use runner API
                    var runner = session.GetRunner();
                    runner.AddInput(new TFOutput(feed, 0), 3);
                    runner.Fetch(add_output);
                    results = runner.Run(status: status);
                    res     = results [0];
                    Assert(res.TensorType == TFDataType.Int32);
                    Assert(res.NumDims == 0);                      // Scalar
                    Assert(res.TensorByteSize == (UIntPtr)4);
                    Assert(Marshal.ReadInt32(res.Data) == 3 + 2);
                }
            }
        }
コード例 #17
0
        public void Should_SigmoidCrossEntropyWithLogits(double [] labels, double [] logits, double [] expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    var tlabels = graph.Placeholder(TFDataType.Double, new TFShape(2, 2));
                    var tlogits = graph.Placeholder(TFDataType.Double, new TFShape(2, 2));

                    TFOutput y = graph.SigmoidCrossEntropyWithLogits(tlabels, tlogits);

                    if (expected != null)
                    {
                        TFTensor [] result = session.Run(new [] { tlabels, tlogits }, new TFTensor [] { labels, logits }, new [] { y });

                        double [] actual = (double [])result [0].GetValue();
                        TestUtils.MatrixEqual(expected, actual, precision: 8);
                    }
                    else
                    {
                        Assert.Throws <TFException> (() => session.Run(new [] { tlabels, tlogits }, new TFTensor [] { labels, logits }, new [] { y }));
                    }
                }
        }
コード例 #18
0
        public List <DetectionResult> RunDetection(int imgWidth, int imgHeight, byte[] img)
        {
            Debug.Assert(img.Length == imgWidth * imgHeight * 3);

            TFTensor tensor = TFTensor.FromBuffer(new TFShape(1, imgWidth, imgHeight, 3), img, 0, img.Length);

            TFTensor[] output;
            lock (_sessionLocker)
            {
                output = _session.Run(new[] { _graph["image_tensor"][0] }, new[] { tensor }, new[]
                {
                    _graph["detection_boxes"][0],
                    _graph["detection_scores"][0],
                    _graph["detection_classes"][0],
                    _graph["num_detections"][0]
                });
            }

            var boxes   = (float[, , ])output[0].GetValue();
            var scores  = (float[, ])output[1].GetValue();
            var classes = (float[, ])output[2].GetValue();
            var xsize   = boxes.GetLength(0);
            var ysize   = Math.Min(boxes.GetLength(1), 5); //HACK: too many results

            var results = new List <DetectionResult>();

            for (var i = 0; i < xsize; i++)
            {
                for (var j = 0; j < ysize; j++)
                {
                    var   top    = (int)(boxes[i, j, 0] * imgHeight);
                    var   left   = (int)(boxes[i, j, 1] * imgWidth);
                    var   bottom = (int)(boxes[i, j, 2] * imgHeight);
                    var   right  = (int)(boxes[i, j, 3] * imgWidth);
                    float score  = scores[i, j];
                    var   @class = Convert.ToInt32(classes[i, j]);

                    //if (score < 0.03) break; //HACK: too many results

                    results.Add(new DetectionResult
                    {
                        Box   = new BBox(top, left, bottom, right),
                        Score = score,
                        Class = @class
                    });
                }
            }

            return(results);
        }
コード例 #19
0
    public static TFTensor ResizeTensor(TFTensor tensor, int size)
    {
        TFOutput input, output;

        using (var graph = ConstructGraphToNormalizeImage(out input, out output, size))
        {
            using (var session = new TFSession(graph))
            {
                var normalized = session.Run(new[] { input }, new[] { tensor }, new[] { output });

                return(normalized[0]);
            }
        }
    }
コード例 #20
0
        public void Should_ClipByAverageNorm(double[,] m, double norm, int axis, double[,] expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var matrix    = graph.Placeholder(TFDataType.Double);
                    var clip_norm = graph.Placeholder(TFDataType.Double);
                    var a         = graph.Const(new TFTensor(axis));

                    TFOutput y = graph.ClipByNorm(matrix, clip_norm, a);

                    if (expected != null)
                    {
                        TFTensor[] result = session.Run(new[] { matrix, clip_norm }, new TFTensor[] { m, norm }, new[] { y });

                        double[,] actual = (double[, ])result[0].GetValue();
                        TestUtils.MatrixEqual(expected, actual, precision: 10);
                    }
                    else
                    {
                        Assert.Throws <TFException>(() => session.Run(new[] { matrix, clip_norm }, new TFTensor[] { m, norm }, new[] { y }));
                    }
                }
        }
コード例 #21
0
        public void Should_EvaluateBitwiseAnd(int aValue, int bValue, int expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    TFOutput a = graph.Placeholder(TFDataType.Int32);
                    TFOutput b = graph.Placeholder(TFDataType.Int32);

                    TFOutput y = graph.BitwiseAnd(a, b);

                    TFTensor[] result = session.Run(new[] { a, b }, new TFTensor[] { aValue, bValue }, new[] { y });

                    Assert.Equal(expected, (int)result[0].GetValue());
                }
        }
コード例 #22
0
        public static Tensor CreateTensorFromBuffer(byte [] buffer, ImageCodec codec, bool use_resize = false, int width = 100, int height = 100)
        {
            var tensor = TFTensor.CreateString(buffer);

            TFGraph  graph;
            TFOutput input, output;

            ConstructGraphToNormalizeImage(out graph, out input, out output, codec);

            using (var session = new TFSession(graph))
            {
                var normalized = session.Run(new[] { input }, new[] { tensor }, new[] { output });
                return(new Tensor(normalized[0]));
            }
        }
コード例 #23
0
        private TFTensor CreateTensorFromImage(byte[] contents, TFDataType dataType = TFDataType.Float)
        {
            // DecodeJpeg uses a scalar String-valued tensor as input.
            using (var tensor = TFTensor.CreateString(contents))
                using (var graph = ConstructGraphToNormalizeImage(out TFOutput input, out TFOutput output, dataType))
                    using (var session = new TFSession(graph))
                    {
                        var normalized = session.Run(
                            inputs: new[] { input },
                            inputValues: new[] { tensor },
                            outputs: new[] { output });

                        return(normalized[0]);
                    }
        }
コード例 #24
0
        private TFTensor CreateTensor(byte[] imageData, int width, int height)
        {
            var(session, input, output) = CreateRawImageGraph(width, height);

            using (session)
            {
                var value = TFTensor.CreateString(imageData);

                return(session.Run(
                           inputValues: new[] { value },
                           inputs: new[] { input },
                           outputs: new[] { output }
                           )[0]);
            }
        }
コード例 #25
0
        public void Should_CalculateTanhGrad_Correctly()
        {
            using (TFGraph graph = new TFGraph())
                using (TFSession session = new TFSession(graph))
                {
                    TFOutput x    = graph.Const(new TFTensor(0.7));
                    TFOutput y    = graph.Tanh(x);
                    TFOutput dy   = graph.Const(new TFTensor(new [] { 1.0 }));
                    TFOutput grad = graph.TanhGrad(y, dy);

                    TFTensor [] result = session.Run(new TFOutput [] { }, new TFTensor [] { }, new [] { grad });

                    double value = (double)result [0].GetValue();
                    Assert.Equal(0.634739589982459, value, 15);
                }
        }
コード例 #26
0
        public static async Task <(TFTensor, TFOutput)> CreateTensorFromImageFile(string fileName, TFDataType destinationDataType, int size, CancellationToken ct)
        {
            var contents = await File.ReadAllBytesAsync(fileName, ct).ConfigureAwait(false);

            var tensor = TFTensor.CreateString(contents);

            using (var graph = CreateGraphToNormalizeImage(out var input, out var output, destinationDataType, size, 0))
                using (var session = new TFSession(graph))
                {
                    var normalized = session.Run(
                        inputs: new[] { input },
                        inputValues: new[] { tensor },
                        outputs: new[] { output });
                    return(normalized[0], output);
                }
        }
コード例 #27
0
        // Convert the image in filename to a Tensor suitable as input to the Inception model.
        static TFTensor CreateTensorFromImageFile(byte[] contents)
        {
            // DecodeJpeg uses a scalar String-valued tensor as input.
            var      inputTensor = TFTensor.CreateString(contents);
            TFGraph  graph       = new TFGraph();
            TFOutput input       = graph.Placeholder(TFDataType.String);
            TFOutput output      = graph.DecodeJpeg(contents: input, channels: 3);

            using (var session = new TFSession(graph))
            {
                var tensor = session.Run(
                    inputs: new[] { input },
                    inputValues: new[] { inputTensor },
                    outputs: new[] { output });
                return(tensor[0]);
            }
        }
コード例 #28
0
        public void Should_ClipByValue(double[,] m, double min, double max, double[,] expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var matrix   = graph.Placeholder(TFDataType.Double);
                    var clip_min = graph.Placeholder(TFDataType.Double);
                    var clip_max = graph.Const(new TFTensor(max));

                    TFOutput y = graph.ClipByValue(matrix, clip_min, clip_max);

                    TFTensor[] result = session.Run(new[] { matrix, clip_min }, new TFTensor[] { m, min }, new[] { y });

                    double[,] actual = (double[, ])result[0].GetValue();
                    Assert.Equal(expected, actual);
                }
        }
コード例 #29
0
        public void ShouldAddGradients()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph)) {
                    var x = graph.Const(3.0);

                    var y1 = graph.Square(x, "Square1");
                    var y2 = graph.Square(y1, "Square2");

                    var y3 = graph.Square(y2, "Square3");
                    var g  = graph.AddGradients(new TFOutput [] { y1, y3 }, new [] { x });

                    var r  = session.Run(new TFOutput [] { }, new TFTensor [] { }, g);
                    var dy = (double)r [0].GetValue();
                    Assert.Equal(17502.0, dy);
                }
        }
コード例 #30
0
        public void Should_Stack(double[] x, double[] y, double[] z, int?axis, double[,] expected)
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var a = graph.Placeholder(TFDataType.Double, new TFShape(2));
                    var b = graph.Placeholder(TFDataType.Double, new TFShape(2));
                    var c = graph.Placeholder(TFDataType.Double, new TFShape(2));

                    TFOutput r = graph.Stack(new[] { a, b, c }, axis: axis);

                    TFTensor[] result = session.Run(new[] { a, b, c }, new TFTensor[] { x, y, z }, new[] { r });

                    double[,] actual = (double[, ])result[0].GetValue();
                    TestUtils.MatrixEqual(expected, actual, precision: 10);
                }
        }