コード例 #1
0
        public void StringTestWithMultiDimStringTensorAsInputAndScalarStringAsOutput()
        {
            using (var graph = new TFGraph())
                using (var session = new TFSession(graph))
                {
                    var X         = graph.Placeholder(TFDataType.String, new TFShape(-1));
                    var delimiter = graph.Const(TFTensor.CreateString(Encoding.UTF8.GetBytes("/")));
                    var indices   = graph.Const(0);
                    var Y         = graph.ReduceJoin(graph.StringSplit(X, delimiter).values, indices, separator: " ");

                    var dataX = new string[] { "Thank/you/very/much!.", "I/am/grateful/to/you.", "So/nice/of/you." };
                    var bytes = new byte[dataX.Length][];
                    bytes[0] = Encoding.UTF8.GetBytes(dataX[0]);
                    bytes[1] = Encoding.UTF8.GetBytes(dataX[1]);
                    bytes[2] = Encoding.UTF8.GetBytes(dataX[2]);
                    var tensorX = TFTensor.CreateString(bytes, new TFShape(3));

                    var outputTensors = session.Run(new TFOutput[] { X }, new TFTensor[] { tensorX }, new[] { Y });

                    var outputY = Encoding.UTF8.GetString(TFTensor.DecodeString(outputTensors[0]));
                    Assert.Equal(string.Join(" ", dataX).Replace("/", " "), outputY);
                }
        }