Skip to content

Commit db94e67

Browse files
committed
Add console logger.
1 parent b58c241 commit db94e67

File tree

8 files changed

+47
-3
lines changed

8 files changed

+47
-3
lines changed

src/TensorFlowNET.Core/Eager/EagerRunner.RecordGradient.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Linq;
3+
using Microsoft.Extensions.Logging;
34
using Tensorflow.Gradients;
45
using static Tensorflow.Binding;
56
using static Tensorflow.tensorflow;
@@ -38,7 +39,7 @@ public bool RecordGradient(string op_name,
3839
}*/
3940
}
4041

41-
// Console.WriteLine($"RecordGradient: should_record={should_record}, op_name={op_name}");
42+
tf.Logger.LogDebug($"RecordGradient: should_record={should_record}, op_name={op_name}");
4243
if (!should_record) return should_record;
4344

4445
Tensor[] op_outputs;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using Tensorflow.Gradients;
3+
using static Tensorflow.Binding;
4+
using static Tensorflow.tensorflow;
5+
6+
namespace Tensorflow.Eager
7+
{
8+
public partial class EagerRunner
9+
{
10+
public int TapeSetPossibleGradientTypes(params Tensor[] args)
11+
{
12+
return 1;
13+
}
14+
}
15+
}

src/TensorFlowNET.Core/Eager/IEagerRunner.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,7 @@ bool RecordGradient(string op_name,
4040
Tensor[] results);
4141

4242
bool MustRecordGradient();
43+
44+
int TapeSetPossibleGradientTypes(params Tensor[] args);
4345
}
4446
}

src/TensorFlowNET.Core/Gradients/Tape.RecordOperation.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using Microsoft.Extensions.Logging;
34
using Tensorflow.Util;
45
using static Tensorflow.tensorflow;
6+
using static Tensorflow.Binding;
57

68
namespace Tensorflow.Gradients
79
{
@@ -34,6 +36,7 @@ public void RecordOperation(string op_type,
3436
foreach (var o in output_tensors)
3537
{
3638
tensor_tape_[o.GetID()] = op_id;
39+
tf.Logger.LogDebug($"RecordOperation: tensor_tape_[{o.GetID()}] = {op_id}");
3740
tensor_usage_[o.GetID()] = 1;
3841
tensors.Add(o);
3942
}

src/TensorFlowNET.Core/Gradients/Tape.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using Tensorflow.Util;
4+
using Microsoft.Extensions.Logging;
35
using static Tensorflow.Binding;
46
using static Tensorflow.tensorflow;
57

@@ -42,6 +44,7 @@ public void Watch(long tensor_id)
4244
if (!CouldBackprop())
4345
return;
4446

47+
tf.Logger.LogDebug($"Watch tensor_id={tensor_id}");
4548
tensor_tape_.emplace(tensor_id, -1);
4649
}
4750

@@ -50,8 +53,13 @@ public bool ShouldRecord(long[] tensor_ids, TF_DataType[] dtypes)
5053
for (int i = 0; i < tensor_ids.Length; ++i)
5154
{
5255
if (tensor_tape_.find(tensor_ids[i]))
56+
{
5357
if (IsDtypeTrainable(dtypes[i]))
58+
{
59+
tf.Logger.LogDebug($"tape.h->ShouldRecord: should_record = true, tensor_tape_.size()={tensor_tape_.Count}, tensor_ids[{i}]={tensor_ids[i]}");
5460
return true;
61+
}
62+
}
5563
}
5664
return false;
5765
}

src/TensorFlowNET.Core/Tensorflow.Binding.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ TensorFlow .NET v0.30 is focused on making more Keras API work including:
8484
<ItemGroup>
8585
<PackageReference Include="Google.Protobuf" Version="3.11.4" />
8686
<PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.138" />
87+
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.10" />
88+
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.10" />
89+
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="3.1.10" />
8790
<PackageReference Include="NumSharp.Lite" Version="0.1.9" />
8891
<PackageReference Include="Protobuf.Text" Version="0.4.0" />
8992
</ItemGroup>

src/TensorFlowNET.Core/tensorflow.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using Microsoft.Extensions.DependencyInjection;
18+
using Microsoft.Extensions.Logging;
1719
using System.Collections.Generic;
1820
using Tensorflow.Contexts;
1921
using Tensorflow.Eager;
@@ -41,9 +43,18 @@ public partial class tensorflow : ITensorFlowObject
4143
public OpDefLibrary OpDefLib;
4244
public Context Context;
4345
public IEagerRunner Runner;
46+
public ILogger Logger;
47+
ServiceProvider serviceProvider;
4448

4549
public tensorflow()
4650
{
51+
serviceProvider = new ServiceCollection()
52+
.AddLogging(cfg => cfg.AddConsole())
53+
.Configure<LoggerFilterOptions>(cfg => cfg.MinLevel = LogLevel.Warning)
54+
.BuildServiceProvider();
55+
56+
Logger = serviceProvider.GetService<ILogger<tensorflow>>();
57+
4758
Status = new Status();
4859
Context = new Context(new ContextOptions(), Status);
4960
OpDefLib = new OpDefLibrary();

src/TensorFlowNET.Keras/Engine/Functional.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using Tensorflow.Keras.ArgsDefinition;
55
using Tensorflow.Keras.Utils;
6+
using Microsoft.Extensions.Logging;
67
using static Tensorflow.Binding;
78

89
namespace Tensorflow.Keras.Engine
@@ -335,7 +336,7 @@ Tensors run_internal_graph(Tensors inputs, bool training = false, Tensors mask =
335336

336337
var layer_inputs = node.MapArguments(tensor_dict);
337338

338-
// Console.WriteLine($"{node.Layer}: {node.Layer.Name}");
339+
tf.Logger.LogDebug($"{node.Layer}: {node.Layer.Name}");
339340
var outputs = node.Layer.Apply(layer_inputs, is_training: training);
340341

341342
// Update tensor_dict for next input

0 commit comments

Comments
 (0)