|
2 | 2 | using System.Collections.Generic; |
3 | 3 | using System.Linq; |
4 | 4 | using System.Text; |
| 5 | +using Tensorflow.Framework; |
5 | 6 | using Tensorflow.Operations; |
6 | 7 | using static Tensorflow.Python; |
7 | 8 |
|
@@ -42,9 +43,9 @@ private static Tensor[] _ConcatGradHelper(Operation op, Tensor grad, int start_v |
42 | 43 | return end_value_index <= dim_index ? new Tensor[] { grad, null } : new Tensor[] { null, grad }; |
43 | 44 |
|
44 | 45 | var concat_dim = op.inputs[dim_index]; |
45 | | - if (end_value_index == -1) |
46 | | - end_value_index = op.inputs.Length - 1; |
47 | | - var input_values = op.inputs._inputs.Skip(start_value_index).Take(end_value_index - start_value_index).ToArray(); |
| 46 | + var input_values = op.inputs._inputs.Skip(start_value_index) |
| 47 | + .Take(end_value_index == -1 ? op.inputs.Length - 1 : end_value_index - start_value_index) |
| 48 | + .ToArray(); |
48 | 49 |
|
49 | 50 | var out_grads = new List<Tensor>(); |
50 | 51 | if (constant_op.is_constant(concat_dim)) |
@@ -92,10 +93,16 @@ there will be a small number of performance regressions.*/ |
92 | 93 | } |
93 | 94 |
|
94 | 95 | return (end_value_index <= dim_index ? |
95 | | - out_grads.ToArray().Concat(null) : |
| 96 | + out_grads.ToArray().Concat(new Tensor[] { null }) : |
96 | 97 | new Tensor[] { null }.Concat(out_grads)).ToArray(); |
97 | 98 | } |
98 | 99 |
|
| 100 | + [RegisterGradient("ExpandDims")] |
| 101 | + public static Tensor[] _ExpandDimsGrad(Operation op, Tensor[] grads) |
| 102 | + { |
| 103 | + return new Tensor[] { _ReshapeToInput(op, grads[0]), null }; |
| 104 | + } |
| 105 | + |
99 | 106 | /// <summary> |
100 | 107 | /// Extract the shapes of a set of input tensors. |
101 | 108 | /// </summary> |
@@ -125,6 +132,45 @@ private static Tensor[] _ExtractInputShapes(Tensor[] inputs) |
125 | 132 | return gen_ops.shape_n(inputs); |
126 | 133 | } |
127 | 134 |
|
| 135 | + /// <summary> |
| 136 | + /// Gradient for GatherV2 op. |
| 137 | + /// </summary> |
| 138 | + /// <param name="op"></param> |
| 139 | + /// <param name="grads"></param> |
| 140 | + /// <returns></returns> |
| 141 | + [RegisterGradient("GatherV2")] |
| 142 | + public static Tensor[] _GatherV2Grad(Operation op, Tensor[] grads) |
| 143 | + { |
| 144 | + var grad = grads[0]; |
| 145 | + var @params = op.inputs[0]; |
| 146 | + ops.colocate_with(@params); |
| 147 | + |
| 148 | + var params_shape = array_ops.shape(@params, out_type: tf.int64); |
| 149 | + params_shape = math_ops.cast(params_shape, tf.int32); |
| 150 | + |
| 151 | + var indices = op.inputs[1]; |
| 152 | + var indices_size = array_ops.expand_dims(array_ops.size(indices), 0); |
| 153 | + var axis = op.inputs[2]; |
| 154 | + var axis_static = tensor_util.constant_value(axis); |
| 155 | + |
| 156 | + // For axis 0 gathers, build an appropriately shaped IndexedSlices. |
| 157 | + if((int)axis_static == 0) |
| 158 | + { |
| 159 | + var params_tail_shape = params_shape[1]; |
| 160 | + var values_shape = array_ops.concat(new[] { indices_size, params_tail_shape }, 0); |
| 161 | + var values = array_ops.reshape(grad, values_shape); |
| 162 | + indices = array_ops.reshape(indices, indices_size); |
| 163 | + return new Tensor[] |
| 164 | + { |
| 165 | + new IndexedSlices(values, indices, params_shape), |
| 166 | + null, |
| 167 | + null |
| 168 | + }; |
| 169 | + } |
| 170 | + |
| 171 | + return new Tensor[] { null, null }; |
| 172 | + } |
| 173 | + |
128 | 174 | [RegisterGradient("Reshape")] |
129 | 175 | public static Tensor[] _ReshapeGrad(Operation op, Tensor[] grads) |
130 | 176 | { |
|
0 commit comments