Skip to content

Commit 84440b2

Browse files
committed
manually construct graph for retain layer.
1 parent 5751c20 commit 84440b2

59 files changed

Lines changed: 1125 additions & 137 deletions

Some content is hidden

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

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflow
140140
* [CNN Text Classification](test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs)
141141

142142
* [Named Entity Recognition](test/TensorFlowNET.Examples/TextProcess/NER)
143+
* [Transfer Learning for Image Classification in InceptionV3](test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs)
143144

144145
### Contribute:
145146

src/KerasNET.Core/Layers/Dense.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Tensor __call__(Tensor x)
4040
var dot = tf.matmul(x, W);
4141
if (this.activation != null)
4242
dot = activation.Activate(dot);
43-
Console.WriteLine("Calling Layer \"" + name + "(" + np.array(dot.getShape().Dimensions).ToString() + ")\" ...");
43+
Console.WriteLine("Calling Layer \"" + name + "(" + np.array(dot.GetShape().Dimensions).ToString() + ")\" ...");
4444
return dot;
4545
}
4646
public TensorShape __shape__()

src/KerasNET.Core/Model.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Tensor getFlow()
6565
#endregion
6666

6767
#region Model Graph Form Layer Stack
68-
var flow_shape = features.getShape();
68+
var flow_shape = features.GetShape();
6969
Flow = features;
7070
for (int i = 0; i < layer_stack.Count; i++)
7171
{

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ public static Tensor one_hot(Tensor indices, int depth,
4949
Tensor off_value = null,
5050
TF_DataType dtype = TF_DataType.DtInvalid,
5151
int axis = -1,
52-
string name = null) => array_ops.one_hot(indices, depth, dtype: dtype, axis: axis, name: name);
52+
string name = null) => array_ops.one_hot(indices, depth, dtype: dtype, axis: axis, name: name);
53+
54+
/// <summary>
55+
/// A placeholder op that passes through `input` when its output is not fed.
56+
/// </summary>
57+
/// <typeparam name="T"></typeparam>
58+
/// <param name="input">A `Tensor`. The default value to produce when output is not fed.</param>
59+
/// <param name="shape">
60+
/// A `tf.TensorShape` or list of `int`s. The (possibly partial) shape of
61+
/// the tensor.
62+
/// </param>
63+
/// <param name="name">A name for the operation (optional).</param>
64+
/// <returns>A `Tensor`. Has the same type as `input`.</returns>
65+
public static Tensor placeholder_with_default<T>(T input, int[] shape, string name = null)
66+
=> gen_array_ops.placeholder_with_default(input, shape, name: name);
5367
}
5468
}

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,21 @@ public static Tensor reduce_sum(Tensor input, int? axis = null, int? reduction_i
277277
}
278278

279279
public static Tensor reduce_sum(Tensor input, int axis, int? reduction_indices = null)
280-
{
281-
return math_ops.reduce_sum(input, axis);
282-
}
280+
=> math_ops.reduce_sum(input, axis);
281+
282+
/// <summary>
283+
/// Computes the maximum of elements across dimensions of a tensor.
284+
/// </summary>
285+
/// <param name="input_tensor"></param>
286+
/// <param name="axis"></param>
287+
/// <param name="keepdims"></param>
288+
/// <param name="name"></param>
289+
/// <returns></returns>
290+
public static Tensor reduce_max(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
291+
=> math_ops.reduce_max(input_tensor, axis, keepdims, name);
292+
293+
public static Tensor reduce_min(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
294+
=> math_ops.reduce_min(input_tensor, axis, keepdims, name);
283295

284296
public static Tensor sigmoid<T>(T x, string name = null)
285297
=> math_ops.sigmoid(x, name: name);

src/TensorFlowNET.Core/Framework/common_shapes.py.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public static Tensor _broadcast_shape_helper(Tensor shape_x, Tensor shape_y)
3737

3838
public static bool has_fully_defined_shape(Tensor tensor)
3939
{
40-
return tensor.getShape().is_fully_defined();
40+
return tensor.GetShape().is_fully_defined();
4141
}
4242
}
4343
}

src/TensorFlowNET.Core/Framework/meta_graph.py.cs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static MetaGraphDef read_meta_graph_file(string filename)
1919
return meta_graph_def;
2020
}
2121

22-
public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_scoped_meta_graph_with_return_elements(MetaGraphDef meta_graph_or_file,
22+
public static (Dictionary<string, VariableV1>, ITensorOrOperation[]) import_scoped_meta_graph_with_return_elements(MetaGraphDef meta_graph_or_file,
2323
bool clear_devices = false,
2424
string import_scope = "",
2525
Dictionary<string, Tensor> input_map = null,
@@ -61,7 +61,7 @@ public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_sco
6161
return_elements: return_elements);
6262

6363
// Restores all the other collections.
64-
var variable_objects = new Dictionary<ByteString, RefVariable>();
64+
var variable_objects = new Dictionary<ByteString, VariableV1>();
6565
foreach(var col in meta_graph_def.CollectionDef.OrderBy(x => x.Key))
6666
{
6767
// Don't add unbound_inputs to the new graph.
@@ -83,11 +83,14 @@ public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_sco
8383
{
8484
foreach (var value in col.Value.BytesList.Value)
8585
{
86-
RefVariable variable = null;
86+
VariableV1 variable = null;
8787
if (!variable_objects.ContainsKey(value))
8888
{
8989
var proto = VariableDef.Parser.ParseFrom(value);
90-
variable = new RefVariable(variable_def: proto, import_scope: scope_to_prepend_to_names);
90+
if (proto.IsResource)
91+
variable = new ResourceVariable(variable_def: proto, import_scope: scope_to_prepend_to_names);
92+
else
93+
variable = new RefVariable(variable_def: proto, import_scope: scope_to_prepend_to_names);
9194
variable_objects[value] = variable;
9295
}
9396
variable = variable_objects[value];
@@ -126,9 +129,9 @@ public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_sco
126129
}
127130
}
128131

129-
var variables = graph.get_collection<RefVariable>(ops.GraphKeys.GLOBAL_VARIABLES,
132+
var variables = graph.get_collection<VariableV1>(ops.GraphKeys.GLOBAL_VARIABLES,
130133
scope: scope_to_prepend_to_names);
131-
var var_list = new Dictionary<string, RefVariable>();
134+
var var_list = new Dictionary<string, VariableV1>();
132135
variables.ForEach(v => var_list[ops.strip_name_scope(v.name, scope_to_prepend_to_names)] = v);
133136

134137
return (var_list, imported_return_elements);

src/TensorFlowNET.Core/Gradients/math_grad.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,27 @@ public static Tensor[] _AddGrad(Operation op, Tensor[] grads)
3232
return new Tensor[] { r1, r2 };
3333
}
3434

35+
public static Tensor[] _DivNoNanGrad(Operation op, Tensor[] grads)
36+
{
37+
var grad = grads[0];
38+
var x = op.inputs[0];
39+
var y = op.inputs[1];
40+
var sx = array_ops.shape(x);
41+
var sy = array_ops.shape(y);
42+
var (rx, ry) = gen_array_ops.broadcast_gradient_args(sx, sy);
43+
x = math_ops.conj(x);
44+
y = math_ops.conj(y);
45+
46+
var reduce_sum1 = math_ops.reduce_sum(math_ops.div_no_nan(grad, y), rx);
47+
var reduce_sum2 = math_ops.reduce_sum(grad * math_ops.div_no_nan(math_ops.div_no_nan(-x, y), y), ry);
48+
49+
return new Tensor[]
50+
{
51+
array_ops.reshape(reduce_sum1, sx),
52+
array_ops.reshape(reduce_sum2, sy)
53+
};
54+
}
55+
3556
/// <summary>
3657
/// Returns grad * exp(x).
3758
/// </summary>

src/TensorFlowNET.Core/Gradients/nn_grad.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,23 @@ public static Tensor[] _SoftmaxCrossEntropyWithLogitsGrad(Operation op, Tensor[]
7474
};
7575
}
7676

77+
public static Tensor[] _SparseSoftmaxCrossEntropyWithLogitsGrad(Operation op, Tensor[] grads)
78+
{
79+
var sparse_softmax_grad_without_gradient = array_ops.prevent_gradient(
80+
op.outputs[1],
81+
message: "Currently there is no way to take the second " +
82+
"derivative of sparse_softmax_cross_entropy_with_logits due to the fused " +
83+
"implementation's interaction with tf.gradients()");
84+
85+
var grad_0 = grads[0];
86+
87+
return new Tensor[]
88+
{
89+
_BroadcastMul(grad_0, sparse_softmax_grad_without_gradient),
90+
null
91+
};
92+
}
93+
7794
private static bool IsZero(Tensor g)
7895
{
7996
if (new string[] { "ZerosLike", "Zeros" }.Contains(g.op.type))

src/TensorFlowNET.Core/Gradients/ops.gradient_function_mapping.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ public static Func<Operation, Tensor[], Tensor[]> get_gradient_function(Operatio
2424
return nn_grad._BiasAddGrad(oper, out_grads);
2525
case "ConcatV2":
2626
return array_grad._ConcatGradV2(oper, out_grads);
27+
case "DivNoNan":
28+
return math_grad._DivNoNanGrad(oper, out_grads);
2729
case "Exp":
2830
return math_grad._ExpGrad(oper, out_grads);
2931
case "Identity":
@@ -62,6 +64,8 @@ public static Func<Operation, Tensor[], Tensor[]> get_gradient_function(Operatio
6264
return nn_grad._SoftmaxGrad(oper, out_grads);
6365
case "SoftmaxCrossEntropyWithLogits":
6466
return nn_grad._SoftmaxCrossEntropyWithLogitsGrad(oper, out_grads);
67+
case "SparseSoftmaxCrossEntropyWithLogits":
68+
return nn_grad._SparseSoftmaxCrossEntropyWithLogitsGrad(oper, out_grads);
6569
case "Transpose":
6670
return array_grad._TransposeGrad(oper, out_grads);
6771
case "TopK":

0 commit comments

Comments
 (0)