Skip to content

Commit 8784c31

Browse files
committed
change tensor shape to Shape.
1 parent b5f357a commit 8784c31

33 files changed

Lines changed: 118 additions & 72 deletions

File tree

‎src/TensorFlowNET.Core/Binding.Util.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public static int len(object a)
155155
switch (a)
156156
{
157157
case Tensor tensor:
158-
return tensor.shape[0];
158+
return (int)tensor.shape[0];
159159
case Tensors arr:
160160
return arr.Length;
161161
case Array arr:

‎src/TensorFlowNET.Core/Framework/tensor_shape.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public static class tensor_shape
1010
{
1111
public static void assert_is_compatible_with(this Tensor self, Tensor other)
1212
{
13-
if (!self.is_compatible_with(other))
13+
/*if (!self.is_compatible_with(other))
1414
{
1515
var selfDim = self.shape
1616
.Aggregate(new StringBuilder("{"), (sb, i) => sb.Append(i).Append(", "), sb => sb.ToString())
@@ -21,7 +21,7 @@ public static void assert_is_compatible_with(this Tensor self, Tensor other)
2121
.Replace(", }", "}");
2222
2323
throw new ArgumentException($"Dimensions {selfDim} and {otherDim} are not compatible");
24-
}
24+
}*/
2525
}
2626

2727
public static bool is_compatible_with(this Tensor self, Tensor other)

‎src/TensorFlowNET.Core/Gradients/image_grad.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@ public static Tensor[] _ResizeNearestNeighborGrad(Operation op, Tensor[] grads)
2727
{
2828
var grad = grads[0];
2929
var image = op.inputs[0];
30-
var shape = new TensorShape(image.shape.Skip(1).Take(2).ToArray());
30+
var shape = new TensorShape(image.shape.dims.Skip(1).Take(2).ToArray());
3131
Tensor image_shape = null;
3232
if (shape.is_fully_defined())
33-
image_shape = constant_op.constant(image.shape.Skip(1).Take(2).ToArray());
33+
image_shape = constant_op.constant(image.shape.dims.Skip(1).Take(2).ToArray());
3434
else
3535
image_shape = array_ops.shape(image)["1:3"];
3636

‎src/TensorFlowNET.Core/Gradients/math_grad.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ public static Tensor[] _MulGrad(Operation op, Tensor[] grads)
195195

196196
if (op is EagerOperation op_eager &&
197197
op_eager.SkipInputIndices.Contains(1) &&
198-
y.NDims == 0)
198+
y.ndim == 0)
199199
{
200200
return new Tensor[]
201201
{
@@ -759,7 +759,7 @@ public static Tensor[] _PowGrad(Operation op, Tensor[] grads)
759759

760760
if (op is EagerOperation op_eager &&
761761
op_eager.SkipInputIndices.Contains(1) &&
762-
y.NDims == 0)
762+
y.ndim == 0)
763763
{
764764
x = math_ops.conj(x);
765765
y = math_ops.conj(y);

‎src/TensorFlowNET.Core/NumPy/Axis.cs‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Text;
45

56
namespace Tensorflow
@@ -22,6 +23,12 @@ public static implicit operator Axis((int, int, int) axis)
2223

2324
public static implicit operator Axis(int[] axis)
2425
=> new Axis(axis);
26+
27+
public static implicit operator Axis(long[] shape)
28+
=> new Axis(shape.Select(x => (int)x).ToArray());
29+
30+
public static implicit operator Axis(Shape shape)
31+
=> new Axis(shape.dims.Select(x => (int)x).ToArray());
2532
}
2633
}
2734

‎src/TensorFlowNET.Core/Numpy/NDArray.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ public partial class NDArray
1111
Tensor _tensor;
1212
public TF_DataType dtype => _tensor.dtype;
1313
public ulong size => _tensor.size;
14-
public ulong dtypesize => _tensor.itemsize;
15-
public int ndim => _tensor.NDims;
14+
public ulong dtypesize => _tensor.dtypesize;
15+
public ulong bytesize => _tensor.bytesize;
16+
public int ndim => _tensor.ndim;
1617
public long[] dims => _tensor.dims.Select(x => Convert.ToInt64(x)).ToArray();
1718
public Shape shape => _tensor.shape;
1819
public IntPtr data => _tensor.TensorDataPointer;

‎src/TensorFlowNET.Core/Numpy/Shape.cs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,21 @@ public static implicit operator Shape((int, int, int, int) dims)
4848
public static implicit operator Shape((long, long, long, long) dims)
4949
=> new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4);
5050

51+
public static implicit operator int[](Shape shape)
52+
=> shape.dims.Select(x => (int)x).ToArray();
53+
54+
public static implicit operator long[](Shape shape)
55+
=> shape.dims;
56+
5157
public bool IsEmpty => size == 0;
5258

5359
public bool IsScalar => ndim == 0;
5460

5561
public static Shape Scalar
5662
=> new Shape(new long[0]);
5763

64+
public long this[int n] => dims[n];
65+
5866
/// <summary>
5967
/// Returns the size this shape represents.
6068
/// </summary>
@@ -81,6 +89,25 @@ public ulong size
8189
}
8290
}
8391

92+
public bool is_fully_defined()
93+
{
94+
return ndim > -1 && dims != null && dims.Count(x => x < 1) == 0;
95+
}
96+
97+
public bool is_compatible_with(TensorShape shape2)
98+
{
99+
if (dims != null && shape2.dims != null)
100+
{
101+
if (dims.Contains(-1) || shape2.dims.Contains(-1))
102+
return true;
103+
104+
if (size != (ulong)shape2.size)
105+
return false;
106+
}
107+
108+
return true;
109+
}
110+
84111
public override bool Equals(object obj)
85112
{
86113
if(obj is Shape shape)

‎src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public Tensor _batch_shape_tensor()
9292

9393
public Tensor _batch_shape()
9494
{
95-
return array_ops.broadcast_static_shape(new Tensor(_loc.shape), new Tensor(_scale.shape));
95+
return array_ops.broadcast_static_shape(new Tensor(_loc.shape.dims), new Tensor(_scale.shape.dims));
9696
}
9797

9898
protected override Tensor _log_prob(Tensor x)

‎src/TensorFlowNET.Core/Operations/NnOps/rnn_cell_impl.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public static Tensor _concat(Tensor prefix, int suffix, bool @static = false)
2727
{
2828
var p = prefix;
2929
var p_static = tensor_util.constant_value(prefix);
30-
if (p.NDims == 0)
30+
if (p.ndim == 0)
3131
p = array_ops.expand_dims(p, 0);
32-
else if (p.NDims != 1)
32+
else if (p.ndim != 1)
3333
throw new ValueError($"prefix tensor must be either a scalar or vector, but saw tensor: {p}");
3434

3535
var s_tensor_shape = new TensorShape(suffix);

‎src/TensorFlowNET.Core/Operations/array_ops.cs‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public static Tensor zeros(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOA
186186

187187
private static Tensor _constant_if_small(int value, Tensor shape)
188188
{
189-
return shape < 1000L;
189+
return shape < 1000UL;
190190
}
191191

192192
private static Tensor _constant_if_small<T>(T value, TensorShape shape, TF_DataType dtype, string name)
@@ -330,7 +330,7 @@ public static Tensor rank(Tensor[] inputs, string name = null)
330330
{
331331
name = scope;
332332
var input_tensor = ops.convert_to_tensor(inputs);
333-
return constant_op.constant(input_tensor.NDims, dtype: tf.int32, name: name);
333+
return constant_op.constant(input_tensor.ndim, dtype: tf.int32, name: name);
334334
});
335335
}
336336

@@ -340,7 +340,7 @@ public static Tensor rank_internal(Tensor input, string name = null, bool optimi
340340
{
341341
name = scope;
342342
var input_tensor = ops.convert_to_tensor(input);
343-
var input_shape = tensor_util.to_shape(input_tensor.shape);
343+
var input_shape = input_tensor.shape;
344344
if (optimize && input_shape.ndim > 0)
345345
return constant_op.constant(input_shape.ndim, dtype: tf.int32, name: name);
346346
else
@@ -364,7 +364,7 @@ public static Tensor ones_like(Tensor tensor, TF_DataType dtype = TF_DataType.Dt
364364
tensor = ops.convert_to_tensor(tensor, name: "tensor");
365365

366366
// is_fully_defined return unexpected value.
367-
if (optimize && tensor_util.to_shape(tensor.shape).is_fully_defined() && dtype != TF_DataType.TF_VARIANT)
367+
if (optimize && tensor.shape.is_fully_defined() && dtype != TF_DataType.TF_VARIANT)
368368
{
369369

370370
}
@@ -589,9 +589,9 @@ public static Tensor shape_internal(Tensor input, string name = null, bool optim
589589
if (!tf.Context.executing_eagerly())
590590
{
591591
var input_shape = input.TensorShape;
592-
if (optimize && input.NDims > -1 && input_shape.is_fully_defined())
592+
if (optimize && input.ndim > -1 && input_shape.is_fully_defined())
593593
{
594-
var nd = np.array(input.shape).astype(out_type.as_system_dtype());
594+
var nd = np.array(input.shape.dims).astype(out_type.as_system_dtype());
595595
return constant_op.constant(nd, name: name);
596596
}
597597
}
@@ -607,7 +607,7 @@ private static Tensor size_internal<T>(T input, string name = null, bool optimiz
607607
name = scope;
608608

609609
var input_tensor = ops.convert_to_tensor(input);
610-
var input_shape = tensor_util.to_shape(input_tensor.shape);
610+
var input_shape = input_tensor.shape;
611611
if (optimize)
612612
{
613613
if (input_shape.is_fully_defined())
@@ -633,7 +633,7 @@ public static Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.D
633633
tensor = ops.convert_to_tensor(tensor, name: "tensor");
634634

635635
// is_fully_defined return unexpected value.
636-
if (optimize && tensor_util.to_shape(tensor.shape).is_fully_defined() && dtype != TF_DataType.TF_VARIANT)
636+
if (optimize && tensor.shape.is_fully_defined() && dtype != TF_DataType.TF_VARIANT)
637637
{
638638

639639
}
@@ -933,7 +933,7 @@ public static Tensor[] split(Tensor value, Tensor size_splits, int axis, int num
933933
string name = "split")
934934
{
935935
if (num == -1)
936-
num = size_splits.shape[0];
936+
num = (int)size_splits.shape[0];
937937

938938
return gen_array_ops.split_v(value, size_splits, axis, num, name: name);
939939
}

0 commit comments

Comments
 (0)