| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 4ff993b commit f13e35d
8 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,2 +1,15 @@ | |||
| 1 | 1 | # Chapter. Gradient | |
| 2 | 2 | ||
| 3 | + ### Register custom gradient function | ||
| 4 | + | ||
| 5 | + TF.NET is extensible which can be added custom gradient function. | ||
| 6 | + | ||
| 7 | + ```csharp | ||
| 8 | + // define gradient function | ||
| 9 | + ops.RegisterGradientFunction("ConcatV2", (oper, out_grads) => | ||
| 10 | + { | ||
| 11 | + var grad = grads[0]; | ||
| 12 | + return new Tensor[]{ }; | ||
| 13 | + }); | ||
| 14 | + ``` | ||
| 15 | + | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,16 @@ | |||
| 1 | + using System; | ||
| 2 | + using System.Collections.Generic; | ||
| 3 | + using System.Text; | ||
| 4 | + | ||
| 5 | + namespace Tensorflow.Gradients | ||
| 6 | + { | ||
| 7 | + public class RegisterGradient : Attribute | ||
| 8 | + { | ||
| 9 | + public string Name { get; set; } | ||
| 10 | + | ||
| 11 | + public RegisterGradient(string name) | ||
| 12 | + { | ||
| 13 | + Name = name; | ||
| 14 | + } | ||
| 15 | + } | ||
| 16 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,8 +10,10 @@ namespace Tensorflow.Gradients | |||
| 10 | 10 | /// <summary> | |
| 11 | 11 | /// tensorflow\python\ops\array_grad.py | |
| 12 | 12 | /// </summary> | |
| 13 | + [RegisterGradient("array_grad")] | ||
| 13 | 14 | public class array_grad | |
| 14 | 15 | { | |
| 16 | + [RegisterGradient("ConcatV2")] | ||
| 15 | 17 | public static Tensor[] _ConcatGradV2(Operation op, Tensor[] grads) | |
| 16 | 18 | { | |
| 17 | 19 | var grad = grads[0]; | |
@@ -123,12 +125,13 @@ private static Tensor[] _ExtractInputShapes(Tensor[] inputs) | |||
| 123 | 125 | return gen_ops.shape_n(inputs); | |
| 124 | 126 | } | |
| 125 | 127 | ||
| 126 | - | ||
| 128 | + [RegisterGradient("Reshape")] | ||
| 127 | 129 | public static Tensor[] _ReshapeGrad(Operation op, Tensor[] grads) | |
| 128 | 130 | { | |
| 129 | 131 | return new Tensor[] { array_ops.reshape(grads[0], array_ops.shape(op.inputs[0])), null }; | |
| 130 | 132 | } | |
| 131 | 133 | ||
| 134 | + [RegisterGradient("Squeeze")] | ||
| 132 | 135 | public static Tensor[] _SqueezeGrad(Operation op, Tensor[] grads) | |
| 133 | 136 | { | |
| 134 | 137 | return new Tensor[] { _ReshapeToInput(op, grads[0]) }; | |
@@ -139,6 +142,7 @@ private static Tensor _ReshapeToInput(Operation op, Tensor grad) | |||
| 139 | 142 | return array_ops.reshape(grad, array_ops.shape(op.inputs[0])); | |
| 140 | 143 | } | |
| 141 | 144 | ||
| 145 | + [RegisterGradient("Transpose")] | ||
| 142 | 146 | public static Tensor[] _TransposeGrad(Operation op, Tensor[] grads) | |
| 143 | 147 | { | |
| 144 | 148 | var p = op.inputs[1]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -69,11 +69,12 @@ public Tensor[] _SwitchGrad(Tensor op, Tensor[] grads) | |||
| 69 | 69 | // false_grad = switch(grad[0], op.inputs[1])[0] | |
| 70 | 70 | // true_grad = switch(grad[1], op.inputs[1])[1] | |
| 71 | 71 | // return merge([false_grad, true_grad])[0], None | |
| 72 | - } | ||
| 73 | - | ||
| 72 | + } | ||
| 73 | + | ||
| 74 | 74 | /// <summary> | |
| 75 | 75 | /// Gradients for a Merge op are calculated using a Switch op. | |
| 76 | 76 | /// </summary> | |
| 77 | + [RegisterGradient("Merge")] | ||
| 77 | 78 | public static Tensor[] _MergeGrad(Operation op, Tensor[] grads) | |
| 78 | 79 | { | |
| 79 | 80 | var grad = grads[0]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -10,8 +10,10 @@ namespace Tensorflow.Gradients | |||
| 10 | 10 | /// <summary> | |
| 11 | 11 | /// Gradients for operators defined in math_ops.py. | |
| 12 | 12 | /// </summary> | |
| 13 | + [RegisterGradient("math_grad")] | ||
| 13 | 14 | public class math_grad | |
| 14 | 15 | { | |
| 16 | + [RegisterGradient("Add")] | ||
| 15 | 17 | public static Tensor[] _AddGrad(Operation op, Tensor[] grads) | |
| 16 | 18 | { | |
| 17 | 19 | var x = op.inputs[0]; | |
@@ -32,6 +34,7 @@ public static Tensor[] _AddGrad(Operation op, Tensor[] grads) | |||
| 32 | 34 | return new Tensor[] { r1, r2 }; | |
| 33 | 35 | } | |
| 34 | 36 | ||
| 37 | + [RegisterGradient("DivNoNan")] | ||
| 35 | 38 | public static Tensor[] _DivNoNanGrad(Operation op, Tensor[] grads) | |
| 36 | 39 | { | |
| 37 | 40 | var grad = grads[0]; | |
@@ -59,6 +62,7 @@ public static Tensor[] _DivNoNanGrad(Operation op, Tensor[] grads) | |||
| 59 | 62 | /// <param name="op"></param> | |
| 60 | 63 | /// <param name="grads"></param> | |
| 61 | 64 | /// <returns></returns> | |
| 65 | + [RegisterGradient("Exp")] | ||
| 62 | 66 | public static Tensor[] _ExpGrad(Operation op, Tensor[] grads) | |
| 63 | 67 | { | |
| 64 | 68 | var grad = grads[0]; | |
@@ -69,11 +73,13 @@ public static Tensor[] _ExpGrad(Operation op, Tensor[] grads) | |||
| 69 | 73 | }); | |
| 70 | 74 | } | |
| 71 | 75 | ||
| 76 | + [RegisterGradient("Identity")] | ||
| 72 | 77 | public static Tensor[] _IdGrad(Operation op, Tensor[] grads) | |
| 73 | 78 | { | |
| 74 | 79 | return new Tensor[] { grads[0] }; | |
| 75 | 80 | } | |
| 76 | 81 | ||
| 82 | + [RegisterGradient("Log")] | ||
| 77 | 83 | public static Tensor[] _LogGrad(Operation op, Tensor[] grads) | |
| 78 | 84 | { | |
| 79 | 85 | var grad = grads[0]; | |
@@ -84,6 +90,7 @@ public static Tensor[] _LogGrad(Operation op, Tensor[] grads) | |||
| 84 | 90 | }); | |
| 85 | 91 | } | |
| 86 | 92 | ||
| 93 | + [RegisterGradient("Mul")] | ||
| 87 | 94 | public static Tensor[] _MulGrad(Operation op, Tensor[] grads) | |
| 88 | 95 | { | |
| 89 | 96 | var x = op.inputs[0]; | |
@@ -112,6 +119,7 @@ public static Tensor[] _MulGrad(Operation op, Tensor[] grads) | |||
| 112 | 119 | return new Tensor[] { reshape1, reshape2 }; | |
| 113 | 120 | } | |
| 114 | 121 | ||
| 122 | + [RegisterGradient("MatMul")] | ||
| 115 | 123 | public static Tensor[] _MatMulGrad(Operation op, Tensor[] grads) | |
| 116 | 124 | { | |
| 117 | 125 | var grad = grads[0]; | |
@@ -145,6 +153,7 @@ public static Tensor[] _MatMulGrad(Operation op, Tensor[] grads) | |||
| 145 | 153 | return new Tensor[] { grad_a, grad_b }; | |
| 146 | 154 | } | |
| 147 | 155 | ||
| 156 | + [RegisterGradient("Mean")] | ||
| 148 | 157 | public static Tensor[] _MeanGrad(Operation op, Tensor[] grads) | |
| 149 | 158 | { | |
| 150 | 159 | var grad = grads[0]; | |
@@ -159,6 +168,7 @@ public static Tensor[] _MeanGrad(Operation op, Tensor[] grads) | |||
| 159 | 168 | return new Tensor[] { math_ops.truediv(sum_grad, math_ops.cast(factor, sum_grad.dtype)), null }; | |
| 160 | 169 | } | |
| 161 | 170 | ||
| 171 | + [RegisterGradient("Neg")] | ||
| 162 | 172 | public static Tensor[] _NegGrad(Operation op, Tensor[] grads) | |
| 163 | 173 | { | |
| 164 | 174 | return new Tensor[] { -grads[0] }; | |
@@ -169,6 +179,7 @@ private static Tensor _safe_shape_div(Tensor x, Tensor y) | |||
| 169 | 179 | return math_ops.floordiv(x, gen_math_ops.maximum(y, 1)); | |
| 170 | 180 | } | |
| 171 | 181 | ||
| 182 | + [RegisterGradient("Sub")] | ||
| 172 | 183 | public static Tensor[] _SubGrad(Operation op, Tensor[] grads) | |
| 173 | 184 | { | |
| 174 | 185 | var grad = grads[0]; | |
@@ -198,6 +209,7 @@ public static bool _ShapesFullySpecifiedAndEqual(Tensor x, Tensor y, Tensor grad | |||
| 198 | 209 | !x_shape.Contains(-1); | |
| 199 | 210 | } | |
| 200 | 211 | ||
| 212 | + [RegisterGradient("Sum")] | ||
| 201 | 213 | public static Tensor[] _SumGrad(Operation op, Tensor[] grads) | |
| 202 | 214 | { | |
| 203 | 215 | var grad = grads[0]; | |
@@ -231,6 +243,7 @@ public static Tensor[] _SumGrad(Operation op, Tensor[] grads) | |||
| 231 | 243 | return new Tensor[] { gen_array_ops.tile(grad, tile_scaling), null }; | |
| 232 | 244 | } | |
| 233 | 245 | ||
| 246 | + [RegisterGradient("RealDiv")] | ||
| 234 | 247 | public static Tensor[] _RealDivGrad(Operation op, Tensor[] grads) | |
| 235 | 248 | { | |
| 236 | 249 | var grad = grads[0]; | |
@@ -254,6 +267,7 @@ public static Tensor[] _RealDivGrad(Operation op, Tensor[] grads) | |||
| 254 | 267 | return new Tensor[] { reshape2, reshape1 }; | |
| 255 | 268 | } | |
| 256 | 269 | ||
| 270 | + [RegisterGradient("Sigmoid")] | ||
| 257 | 271 | public static Tensor[] _SigmoidGrad(Operation op, Tensor[] grads) | |
| 258 | 272 | { | |
| 259 | 273 | var grad = grads[0]; | |
@@ -266,6 +280,7 @@ public static Tensor[] _SigmoidGrad(Operation op, Tensor[] grads) | |||
| 266 | 280 | }); | |
| 267 | 281 | } | |
| 268 | 282 | ||
| 283 | + [RegisterGradient("Square")] | ||
| 269 | 284 | public static Tensor[] _SquareGrad(Operation op, Tensor[] grads) | |
| 270 | 285 | { | |
| 271 | 286 | var grad = grads[0]; | |
@@ -279,6 +294,7 @@ public static Tensor[] _SquareGrad(Operation op, Tensor[] grads) | |||
| 279 | 294 | }); | |
| 280 | 295 | } | |
| 281 | 296 | ||
| 297 | + [RegisterGradient("Pow")] | ||
| 282 | 298 | public static Tensor[] _PowGrad(Operation op, Tensor[] grads) | |
| 283 | 299 | { | |
| 284 | 300 | var grad = grads[0]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -9,6 +9,7 @@ namespace Tensorflow.Gradients | |||
| 9 | 9 | /// <summary> | |
| 10 | 10 | /// | |
| 11 | 11 | /// </summary> | |
| 12 | + [RegisterGradient("math_grad")] | ||
| 12 | 13 | public class nn_grad | |
| 13 | 14 | { | |
| 14 | 15 | /// <summary> | |
@@ -17,6 +18,7 @@ public class nn_grad | |||
| 17 | 18 | /// <param name="op"></param> | |
| 18 | 19 | /// <param name="grad"></param> | |
| 19 | 20 | /// <returns></returns> | |
| 21 | + [RegisterGradient("BiasAdd")] | ||
| 20 | 22 | public static Tensor[] _BiasAddGrad(Operation op, Tensor[] grads) | |
| 21 | 23 | { | |
| 22 | 24 | var grad = grads[0]; | |
@@ -25,6 +27,7 @@ public static Tensor[] _BiasAddGrad(Operation op, Tensor[] grads) | |||
| 25 | 27 | return new Tensor[] { grad, bias_add_grad }; | |
| 26 | 28 | } | |
| 27 | 29 | ||
| 30 | + [RegisterGradient("Relu")] | ||
| 28 | 31 | public static Tensor[] _ReluGrad(Operation op, Tensor[] grads) | |
| 29 | 32 | { | |
| 30 | 33 | return new Tensor[] { gen_nn_ops.relu_grad(grads[0], op.outputs[0]) }; | |
@@ -36,6 +39,7 @@ public static Tensor[] _ReluGrad(Operation op, Tensor[] grads) | |||
| 36 | 39 | /// <param name="op"></param> | |
| 37 | 40 | /// <param name="grads"></param> | |
| 38 | 41 | /// <returns></returns> | |
| 42 | + [RegisterGradient("Softmax")] | ||
| 39 | 43 | public static Tensor[] _SoftmaxGrad(Operation op, Tensor[] grads) | |
| 40 | 44 | { | |
| 41 | 45 | var grad_softmax = grads[0]; | |
@@ -54,6 +58,7 @@ public static Tensor[] _SoftmaxGrad(Operation op, Tensor[] grads) | |||
| 54 | 58 | /// <param name="grad_loss"></param> | |
| 55 | 59 | /// <param name="grad_grad"></param> | |
| 56 | 60 | /// <returns></returns> | |
| 61 | + [RegisterGradient("SoftmaxCrossEntropyWithLogits")] | ||
| 57 | 62 | public static Tensor[] _SoftmaxCrossEntropyWithLogitsGrad(Operation op, Tensor[] grads) | |
| 58 | 63 | { | |
| 59 | 64 | var grad_loss = grads[0]; | |
@@ -74,6 +79,7 @@ public static Tensor[] _SoftmaxCrossEntropyWithLogitsGrad(Operation op, Tensor[] | |||
| 74 | 79 | }; | |
| 75 | 80 | } | |
| 76 | 81 | ||
| 82 | + [RegisterGradient("SparseSoftmaxCrossEntropyWithLogits")] | ||
| 77 | 83 | public static Tensor[] _SparseSoftmaxCrossEntropyWithLogitsGrad(Operation op, Tensor[] grads) | |
| 78 | 84 | { | |
| 79 | 85 | var sparse_softmax_grad_without_gradient = array_ops.prevent_gradient( | |
@@ -111,6 +117,7 @@ private static Tensor _BroadcastMul(Tensor vec, Tensor mat) | |||
| 111 | 117 | /// <param name="op"></param> | |
| 112 | 118 | /// <param name="grads"></param> | |
| 113 | 119 | /// <returns></returns> | |
| 120 | + [RegisterGradient("TopK")] | ||
| 114 | 121 | public static Tensor[] _TopKGrad(Operation op, Tensor[] grads) | |
| 115 | 122 | { | |
| 116 | 123 | var grad = grads[0]; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,80 +1,65 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Collections.Generic; | |
| 3 | + using System.Linq; | ||
| 4 | + using System.Reflection; | ||
| 3 | 5 | using System.Text; | |
| 4 | 6 | using Tensorflow.Gradients; | |
| 5 | 7 | ||
| 6 | 8 | namespace Tensorflow | |
| 7 | 9 | { | |
| 8 | 10 | public partial class ops | |
| 9 | 11 | { | |
| 12 | + static Dictionary<string, Func<Operation, Tensor[], Tensor[]>> gradientFunctions = null; | ||
| 13 | + | ||
| 14 | + /// <summary> | ||
| 15 | + /// Regiter new gradient function | ||
| 16 | + /// </summary> | ||
| 17 | + /// <param name="name">operation type</param> | ||
| 18 | + /// <param name="func">function delegate</param> | ||
| 19 | + public static void RegisterGradientFunction(string name, Func<Operation, Tensor[], Tensor[]> func) | ||
| 20 | + { | ||
| 21 | + if(gradientFunctions == null) | ||
| 22 | + gradientFunctions = new Dictionary<string, Func<Operation, Tensor[], Tensor[]>>(); | ||
| 23 | + | ||
| 24 | + gradientFunctions[name] = func; | ||
| 25 | + } | ||
| 26 | + | ||
| 10 | 27 | public static Func<Operation, Tensor[], Tensor[]> get_gradient_function(Operation op) | |
| 11 | 28 | { | |
| 12 | 29 | if (op.inputs == null) return null; | |
| 13 | 30 | ||
| 14 | - // map tensorflow\python\ops\math_grad.py | ||
| 15 | - return (oper, out_grads) => | ||
| 31 | + if (gradientFunctions == null) | ||
| 16 | 32 | { | |
| 17 | - // Console.WriteLine($"get_gradient_function: {oper.type} '{oper.name}'"); | ||
| 33 | + gradientFunctions = new Dictionary<string, Func<Operation, Tensor[], Tensor[]>>(); | ||
| 18 | 34 | ||
| 19 | - switch (oper.type) | ||
| 35 | + var gradGroups = Assembly.GetExecutingAssembly() | ||
| 36 | + .GetTypes() | ||
| 37 | + .Where(x => x.GetCustomAttribute<RegisterGradient>() != null) | ||
| 38 | + .ToArray(); | ||
| 39 | + | ||
| 40 | + foreach (var g in gradGroups) | ||
| 20 | 41 | { | |
| 21 | - case "Add": | ||
| 22 | - return math_grad._AddGrad(oper, out_grads); | ||
| 23 | - case "BiasAdd": | ||
| 24 | - return nn_grad._BiasAddGrad(oper, out_grads); | ||
| 25 | - case "ConcatV2": | ||
| 26 | - return array_grad._ConcatGradV2(oper, out_grads); | ||
| 27 | - case "DivNoNan": | ||
| 28 | - return math_grad._DivNoNanGrad(oper, out_grads); | ||
| 29 | - case "Exp": | ||
| 30 | - return math_grad._ExpGrad(oper, out_grads); | ||
| 31 | - case "Identity": | ||
| 32 | - return math_grad._IdGrad(oper, out_grads); | ||
| 33 | - case "Log": | ||
| 34 | - return math_grad._LogGrad(oper, out_grads); | ||
| 35 | - case "MatMul": | ||
| 36 | - return math_grad._MatMulGrad(oper, out_grads); | ||
| 37 | - case "Merge": | ||
| 38 | - return control_flow_grad._MergeGrad(oper, out_grads); | ||
| 39 | - case "Mul": | ||
| 40 | - return math_grad._MulGrad(oper, out_grads); | ||
| 41 | - case "Mean": | ||
| 42 | - return math_grad._MeanGrad(oper, out_grads); | ||
| 43 | - case "Neg": | ||
| 44 | - return math_grad._NegGrad(oper, out_grads); | ||
| 45 | - case "Sum": | ||
| 46 | - return math_grad._SumGrad(oper, out_grads); | ||
| 47 | - case "Sub": | ||
| 48 | - return math_grad._SubGrad(oper, out_grads); | ||
| 49 | - case "Pow": | ||
| 50 | - return math_grad._PowGrad(oper, out_grads); | ||
| 51 | - case "RealDiv": | ||
| 52 | - return math_grad._RealDivGrad(oper, out_grads); | ||
| 53 | - case "Reshape": | ||
| 54 | - return array_grad._ReshapeGrad(oper, out_grads); | ||
| 55 | - case "Relu": | ||
| 56 | - return nn_grad._ReluGrad(oper, out_grads); | ||
| 57 | - case "Sigmoid": | ||
| 58 | - return math_grad._SigmoidGrad(oper, out_grads); | ||
| 59 | - case "Square": | ||
| 60 | - return math_grad._SquareGrad(oper, out_grads); | ||
| 61 | - case "Squeeze": | ||
| 62 | - return array_grad._SqueezeGrad(oper, out_grads); | ||
| 63 | - case "Softmax": | ||
| 64 | - return nn_grad._SoftmaxGrad(oper, out_grads); | ||
| 65 | - case "SoftmaxCrossEntropyWithLogits": | ||
| 66 | - return nn_grad._SoftmaxCrossEntropyWithLogitsGrad(oper, out_grads); | ||
| 67 | - case "SparseSoftmaxCrossEntropyWithLogits": | ||
| 68 | - return nn_grad._SparseSoftmaxCrossEntropyWithLogitsGrad(oper, out_grads); | ||
| 69 | - case "Transpose": | ||
| 70 | - return array_grad._TransposeGrad(oper, out_grads); | ||
| 71 | - case "TopK": | ||
| 72 | - case "TopKV2": | ||
| 73 | - return nn_grad._TopKGrad(oper, out_grads); | ||
| 74 | - default: | ||
| 75 | - throw new NotImplementedException($"get_gradient_function {oper.type}"); | ||
| 42 | + var methods = g.GetMethods().Where(x => x.GetCustomAttribute<RegisterGradient>() != null) | ||
| 43 | + .ToArray(); | ||
| 44 | + | ||
| 45 | + foreach (var m in methods) | ||
| 46 | + { | ||
| 47 | + RegisterGradientFunction(m.GetCustomAttribute<RegisterGradient>().Name, | ||
| 48 | + (oper, out_grads) => | ||
| 49 | + g.InvokeMember(m.Name, | ||
| 50 | + BindingFlags.InvokeMethod, | ||
| 51 | + null, | ||
| 52 | + null, | ||
| 53 | + args: new object[] { oper, out_grads }) as Tensor[] | ||
| 54 | + ); | ||
| 55 | + } | ||
| 76 | 56 | } | |
| 77 | - }; | ||
| 57 | + } | ||
| 58 | + | ||
| 59 | + if (!gradientFunctions.ContainsKey(op.type)) | ||
| 60 | + throw new NotImplementedException($"can't get graident function through get_gradient_function {op.type}"); | ||
| 61 | + | ||
| 62 | + return gradientFunctions[op.type]; | ||
| 78 | 63 | } | |
| 79 | 64 | } | |
| 80 | 65 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -20,7 +20,8 @@ Docs: https://tensorflownet.readthedocs.io</Description> | |||
| 20 | 20 | <AssemblyVersion>0.8.1.0</AssemblyVersion> | |
| 21 | 21 | <PackageReleaseNotes>Changes since v0.8: | |
| 22 | 22 | ||
| 23 | - Removed global static graph instance.</PackageReleaseNotes> | ||
| 23 | + 1. Removed global static graph instance. | ||
| 24 | + 2. Provide custom gradient function.</PackageReleaseNotes> | ||
| 24 | 25 | <LangVersion>7.2</LangVersion> | |
| 25 | 26 | <FileVersion>0.8.1.0</FileVersion> | |
| 26 | 27 | </PropertyGroup> | |
| Back | FazBrowse Home | New Git URL |
0 commit comments