| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5751c20 commit 84440b2
59 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,6 +140,7 @@ Read the docs & book [The Definitive Guide to Tensorflow.NET](https://tensorflow | |||
| 140 | 140 | * [CNN Text Classification](test/TensorFlowNET.Examples/TextProcess/cnn_models/VdCnn.cs) | |
| 141 | 141 | ||
| 142 | 142 | * [Named Entity Recognition](test/TensorFlowNET.Examples/TextProcess/NER) | |
| 143 | + * [Transfer Learning for Image Classification in InceptionV3](test/TensorFlowNET.Examples/ImageProcess/RetrainImageClassifier.cs) | ||
| 143 | 144 | ||
| 144 | 145 | ### Contribute: | |
| 145 | 146 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -40,7 +40,7 @@ public Tensor __call__(Tensor x) | |||
| 40 | 40 | var dot = tf.matmul(x, W); | |
| 41 | 41 | if (this.activation != null) | |
| 42 | 42 | 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() + ")\" ..."); | ||
| 44 | 44 | return dot; | |
| 45 | 45 | } | |
| 46 | 46 | public TensorShape __shape__() | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -65,7 +65,7 @@ public Tensor getFlow() | |||
| 65 | 65 | #endregion | |
| 66 | 66 | ||
| 67 | 67 | #region Model Graph Form Layer Stack | |
| 68 | - var flow_shape = features.getShape(); | ||
| 68 | + var flow_shape = features.GetShape(); | ||
| 69 | 69 | Flow = features; | |
| 70 | 70 | for (int i = 0; i < layer_stack.Count; i++) | |
| 71 | 71 | { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -49,6 +49,20 @@ public static Tensor one_hot(Tensor indices, int depth, | |||
| 49 | 49 | Tensor off_value = null, | |
| 50 | 50 | TF_DataType dtype = TF_DataType.DtInvalid, | |
| 51 | 51 | 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); | ||
| 53 | 67 | } | |
| 54 | 68 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -277,9 +277,21 @@ public static Tensor reduce_sum(Tensor input, int? axis = null, int? reduction_i | |||
| 277 | 277 | } | |
| 278 | 278 | ||
| 279 | 279 | 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); | ||
| 283 | 295 | ||
| 284 | 296 | public static Tensor sigmoid<T>(T x, string name = null) | |
| 285 | 297 | => math_ops.sigmoid(x, name: name); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -37,7 +37,7 @@ public static Tensor _broadcast_shape_helper(Tensor shape_x, Tensor shape_y) | |||
| 37 | 37 | ||
| 38 | 38 | public static bool has_fully_defined_shape(Tensor tensor) | |
| 39 | 39 | { | |
| 40 | - return tensor.getShape().is_fully_defined(); | ||
| 40 | + return tensor.GetShape().is_fully_defined(); | ||
| 41 | 41 | } | |
| 42 | 42 | } | |
| 43 | 43 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -19,7 +19,7 @@ public static MetaGraphDef read_meta_graph_file(string filename) | |||
| 19 | 19 | return meta_graph_def; | |
| 20 | 20 | } | |
| 21 | 21 | ||
| 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, | ||
| 23 | 23 | bool clear_devices = false, | |
| 24 | 24 | string import_scope = "", | |
| 25 | 25 | Dictionary<string, Tensor> input_map = null, | |
@@ -61,7 +61,7 @@ public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_sco | |||
| 61 | 61 | return_elements: return_elements); | |
| 62 | 62 | ||
| 63 | 63 | // Restores all the other collections. | |
| 64 | - var variable_objects = new Dictionary<ByteString, RefVariable>(); | ||
| 64 | + var variable_objects = new Dictionary<ByteString, VariableV1>(); | ||
| 65 | 65 | foreach(var col in meta_graph_def.CollectionDef.OrderBy(x => x.Key)) | |
| 66 | 66 | { | |
| 67 | 67 | // Don't add unbound_inputs to the new graph. | |
@@ -83,11 +83,14 @@ public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_sco | |||
| 83 | 83 | { | |
| 84 | 84 | foreach (var value in col.Value.BytesList.Value) | |
| 85 | 85 | { | |
| 86 | - RefVariable variable = null; | ||
| 86 | + VariableV1 variable = null; | ||
| 87 | 87 | if (!variable_objects.ContainsKey(value)) | |
| 88 | 88 | { | |
| 89 | 89 | 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); | ||
| 91 | 94 | variable_objects[value] = variable; | |
| 92 | 95 | } | |
| 93 | 96 | variable = variable_objects[value]; | |
@@ -126,9 +129,9 @@ public static (Dictionary<string, RefVariable>, ITensorOrOperation[]) import_sco | |||
| 126 | 129 | } | |
| 127 | 130 | } | |
| 128 | 131 | ||
| 129 | - var variables = graph.get_collection<RefVariable>(ops.GraphKeys.GLOBAL_VARIABLES, | ||
| 132 | + var variables = graph.get_collection<VariableV1>(ops.GraphKeys.GLOBAL_VARIABLES, | ||
| 130 | 133 | scope: scope_to_prepend_to_names); | |
| 131 | - var var_list = new Dictionary<string, RefVariable>(); | ||
| 134 | + var var_list = new Dictionary<string, VariableV1>(); | ||
| 132 | 135 | variables.ForEach(v => var_list[ops.strip_name_scope(v.name, scope_to_prepend_to_names)] = v); | |
| 133 | 136 | ||
| 134 | 137 | return (var_list, imported_return_elements); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -32,6 +32,27 @@ public static Tensor[] _AddGrad(Operation op, Tensor[] grads) | |||
| 32 | 32 | return new Tensor[] { r1, r2 }; | |
| 33 | 33 | } | |
| 34 | 34 | ||
| 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 | + | ||
| 35 | 56 | /// <summary> | |
| 36 | 57 | /// Returns grad * exp(x). | |
| 37 | 58 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -74,6 +74,23 @@ public static Tensor[] _SoftmaxCrossEntropyWithLogitsGrad(Operation op, Tensor[] | |||
| 74 | 74 | }; | |
| 75 | 75 | } | |
| 76 | 76 | ||
| 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 | + | ||
| 77 | 94 | private static bool IsZero(Tensor g) | |
| 78 | 95 | { | |
| 79 | 96 | if (new string[] { "ZerosLike", "Zeros" }.Contains(g.op.type)) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -24,6 +24,8 @@ public static Func<Operation, Tensor[], Tensor[]> get_gradient_function(Operatio | |||
| 24 | 24 | return nn_grad._BiasAddGrad(oper, out_grads); | |
| 25 | 25 | case "ConcatV2": | |
| 26 | 26 | return array_grad._ConcatGradV2(oper, out_grads); | |
| 27 | + case "DivNoNan": | ||
| 28 | + return math_grad._DivNoNanGrad(oper, out_grads); | ||
| 27 | 29 | case "Exp": | |
| 28 | 30 | return math_grad._ExpGrad(oper, out_grads); | |
| 29 | 31 | case "Identity": | |
@@ -62,6 +64,8 @@ public static Func<Operation, Tensor[], Tensor[]> get_gradient_function(Operatio | |||
| 62 | 64 | return nn_grad._SoftmaxGrad(oper, out_grads); | |
| 63 | 65 | case "SoftmaxCrossEntropyWithLogits": | |
| 64 | 66 | return nn_grad._SoftmaxCrossEntropyWithLogitsGrad(oper, out_grads); | |
| 67 | + case "SparseSoftmaxCrossEntropyWithLogits": | ||
| 68 | + return nn_grad._SparseSoftmaxCrossEntropyWithLogitsGrad(oper, out_grads); | ||
| 65 | 69 | case "Transpose": | |
| 66 | 70 | return array_grad._TransposeGrad(oper, out_grads); | |
| 67 | 71 | case "TopK": | |
| Back | FazBrowse Home | New Git URL |
0 commit comments