Browse Source

Add tf.print().

tags/v0.40-tf2.4-tstring
Oceania2018 4 years ago
parent
commit
827d8f211e
7 changed files with 99 additions and 3 deletions
  1. +4
    -0
      src/TensorFlowNET.Core/APIs/tf.debugging.cs
  2. +23
    -0
      src/TensorFlowNET.Core/APIs/tf.logging.cs
  3. +4
    -1
      src/TensorFlowNET.Core/APIs/tf.strings.cs
  4. +3
    -2
      src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs
  5. +36
    -0
      src/TensorFlowNET.Core/Operations/logging_ops.cs
  6. +13
    -0
      src/TensorFlowNET.Core/Operations/string_ops.cs
  7. +16
    -0
      test/TensorFlowNET.UnitTest/ManagedAPI/LoggingTest.cs

+ 4
- 0
src/TensorFlowNET.Core/APIs/tf.debugging.cs View File

@@ -15,6 +15,7 @@
******************************************************************************/

using Tensorflow.Debugging;
using static Tensorflow.Binding;

namespace Tensorflow
{
@@ -27,5 +28,8 @@ namespace Tensorflow
/// https://developer.ibm.com/technologies/artificial-intelligence/tutorials/debug-tensorflow/
/// </summary>
public DebugImpl debugging => new DebugImpl();

public void print(Tensor input)
=> tf.logging.print_v2(input);
}
}

+ 23
- 0
src/TensorFlowNET.Core/APIs/tf.logging.cs View File

@@ -0,0 +1,23 @@
/*****************************************************************************
Copyright 2021 Haiping Chen. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

namespace Tensorflow
{
public partial class tensorflow
{
public logging_ops logging => new logging_ops();
}
}

+ 4
- 1
src/TensorFlowNET.Core/APIs/tf.strings.cs View File

@@ -14,7 +14,7 @@
limitations under the License.
******************************************************************************/

using Tensorflow.Framework;
using static Tensorflow.Binding;

namespace Tensorflow
{
@@ -77,6 +77,9 @@ namespace Tensorflow
public Tensor string_length(Tensor input, string name = null, string unit = "BYTE")
=> ops.string_length(input, name: name, unit: unit);

public Tensor format(string template, Tensor[] inputs, string placeholder = "{}", int summarize = 3, string name = null)
=> ops.string_format(inputs, template: template, placeholder: placeholder, summarize: summarize, name: name);

public RaggedTensor split(Tensor input, string sep = "", int maxsplit = -1, string name = null)
=> ops.string_split_v2(input, sep: sep, maxsplit : maxsplit, name : name);



+ 3
- 2
src/TensorFlowNET.Core/Contexts/Context.ExecuteOp.cs View File

@@ -52,10 +52,11 @@ namespace Tensorflow.Contexts

Func<Tensors> eagerAction = () =>
{
return tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(OpType, Name, args.OpInputArgs)
var opExecInfo = new FastPathOpExecInfo(OpType, Name, args.OpInputArgs)
{
attrs = args.OpAttrs
});
};
return tf.Runner.TFE_FastPathExecute(opExecInfo);
};

if (tf.Context.has_graph_arg(args.OpInputArgs))


+ 36
- 0
src/TensorFlowNET.Core/Operations/logging_ops.cs View File

@@ -0,0 +1,36 @@
/*****************************************************************************
Copyright 2021 Haiping Chen. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

using Tensorflow.Contexts;
using static Tensorflow.Binding;

namespace Tensorflow
{
public class logging_ops
{
public Tensor print_v2(Tensor input, string output_stream = "stderr", string end = "\n", string name = null)
{
var formatted_string = tf.strings.format("{}",
new[] { input },
placeholder: "{}",
summarize: 3,
name: name);

return tf.Context.ExecuteOp("PrintV2", name, new ExecuteOpArgs(formatted_string)
.SetAttributes(new { output_stream, end }));
}
}
}

+ 13
- 0
src/TensorFlowNET.Core/Operations/string_ops.cs View File

@@ -60,6 +60,19 @@ namespace Tensorflow
}
}.SetAttributes(new { unit }));

public Tensor string_format(Tensor[] inputs, string template = "%s", string placeholder = "%s", int summarize = 3, string name = null)
=> tf.Context.ExecuteOp("StringFormat", name, new ExecuteOpArgs()
{
OpInputArgs = new object[] { inputs },
GetGradientAttrs = op => new
{
T = op.get_attr<TF_DataType>("T"),
template = op.get_attr<string>("template"),
placeholder = op.get_attr<string>("placeholder"),
summarize = op.get_attr<int>("summarize")
}
}.SetAttributes(new { template, placeholder, summarize }));

public RaggedTensor string_split_v2(Tensor input, string sep = "", int maxsplit = -1, string name = null)
{
return tf_with(ops.name_scope(name, "StringSplit"), scope =>


+ 16
- 0
test/TensorFlowNET.UnitTest/ManagedAPI/LoggingTest.cs View File

@@ -0,0 +1,16 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
using static Tensorflow.Binding;

namespace TensorFlowNET.UnitTest.ManagedAPI
{
[TestClass]
public class LoggingTest
{
[TestMethod]
public void PrintTest()
{
var tensor = tf.range(10);
tf.print(tensor);
}
}
}

Loading…
Cancel
Save