Skip to content

Commit 827d8f2

Browse files
committed
Add tf.print().
1 parent f874d3a commit 827d8f2

File tree

7 files changed

+99
-3
lines changed

7 files changed

+99
-3
lines changed

src/TensorFlowNET.Core/APIs/tf.debugging.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using Tensorflow.Debugging;
18+
using static Tensorflow.Binding;
1819

1920
namespace Tensorflow
2021
{
@@ -27,5 +28,8 @@ public partial class tensorflow
2728
/// https://developer.ibm.com/technologies/artificial-intelligence/tutorials/debug-tensorflow/
2829
/// </summary>
2930
public DebugImpl debugging => new DebugImpl();
31+
32+
public void print(Tensor input)
33+
=> tf.logging.print_v2(input);
3034
}
3135
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*****************************************************************************
2+
Copyright 2021 Haiping Chen. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
namespace Tensorflow
18+
{
19+
public partial class tensorflow
20+
{
21+
public logging_ops logging => new logging_ops();
22+
}
23+
}

src/TensorFlowNET.Core/APIs/tf.strings.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17-
using Tensorflow.Framework;
17+
using static Tensorflow.Binding;
1818

1919
namespace Tensorflow
2020
{
@@ -77,6 +77,9 @@ public Tensor substr(string input, int pos, int len,
7777
public Tensor string_length(Tensor input, string name = null, string unit = "BYTE")
7878
=> ops.string_length(input, name: name, unit: unit);
7979

80+
public Tensor format(string template, Tensor[] inputs, string placeholder = "{}", int summarize = 3, string name = null)
81+
=> ops.string_format(inputs, template: template, placeholder: placeholder, summarize: summarize, name: name);
82+
8083
public RaggedTensor split(Tensor input, string sep = "", int maxsplit = -1, string name = null)
8184
=> ops.string_split_v2(input, sep: sep, maxsplit : maxsplit, name : name);
8285

src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ public Tensors ExecuteOp(string OpType, string Name, ExecuteOpArgs args)
5252

5353
Func<Tensors> eagerAction = () =>
5454
{
55-
return tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(OpType, Name, args.OpInputArgs)
55+
var opExecInfo = new FastPathOpExecInfo(OpType, Name, args.OpInputArgs)
5656
{
5757
attrs = args.OpAttrs
58-
});
58+
};
59+
return tf.Runner.TFE_FastPathExecute(opExecInfo);
5960
};
6061

6162
if (tf.Context.has_graph_arg(args.OpInputArgs))
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*****************************************************************************
2+
Copyright 2021 Haiping Chen. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************/
16+
17+
using Tensorflow.Contexts;
18+
using static Tensorflow.Binding;
19+
20+
namespace Tensorflow
21+
{
22+
public class logging_ops
23+
{
24+
public Tensor print_v2(Tensor input, string output_stream = "stderr", string end = "\n", string name = null)
25+
{
26+
var formatted_string = tf.strings.format("{}",
27+
new[] { input },
28+
placeholder: "{}",
29+
summarize: 3,
30+
name: name);
31+
32+
return tf.Context.ExecuteOp("PrintV2", name, new ExecuteOpArgs(formatted_string)
33+
.SetAttributes(new { output_stream, end }));
34+
}
35+
}
36+
}

src/TensorFlowNET.Core/Operations/string_ops.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ public Tensor string_length(Tensor input, string name = null, string unit = "BYT
6060
}
6161
}.SetAttributes(new { unit }));
6262

63+
public Tensor string_format(Tensor[] inputs, string template = "%s", string placeholder = "%s", int summarize = 3, string name = null)
64+
=> tf.Context.ExecuteOp("StringFormat", name, new ExecuteOpArgs()
65+
{
66+
OpInputArgs = new object[] { inputs },
67+
GetGradientAttrs = op => new
68+
{
69+
T = op.get_attr<TF_DataType>("T"),
70+
template = op.get_attr<string>("template"),
71+
placeholder = op.get_attr<string>("placeholder"),
72+
summarize = op.get_attr<int>("summarize")
73+
}
74+
}.SetAttributes(new { template, placeholder, summarize }));
75+
6376
public RaggedTensor string_split_v2(Tensor input, string sep = "", int maxsplit = -1, string name = null)
6477
{
6578
return tf_with(ops.name_scope(name, "StringSplit"), scope =>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
2+
using static Tensorflow.Binding;
3+
4+
namespace TensorFlowNET.UnitTest.ManagedAPI
5+
{
6+
[TestClass]
7+
public class LoggingTest
8+
{
9+
[TestMethod]
10+
public void PrintTest()
11+
{
12+
var tensor = tf.range(10);
13+
tf.print(tensor);
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)