Skip to content

Commit ea3fc6a

Browse files
committed
organize keras files.
1 parent 905c4a9 commit ea3fc6a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+103
-56
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Linq;
3-
using Microsoft.Extensions.Logging;
43
using Tensorflow.Gradients;
54
using static Tensorflow.Binding;
65
using static Tensorflow.tensorflow;
@@ -39,7 +38,7 @@ public bool RecordGradient(string op_name,
3938
}*/
4039
}
4140

42-
tf.Logger.LogDebug($"RecordGradient: should_record={should_record}, op_name={op_name}");
41+
tf.Logger.Debug($"RecordGradient: should_record={should_record}, op_name={op_name}");
4342
if (!should_record) return should_record;
4443

4544
Tensor[] op_outputs;

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System;
22
using System.Collections.Generic;
3-
using Microsoft.Extensions.Logging;
43
using Tensorflow.Util;
54
using static Tensorflow.tensorflow;
65
using static Tensorflow.Binding;
@@ -36,7 +35,7 @@ public void RecordOperation(string op_type,
3635
foreach (var o in output_tensors)
3736
{
3837
tensor_tape_[o.GetID()] = op_id;
39-
tf.Logger.LogDebug($"RecordOperation: tensor_tape_[{o.GetID()}] = {op_id}");
38+
tf.Logger.Debug($"RecordOperation: tensor_tape_[{o.GetID()}] = {op_id}");
4039
tensor_usage_[o.GetID()] = 1;
4140
tensors.Add(o);
4241
}

src/TensorFlowNET.Core/Gradients/Tape.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using Tensorflow.Util;
4-
using Microsoft.Extensions.Logging;
54
using static Tensorflow.Binding;
65
using static Tensorflow.tensorflow;
76

@@ -44,7 +43,7 @@ public void Watch(long tensor_id)
4443
if (!CouldBackprop())
4544
return;
4645

47-
tf.Logger.LogDebug($"Watch tensor_id={tensor_id}");
46+
tf.Logger.Debug($"Watch tensor_id={tensor_id}");
4847
tensor_tape_.emplace(tensor_id, -1);
4948
}
5049

@@ -56,7 +55,7 @@ public bool ShouldRecord(long[] tensor_ids, TF_DataType[] dtypes)
5655
{
5756
if (IsDtypeTrainable(dtypes[i]))
5857
{
59-
tf.Logger.LogDebug($"tape.h->ShouldRecord: should_record = true, tensor_tape_.size()={tensor_tape_.Count}, tensor_ids[{i}]={tensor_ids[i]}");
58+
tf.Logger.Debug($"tape.h->ShouldRecord: should_record = true, tensor_tape_.size()={tensor_tape_.Count}, tensor_ids[{i}]={tensor_ids[i]}");
6059
return true;
6160
}
6261
}

src/TensorFlowNET.Core/Gradients/array_grad.cs

Lines changed: 33 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ limitations under the License.
1616

1717
using System.Collections.Generic;
1818
using System.Linq;
19+
using Tensorflow.Eager;
1920
using Tensorflow.Framework;
2021
using static Tensorflow.Binding;
2122

@@ -82,7 +83,14 @@ private static Tensor[] _ConcatGradHelper(Operation op, Tensor grad, int start_v
8283
.ToArray();
8384

8485
var out_grads = new List<Tensor>();
85-
if (constant_op.is_constant(concat_dim))
86+
if(concat_dim is EagerTensor)
87+
{
88+
var non_neg_concat_dim = (int)concat_dim % input_values[0].rank;
89+
var sizes = input_values.Select(x => x.shape[non_neg_concat_dim]).ToArray();
90+
var sizes_tensor = constant_op.constant(sizes);
91+
out_grads = gen_array_ops.split_v(grad, sizes_tensor, sizes[0], non_neg_concat_dim).ToList();
92+
}
93+
else if (constant_op.is_constant(concat_dim))
8694
{
8795
/*If concat_dim is a constant defined in a different context,
8896
then we duplicate it in the current context to avoid passing it
@@ -97,33 +105,33 @@ through an Enter node.
97105
var value = tensor_util.constant_value(concat_dim);
98106
concat_dim = constant_op.constant(value: value, dtype: concat_dim.dtype);
99107
}
100-
}
101108

102-
// Using mod here for convenience since concat_dim is already verified
103-
// in concat implementation to be within the allowed [-rank, rank) range.
104-
var non_neg_concat_dim = concat_dim % array_ops.rank(input_values[0]);
109+
// Using mod here for convenience since concat_dim is already verified
110+
// in concat implementation to be within the allowed [-rank, rank) range.
111+
var non_neg_concat_dim = concat_dim % array_ops.rank(input_values[0]);
105112

106-
// Get the inputs' tensor shapes
107-
var sizes = _ExtractInputShapes(input_values);
113+
// Get the inputs' tensor shapes
114+
var sizes = _ExtractInputShapes(input_values);
108115

109-
/* The magic number of 16 was found through benchmarking a range of sizes
110-
on CPUs and a Maxwell TitanX. A speedup was seen in a large majority of
111-
cases when switching implementations at N=16, but it is possible that
112-
there will be a small number of performance regressions.*/
113-
if (len(sizes) > 16)
114-
{
115-
// extract the size of each input along the concat dimension
116-
var slice = array_ops.slice(array_ops.stack(sizes, axis: 1),
117-
new Tensor[] { non_neg_concat_dim, tf.constant(0) },
118-
new Tensor[] { tf.constant(1), tf.constant(-1) });
119-
var squeeze_sizes = array_ops.squeeze(slice);
120-
out_grads = array_ops.split(axis: grad, value: squeeze_sizes, num_split: (int)non_neg_concat_dim).ToList();
121-
}
122-
else
123-
{
124-
var offset = gen_array_ops.concat_offset(non_neg_concat_dim, sizes);
125-
foreach (var (begin, size) in zip(offset, sizes))
126-
out_grads.Add(gen_array_ops.slice(grad, begin, size));
116+
/* The magic number of 16 was found through benchmarking a range of sizes
117+
on CPUs and a Maxwell TitanX. A speedup was seen in a large majority of
118+
cases when switching implementations at N=16, but it is possible that
119+
there will be a small number of performance regressions.*/
120+
if (len(sizes) > 16)
121+
{
122+
// extract the size of each input along the concat dimension
123+
var slice = array_ops.slice(array_ops.stack(sizes, axis: 1),
124+
new Tensor[] { non_neg_concat_dim, tf.constant(0) },
125+
new Tensor[] { tf.constant(1), tf.constant(-1) });
126+
var squeeze_sizes = array_ops.squeeze(slice);
127+
out_grads = array_ops.split(axis: grad, value: squeeze_sizes, num_split: (int)non_neg_concat_dim).ToList();
128+
}
129+
else
130+
{
131+
var offset = gen_array_ops.concat_offset(non_neg_concat_dim, sizes);
132+
foreach (var (begin, size) in zip(offset, sizes))
133+
out_grads.Add(gen_array_ops.slice(grad, begin, size));
134+
}
127135
}
128136

129137
return (end_value_index <= dim_index ?

src/TensorFlowNET.Core/Graphs/AutoGraphAttribute.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ public override void OnEntry(MethodExecutionArgs args)
5656

5757
public override void OnExit(MethodExecutionArgs args)
5858
{
59+
var returnValue = mark_as_return(args.ReturnValue as Tensors);
60+
5961
var opers = graph._nodes_by_name.Values.Select(x => x as Operation).ToArray();
6062

6163
if (args.ReturnValue is Tensors outputs)
@@ -102,5 +104,10 @@ public override void OnExit(MethodExecutionArgs args)
102104
// run function
103105
args.ReturnValue = function(originalInputs);
104106
}
107+
108+
Tensor mark_as_return(Tensor tensor)
109+
{
110+
return array_ops.identity(tensor);
111+
}
105112
}
106113
}

src/TensorFlowNET.Core/Keras/ArgsDefinition/LeakyReLUArgs.cs renamed to src/TensorFlowNET.Core/Keras/ArgsDefinition/Activation/LeakyReLuArgs.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace Tensorflow.Keras.ArgsDefinition
66
{
7-
public class LeakyReLUArgs : LayerArgs
7+
public class LeakyReLuArgs : LayerArgs
88
{
99
/// <summary>
1010
/// Negative slope coefficient.

src/TensorFlowNET.Core/Keras/ArgsDefinition/Conv2DArgs.cs renamed to src/TensorFlowNET.Core/Keras/ArgsDefinition/Convolution/Conv2DArgs.cs

File renamed without changes.

src/TensorFlowNET.Core/Keras/ArgsDefinition/ConvolutionalArgs.cs renamed to src/TensorFlowNET.Core/Keras/ArgsDefinition/Convolution/ConvolutionalArgs.cs

File renamed without changes.

src/TensorFlowNET.Core/Keras/ArgsDefinition/DenseArgs.cs renamed to src/TensorFlowNET.Core/Keras/ArgsDefinition/Core/DenseArgs.cs

File renamed without changes.

src/TensorFlowNET.Core/Keras/ArgsDefinition/EmbeddingArgs.cs renamed to src/TensorFlowNET.Core/Keras/ArgsDefinition/Core/EmbeddingArgs.cs

File renamed without changes.

0 commit comments

Comments
 (0)