Skip to content

Commit da33a8b

Browse files
committed
Introduce Axis record type.
1 parent 7f387d0 commit da33a8b

21 files changed

Lines changed: 124 additions & 120 deletions

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

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -467,12 +467,9 @@ public Tensor real(Tensor input, string name = null)
467467
/// <param name="keepdims">If true, retains reduced dimensions with length 1.</param>
468468
/// <param name="name"></param>
469469
/// <returns>The reduced tensor.</returns>
470-
public Tensor reduce_any(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
470+
public Tensor reduce_any(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
471471
=> math_ops.reduce_any(input_tensor, axis: axis, keepdims: keepdims, name: name);
472472

473-
public Tensor reduce_any(Tensor input_tensor, int axis = 0, bool keepdims = false, string name = null)
474-
=> math_ops.reduce_any(input_tensor, axis: new[] { axis }, keepdims: keepdims, name: name);
475-
476473
/// <summary>
477474
/// Computes the "logical and" of elements across dimensions of a tensor.
478475
/// </summary>
@@ -481,7 +478,7 @@ public Tensor reduce_any(Tensor input_tensor, int axis = 0, bool keepdims = fals
481478
/// <param name="keepdims"></param>
482479
/// <param name="name"></param>
483480
/// <returns>The reduced tensor.</returns>
484-
public Tensor reduce_all(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
481+
public Tensor reduce_all(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
485482
=> math_ops.reduce_all(input_tensor, axis: axis, keepdims: keepdims, name: name);
486483

487484
/// <summary>
@@ -492,7 +489,7 @@ public Tensor reduce_all(Tensor input_tensor, int[] axis = null, bool keepdims =
492489
/// <param name="keepdims"></param>
493490
/// <param name="name"></param>
494491
/// <returns></returns>
495-
public Tensor reduce_prod(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
492+
public Tensor reduce_prod(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
496493
=> math_ops.reduce_prod(input_tensor, axis: axis, keepdims: keepdims, name: name);
497494

498495
/// <summary>
@@ -537,19 +534,16 @@ public Tensor reduce_sum(Tensor input, TensorShape axis, int? reduction_indices
537534
/// <param name="keepdims"></param>
538535
/// <param name="name"></param>
539536
/// <returns></returns>
540-
public Tensor reduce_max(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
541-
=> math_ops.reduce_max(input_tensor, axis, keepdims, name);
542-
543-
public Tensor reduce_max(Tensor input_tensor, int axis, bool keepdims = false, string name = null)
537+
public Tensor reduce_max(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
544538
=> math_ops.reduce_max(input_tensor, axis, keepdims, name);
545539

546-
public Tensor reduce_min(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
540+
public Tensor reduce_min(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
547541
=> math_ops.reduce_min(input_tensor, axis, keepdims, name);
548542

549-
public Tensor reduce_std(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
543+
public Tensor reduce_std(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
550544
=> math_ops.reduce_std(input_tensor, axis, keepdims, name);
551545

552-
public Tensor reduce_variance(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null)
546+
public Tensor reduce_variance(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null)
553547
=> math_ops.reduce_variance(input_tensor, axis, keepdims, name);
554548

555549
public Tensor sigmoid<T>(T x, string name = null)
@@ -558,15 +552,9 @@ public Tensor sigmoid<T>(T x, string name = null)
558552
public Tensor sum(Tensor input, int axis, bool keep_dims = false, string name = null)
559553
=> gen_math_ops._sum(input, axis, keep_dims: keep_dims, name: name);
560554

561-
public Tensor reduce_mean(Tensor input_tensors, int axis, bool keepdims = false, string name = null)
562-
=> math_ops.reduce_mean(input_tensors, axis: new[] { axis }, keepdims: keepdims, name: name);
563-
564-
public Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
555+
public Tensor reduce_mean(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
565556
=> math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices);
566557

567-
public Tensor reduce_mean(Tensor[] input_tensors, int? axis = null, bool keepdims = false, string name = null)
568-
=> math_ops.reduce_mean(input_tensors, axis: axis, keepdims: keepdims, name: name);
569-
570558
public Tensor round(Tensor x, string name = null)
571559
=> gen_math_ops.round(x, name: name);
572560

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public Tensor elu(Tensor features, string name = null)
8989
=> gen_nn_ops.elu(features, name: name);
9090

9191
public (Tensor, Tensor) moments(Tensor x,
92-
int[] axes,
92+
Axis axes,
9393
string name = null,
9494
bool keep_dims = false) => nn_impl.moments(x,
9595
axes,

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace Tensorflow
1919
public partial class tensorflow
2020
{
2121
public Tensor reduce_logsumexp(Tensor input_tensor,
22-
int[] axis = null,
22+
Axis? axis = null,
2323
bool keepdims = false,
2424
string name = null) => math_ops.reduce_logsumexp(input_tensor, axis, keepdims, name);
2525

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Tensorflow
6+
{
7+
public record Axis(params int[] axis)
8+
{
9+
public int this[int index] => axis[index];
10+
11+
public static implicit operator int[]?(Axis axis)
12+
=> axis?.axis;
13+
14+
public static implicit operator Axis(int axis)
15+
=> new Axis(axis);
16+
17+
public static implicit operator Axis((int, int) axis)
18+
=> new Axis(axis);
19+
20+
public static implicit operator Axis((int, int, int) axis)
21+
=> new Axis(axis);
22+
23+
public static implicit operator Axis(int[] axis)
24+
=> new Axis(axis);
25+
}
26+
}
27+
28+
namespace System.Runtime.CompilerServices
29+
{
30+
internal static class IsExternalInit { }
31+
}

src/TensorFlowNET.Core/NumPy/Numpy.Math.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ public partial class np
1212
public static NDArray log(NDArray x)
1313
=> throw new NotImplementedException("");
1414

15-
public static NDArray prod(NDArray array, int? axis = null, Type dtype = null, bool keepdims = false)
16-
=> tf.reduce_prod(ops.convert_to_tensor(array));
15+
public static NDArray prod(NDArray array, Axis? axis = null, Type? dtype = null, bool keepdims = false)
16+
=> tf.reduce_prod(ops.convert_to_tensor(array), axis: axis);
1717

1818
public static NDArray prod<T>(params T[] array) where T : unmanaged
1919
=> tf.reduce_prod(ops.convert_to_tensor(array));

src/TensorFlowNET.Core/Numpy/Shape.cs

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,21 @@
33
using System.Linq;
44
using System.Text;
55

6-
namespace Tensorflow.NumPy
6+
namespace Tensorflow
77
{
88
public class Shape
99
{
1010
public int ndim => _dims.Length;
1111
long[] _dims;
1212
public long[] dims => _dims;
1313

14+
public Shape()
15+
{
16+
}
17+
18+
public Shape(params int[] dims)
19+
=> _dims = dims.Select(x => Convert.ToInt64(x)).ToArray();
20+
1421
public Shape(params long[] dims)
1522
=> _dims = dims;
1623

@@ -21,14 +28,27 @@ public static implicit operator Shape(long[] dims)
2128
=> new Shape(dims);
2229

2330
public static implicit operator Shape(int[] dims)
24-
=> new Shape(dims.Select(x => Convert.ToInt64(x)).ToArray());
31+
=> new Shape(dims);
32+
33+
public static implicit operator Shape((int, int) dims)
34+
=> new Shape(dims.Item1, dims.Item2);
2535

2636
public static implicit operator Shape((long, long) dims)
2737
=> new Shape(dims.Item1, dims.Item2);
2838

29-
public bool IsSliced => throw new NotImplementedException("");
30-
public bool IsScalar => throw new NotImplementedException("");
31-
public bool IsBroadcasted => throw new NotImplementedException("");
39+
public static implicit operator Shape((int, int, int) dims)
40+
=> new Shape(dims.Item1, dims.Item2, dims.Item3);
41+
42+
public static implicit operator Shape((long, long, long) dims)
43+
=> new Shape(dims.Item1, dims.Item2, dims.Item3);
44+
45+
public static implicit operator Shape((int, int, int, int) dims)
46+
=> new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4);
47+
48+
public static implicit operator Shape((long, long, long, long) dims)
49+
=> new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4);
50+
51+
public bool IsScalar => ndim == 0;
3252

3353
public static Shape Scalar
3454
=> new Shape(new long[0]);
@@ -55,6 +75,18 @@ public ulong size
5575

5676
public bool IsEmpty => throw new NotImplementedException("");
5777

78+
public override bool Equals(object obj)
79+
{
80+
if(obj is Shape shape)
81+
{
82+
if (shape.ndim != ndim)
83+
return false;
84+
if (Enumerable.SequenceEqual(dims, shape.dims))
85+
return true;
86+
}
87+
return base.Equals(obj);
88+
}
89+
5890
public override string ToString()
5991
{
6092
return "(" + string.Join(", ", _dims) + ")";

src/TensorFlowNET.Core/Numpy/Slice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text;
55
using System.Text.RegularExpressions;
66

7-
namespace Tensorflow.NumPy
7+
namespace Tensorflow
88
{
99
/// <summary> <br></br>
1010
/// NDArray can be indexed using slicing <br></br>

src/TensorFlowNET.Core/Operations/image_ops_impl.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -968,9 +968,9 @@ public static Tensor per_image_standardization(Tensor image)
968968
var num_pixels_ = array_ops.shape(image).dims;
969969
num_pixels_ = num_pixels_.Skip(num_pixels_.Length - 3).Take(num_pixels_.Length - (num_pixels_.Length - 3)).ToArray();
970970
Tensor num_pixels = math_ops.reduce_prod(new Tensor(num_pixels_));
971-
Tensor image_mean = math_ops.reduce_mean(image, axis: new int[] { -1, -2, -3 }, keepdims: true);
971+
Tensor image_mean = math_ops.reduce_mean(image, axis: new(-1, -2, -3), keepdims: true);
972972

973-
var stddev = math_ops.reduce_std(image, axis: new int[] { -1, -2, -3 }, keepdims: true);
973+
var stddev = math_ops.reduce_std(image, axis: new(-1, -2, -3), keepdims: true);
974974
var min_stddev = math_ops.rsqrt(math_ops.cast(num_pixels, image.dtype));
975975
var adjusted_stddev = math_ops.maximum(stddev, min_stddev);
976976

@@ -1408,7 +1408,7 @@ public static Tensor psnr(Tensor a, Tensor b, Tensor max_val, string name = null
14081408
max_val = convert_image_dtype(max_val, dtypes.float32);
14091409
a = convert_image_dtype(a, dtypes.float32);
14101410
b = convert_image_dtype(b, dtypes.float32);
1411-
Tensor mse = math_ops.reduce_mean(gen_math_ops.squared_difference(a, b), new int[] { -3, -2, -1 });
1411+
Tensor mse = math_ops.reduce_mean(gen_math_ops.squared_difference(a, b), new(-3, -2, -1));
14121412
var psnr_val = math_ops.subtract(
14131413
(20 * math_ops.log(max_val)) / math_ops.log(ops.convert_to_tensor(10.0)),
14141414
math_ops.cast(10 / math_ops.log(ops.convert_to_tensor(10)), dtypes.float32) * math_ops.log(mse),
@@ -1503,8 +1503,8 @@ Tensor reducer(Tensor x)
15031503
(Tensor luminance, Tensor cs) = _ssim_helper(img1, img2, reducer, max_val, compensation, k1, k2);
15041504

15051505
var axes = constant_op.constant(new[] { -3, -2 }, dtype: dtypes.int32);
1506-
var ssim_val = math_ops.reduce_mean(luminance * cs, axes.dims);
1507-
cs = math_ops.reduce_mean(cs, axes.dims);
1506+
var ssim_val = math_ops.reduce_mean(luminance * cs, new(axes.dims));
1507+
cs = math_ops.reduce_mean(cs, new(axes.dims));
15081508
return (ssim_val, cs);
15091509
}
15101510

@@ -1527,7 +1527,7 @@ public static Tensor ssim(Tensor img1, Tensor img2, float max_val = 1f, float fi
15271527
(Tensor ssim_per_channel, Tensor ___) = _ssim_per_channel(img1, img2, max_val, filter_size,
15281528
filter_sigma, k1, k2);
15291529

1530-
return math_ops.reduce_mean(ssim_per_channel, new int[] { -1 });
1530+
return math_ops.reduce_mean(ssim_per_channel, new(-1));
15311531
});
15321532
}
15331533

@@ -1645,9 +1645,9 @@ Tensor[] do_pad(Tensor[] images, Tensor remainder)
16451645
var mcs_and_ssim = array_ops.stack(
16461646
math_ops.add(mcs, new[] { gen_nn_ops.relu(ssim_per_channel) }), axis: -1);
16471647
var ms_ssim = math_ops.reduce_prod(
1648-
math_ops.pow(mcs_and_ssim, power_factors), new int[] { -1 });
1648+
math_ops.pow(mcs_and_ssim, power_factors), new(-1));
16491649

1650-
return math_ops.reduce_mean(ms_ssim, new int[] { -1 });
1650+
return math_ops.reduce_mean(ms_ssim, new(-1));
16511651
});
16521652
}
16531653

@@ -1830,7 +1830,7 @@ internal static (Tensor, Tensor, Tensor, Tensor) _cross_suppression(Tensor boxes
18301830
new object[] { batch_size, tile_size, 4 });
18311831
var iou = _bbox_overlap(new_slice, box_slice);
18321832
var box_slice_after_suppression = array_ops.expand_dims(
1833-
math_ops.cast(math_ops.reduce_all(iou < iou_threshold, new int[] { 1 }),
1833+
math_ops.cast(math_ops.reduce_all(iou < iou_threshold, new(1)),
18341834
box_slice.dtype),
18351835
2) * box_slice;
18361836
return (boxes, box_slice_after_suppression, iou_threshold, inner_idx + 1);
@@ -1913,7 +1913,7 @@ I have no idea what's going on here. Not even going to try to port it yet.
19131913

19141914
output_size = output_size + math_ops.reduce_sum(
19151915
math_ops.cast(
1916-
math_ops.reduce_any(box_slice > 0, new int[] { 2 }), dtypes.int32), new int[] { 1 });
1916+
math_ops.reduce_any(box_slice > 0, new(2)), dtypes.int32), new int[] { 1 });
19171917
}
19181918
return (boxes, iou_threshold, output_size, idx + 1);
19191919
}
@@ -2074,7 +2074,7 @@ object[] suppression_loop_body(object[] args)
20742074

20752075
(Tensor values, Tensor indices) = gen_ops.top_k_v2(
20762076
math_ops.cast(math_ops.reduce_any(
2077-
(Tensor)selboxes__output_size_[0] > 0, new int[] { 2 }), dtypes.int32) *
2077+
(Tensor)selboxes__output_size_[0] > 0, new(2)), dtypes.int32) *
20782078
array_ops.expand_dims(
20792079
math_ops.range(num_boxes_after_padding, 0, -1), 0),
20802080
max_output_size);

0 commit comments

Comments
 (0)