| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3fcc4d8 commit d0ec659
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -140,6 +140,16 @@ public Tensor identity(Tensor input, string name = null) | |||
| 140 | 140 | public Tensor gather(Tensor @params, Tensor indices, string name = null, int axis = 0) | |
| 141 | 141 | => array_ops.gather(@params, indices, name: name, axis: ops.convert_to_tensor(axis)); | |
| 142 | 142 | ||
| 143 | + /// <summary> | ||
| 144 | + /// Gather slices from `params` into a Tensor with shape specified by `indices`. | ||
| 145 | + /// </summary> | ||
| 146 | + /// <param name="params"></param> | ||
| 147 | + /// <param name="indices"></param> | ||
| 148 | + /// <param name="name"></param> | ||
| 149 | + /// <returns></returns> | ||
| 150 | + public Tensor gather_nd(Tensor @params, Tensor indices, string name = null) | ||
| 151 | + => gen_array_ops.gather_nd(@params, indices, name: name); | ||
| 152 | + | ||
| 143 | 153 | /// <summary> | |
| 144 | 154 | /// Return the elements, either from `x` or `y`, depending on the `condition`. | |
| 145 | 155 | /// </summary> | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -403,7 +403,26 @@ public static Tensor[] _TileGrad(Operation op, Tensor[] grads) | |||
| 403 | 403 | input_grad.set_shape(op.inputs[0].GetShape()); | |
| 404 | 404 | } | |
| 405 | 405 | return new Tensor[] { input_grad, null }; | |
| 406 | + } | ||
| 406 | 407 | ||
| 408 | + [RegisterGradient("GatherNd")] | ||
| 409 | + public static Tensor[] _GatherNdGrad(Operation op, Tensor[] grads) | ||
| 410 | + { | ||
| 411 | + var @ref = op.inputs[0]; | ||
| 412 | + var indices = op.inputs[1]; | ||
| 413 | + var grad = grads[0]; | ||
| 414 | + var ref_shape = array_ops.shape(@ref, out_type: indices.dtype); | ||
| 415 | + Tensor ref_grad = null; | ||
| 416 | + if (indices.shape.ndim == 2 && indices.shape.dims[indices.shape.Length - 1] == 1) | ||
| 417 | + { | ||
| 418 | + ref_grad = (Tensor)new IndexedSlices(grad, array_ops.squeeze(indices, axis: -1), ref_shape); | ||
| 419 | + } | ||
| 420 | + else | ||
| 421 | + { | ||
| 422 | + ref_grad = gen_array_ops.scatter_nd(indices, grad, ref_shape); | ||
| 423 | + } | ||
| 424 | + return new Tensor[] { ref_grad, null }; | ||
| 407 | 425 | } | |
| 426 | + | ||
| 408 | 427 | } | |
| 409 | 428 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -829,7 +829,7 @@ public static Tensor strided_slice_grad(Tensor shape, Tensor begin, Tensor end, | |||
| 829 | 829 | /// <returns>A `Tensor`. Has the same type as `input`. | |
| 830 | 830 | /// Contains the same data as `input`, but has one or more dimensions of | |
| 831 | 831 | /// size 1 removed.</returns> | |
| 832 | - public static Tensor squeeze(Tensor input, int[] axis = null, string name = null) | ||
| 832 | + public static Tensor squeeze(Tensor input, Axis axis = null, string name = null) | ||
| 833 | 833 | => gen_array_ops.squeeze(input, axis, name); | |
| 834 | 834 | ||
| 835 | 835 | public static Tensor identity(Tensor input, string name = null) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -62,7 +62,7 @@ public void SquaredDifference_1D() | |||
| 62 | 62 | // Calcute the gradient of (x1-x2)^2 | |
| 63 | 63 | // by Automatic Differentiation in Eager mode | |
| 64 | 64 | // Expected is 2*(abs(x1-x2)) | |
| 65 | - Tensor x1 = new NDArray( new float[] { 1, 3, 5, 21, 19, 17 }); | ||
| 65 | + Tensor x1 = new NDArray(new float[] { 1, 3, 5, 21, 19, 17 }); | ||
| 66 | 66 | Tensor x2 = new NDArray(new float[] { 29, 27, 23, 7, 11, 13 }); | |
| 67 | 67 | float[] expected = new float[] | |
| 68 | 68 | { | |
@@ -187,5 +187,20 @@ public void Tile() | |||
| 187 | 187 | Assert.AreEqual((float)grad.numpy(), 2.0f); | |
| 188 | 188 | } | |
| 189 | 189 | } | |
| 190 | + | ||
| 191 | + [TestMethod] | ||
| 192 | + public void GatherNdTest() | ||
| 193 | + { | ||
| 194 | + var x = tf.constant(new float[,] { { 1.0f, 2.0f, 3.0f }, { 1.0f, 2.0f, 3.0f }, { 1.0f, 2.0f, 3.0f } }, dtype: TF_DataType.TF_FLOAT); | ||
| 195 | + var indices = tf.constant(new int[,] { { 0, 1 }, { 1, 1 }, { 2, 1 } }, dtype: TF_DataType.TF_INT32); | ||
| 196 | + using (var tape = tf.GradientTape()) | ||
| 197 | + { | ||
| 198 | + tape.watch(x); | ||
| 199 | + var res = tf.gather_nd(x, indices); | ||
| 200 | + var grad = tape.gradient(res, x); | ||
| 201 | + var expected = np.array(new float[,] { { 0f, 1f, 0f }, { 0f, 1f, 0f }, { 0f, 1f, 0f } }); | ||
| 202 | + Assert.IsTrue(Enumerable.SequenceEqual(grad.ToArray<float>(), expected.ToArray<float>())); | ||
| 203 | + } | ||
| 204 | + } | ||
| 190 | 205 | } | |
| 191 | 206 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments