unsafe void Run(KeyValuePair <Operation, Tensor>[] inputs, TF_Output[] outputs, int[] expected_results) { var csession = new CSession(host_graph_, s_); ASSERT_EQ(TF_OK, s_.Code, s_.Message); csession.SetInputs(inputs); csession.SetOutputs(outputs); csession.Run(s_); ASSERT_EQ(TF_OK, s_.Code, s_.Message); for (int i = 0; i < expected_results.Length; ++i) { var output = csession.output_tensor(i); ASSERT_TRUE(output != IntPtr.Zero); EXPECT_EQ(TF_DataType.TF_INT32, c_api.TF_TensorType(output)); EXPECT_EQ(0, c_api.TF_NumDims(output)); ASSERT_EQ(sizeof(int), (int)c_api.TF_TensorByteSize(output)); var output_contents = c_api.TF_TensorData(output); EXPECT_EQ(expected_results[i], *(int *)output_contents.ToPointer()); } }
private void RunGraphsAndCompareOutputs(TF_Output[] grad_outputs, TF_Output[] expected_grad_outputs) { var csession = new CSession(graph_, s_); var expected_csession = new CSession(expected_graph_, s_); var grad_outputs_vec = grad_outputs; csession.SetOutputs(grad_outputs_vec); csession.Run(s_); ASSERT_EQ(TF_OK, TF_GetCode(s_)); var out0 = csession.output_tensor(0); var out1 = csession.output_tensor(1); var expected_grad_outputs_vec = expected_grad_outputs; expected_csession.SetOutputs(expected_grad_outputs_vec); expected_csession.Run(s_); ASSERT_EQ(TF_OK, TF_GetCode(s_)); var expected_out0 = expected_csession.output_tensor(0); var expected_out1 = expected_csession.output_tensor(1); //CompareTensors(out0, expected_out0); //CompareTensors(out1, expected_out1); }
public void Session() { lock (Locks.ProcessWide) { var s = new Status(); var graph = new Graph().as_default(); // Make a placeholder operation. var feed = c_test_util.Placeholder(graph, s); // Make a constant operation with the scalar "2". var two = c_test_util.ScalarConst(2, graph, s); // Add operation. var add = c_test_util.Add(feed, two, graph, s); var csession = new CSession(graph, s); ASSERT_EQ(TF_Code.TF_OK, s.Code); // Run the graph. var inputs = new Dictionary <Operation, Tensor>(); inputs.Add(feed, new Tensor(3)); csession.SetInputs(inputs); var outputs = new TF_Output[] { new TF_Output(add, 0) }; csession.SetOutputs(outputs); csession.Run(s); Tensor outTensor = csession.output_tensor(0); EXPECT_EQ(TF_DataType.TF_INT32, outTensor.dtype); EXPECT_EQ(0, outTensor.NDims); ASSERT_EQ((ulong)sizeof(uint), outTensor.bytesize); var output_contents = outTensor.ToArray <int>(); EXPECT_EQ(3 + 2, output_contents[0]); // Add another operation to the graph. var neg = c_test_util.Neg(add, graph, s); ASSERT_EQ(TF_Code.TF_OK, s.Code); // Run up to the new operation. inputs = new Dictionary <Operation, Tensor>(); inputs.Add(feed, new Tensor(7)); csession.SetInputs(inputs); outputs = new TF_Output[] { new TF_Output(neg, 0) }; csession.SetOutputs(outputs); csession.Run(s); ASSERT_EQ(TF_Code.TF_OK, s.Code); outTensor = csession.output_tensor(0); ASSERT_TRUE(outTensor != IntPtr.Zero); EXPECT_EQ(TF_DataType.TF_INT32, outTensor.dtype); EXPECT_EQ(0, outTensor.NDims); // scalar ASSERT_EQ((ulong)sizeof(uint), outTensor.bytesize); output_contents = outTensor.ToArray <int>(); EXPECT_EQ(-(7 + 2), output_contents[0]); // Clean up csession.CloseAndDelete(s); ASSERT_EQ(TF_Code.TF_OK, s.Code); } }