public void Should_RunPartialRun() { using (var graph = new TFGraph()) using (var session = new TFSession(graph)) { float aValue = 1; float bValue = 2; var a = graph.Placeholder(TFDataType.Float); var b = graph.Placeholder(TFDataType.Float); var c = graph.Placeholder(TFDataType.Float); var r1 = graph.Add(a, b); var r2 = graph.Mul(r1, c); var h = session.PartialRunSetup(new[] { a, b, c }, new[] { r1, r2 }, new[] { r1.Operation, r2.Operation }); var res = session.PartialRun(h, new[] { a, b }, new TFTensor[] { aValue, bValue }, new TFOutput[] { r1 }, new[] { r1.Operation }); // 1+2=3 var calculated = (float)res[0].GetValue(); Assert.Equal(3, calculated); float temp = calculated * 17; // 3*17=51 res = session.PartialRun(h, new[] { c }, new TFTensor[] { temp }, new[] { r2 }, new[] { r2.Operation }); // 51*3=153 calculated = (float)res[0].GetValue(); Assert.Equal(153, calculated); } }
static void Main(string[] args) { // 创建图 var g = new TFGraph(); var xValue = 1.0f; var yValue = 2.0f; // 创建占位符 var x = g.Placeholder(TFDataType.Float); var y = g.Placeholder(TFDataType.Float); var z = g.Placeholder(TFDataType.Float); // 创建运算 var a = g.Add(x, y); // x+y var b = g.Mul(a, z); // a*z // 创建会话 var sess = new TFSession(g); // 运行设置:a = x + y, b = (x + y) * z var setup = sess.PartialRunSetup( new[] { x, y, z }, new[] { a, b }, new[] { a.Operation, b.Operation }); // 部分运行 var result1 = sess.PartialRun(setup, new[] { x, y }, new TFTensor[] { xValue, yValue }, new TFOutput[] { a }, new[] { a.Operation }); // a = x + y = 1 + 2 = 3 // 计算结果 var aValue = (float)result1[0].GetValue(); // 3 Console.WriteLine("a = {0}", aValue); // 部分运行 var result2 = sess.PartialRun(setup, new[] { z }, new TFTensor[] { aValue * 17 }, // 3 * 17 = 51 new[] { b }, new[] { b.Operation }); // 51 * 3=153 // 计算结果 var bValue = (float)result2[0].GetValue(); Console.WriteLine("b = {0}", bValue); }