|
5 | 5 |
|
6 | 6 | namespace Tensorflow.Common.Types |
7 | 7 | { |
8 | | - public class GeneralizedTensorShape: IEnumerable<long?[]>, INestStructure<long?>, INestable<long?> |
| 8 | + public class GeneralizedTensorShape: Nest<Shape> |
9 | 9 | { |
10 | | - public TensorShapeConfig[] Shapes { get; set; } |
11 | | - /// <summary> |
12 | | - /// create a single-dim generalized Tensor shape. |
13 | | - /// </summary> |
14 | | - /// <param name="dim"></param> |
15 | | - public GeneralizedTensorShape(int dim, int size = 1) |
| 10 | + public GeneralizedTensorShape(Shape value, string? name = null) |
16 | 11 | { |
17 | | - var elem = new TensorShapeConfig() { Items = new long?[] { dim } }; |
18 | | - Shapes = Enumerable.Repeat(elem, size).ToArray(); |
19 | | - //Shapes = new TensorShapeConfig[size]; |
20 | | - //Shapes.Initialize(new TensorShapeConfig() { Items = new long?[] { dim } }); |
21 | | - //Array.Initialize(Shapes, new TensorShapeConfig() { Items = new long?[] { dim } }); |
22 | | - ////Shapes = new TensorShapeConfig[] { new TensorShapeConfig() { Items = new long?[] { dim } } }; |
| 12 | + NodeValue = value; |
| 13 | + NestType = NestType.Node; |
23 | 14 | } |
24 | 15 |
|
25 | | - public GeneralizedTensorShape(Shape shape) |
| 16 | + public GeneralizedTensorShape(IEnumerable<Shape> values, string? name = null) |
26 | 17 | { |
27 | | - Shapes = new TensorShapeConfig[] { shape }; |
| 18 | + ListValue = values.Select(s => new Nest<Shape>(s) as INestStructure<Shape>).ToList(); |
| 19 | + Name = name; |
| 20 | + NestType = NestType.List; |
28 | 21 | } |
29 | 22 |
|
30 | | - public GeneralizedTensorShape(TensorShapeConfig shape) |
| 23 | + public GeneralizedTensorShape(Dictionary<string, Shape> value, string? name = null) |
31 | 24 | { |
32 | | - Shapes = new TensorShapeConfig[] { shape }; |
| 25 | + DictValue = value.ToDictionary(x => x.Key, x => new Nest<Shape>(x.Value) as INestStructure<Shape>); |
| 26 | + Name = name; |
| 27 | + NestType = NestType.Dictionary; |
33 | 28 | } |
34 | 29 |
|
35 | | - public GeneralizedTensorShape(TensorShapeConfig[] shapes) |
| 30 | + public GeneralizedTensorShape(Nest<Shape> other) |
36 | 31 | { |
37 | | - Shapes = shapes; |
38 | | - } |
39 | | - |
40 | | - public GeneralizedTensorShape(IEnumerable<Shape> shape) |
41 | | - { |
42 | | - Shapes = shape.Select(x => (TensorShapeConfig)x).ToArray(); |
| 32 | + NestType = other.NestType; |
| 33 | + NodeValue = other.NodeValue; |
| 34 | + DictValue = other.DictValue; |
| 35 | + ListValue = other.ListValue; |
| 36 | + Name = other.Name; |
43 | 37 | } |
44 | 38 |
|
45 | 39 | public Shape ToSingleShape() |
46 | 40 | { |
47 | | - if (Shapes.Length != 1) |
| 41 | + var shapes = Flatten().ToList(); |
| 42 | + if (shapes.Count != 1) |
48 | 43 | { |
49 | 44 | throw new ValueError("The generalized shape contains more than 1 dim."); |
50 | 45 | } |
51 | | - var shape_config = Shapes[0]; |
52 | | - Debug.Assert(shape_config is not null); |
53 | | - return new Shape(shape_config.Items.Select(x => x is null ? -1 : x.Value).ToArray()); |
| 46 | + return shapes[0]; |
54 | 47 | } |
55 | 48 |
|
56 | 49 | public long ToNumber() |
57 | 50 | { |
58 | | - if(Shapes.Length != 1 || Shapes[0].Items.Length != 1) |
| 51 | + var shapes = Flatten().ToList(); |
| 52 | + if (shapes.Count != 1 || shapes[0].ndim != 1) |
59 | 53 | { |
60 | 54 | throw new ValueError("The generalized shape contains more than 1 dim."); |
61 | 55 | } |
62 | | - var res = Shapes[0].Items[0]; |
63 | | - return res is null ? -1 : res.Value; |
64 | | - } |
65 | | - |
66 | | - public Shape[] ToShapeArray() |
67 | | - { |
68 | | - return Shapes.Select(x => new Shape(x.Items.Select(y => y is null ? -1 : y.Value).ToArray())).ToArray(); |
69 | | - } |
70 | | - |
71 | | - public IEnumerable<long?> Flatten() |
72 | | - { |
73 | | - List<long?> result = new List<long?>(); |
74 | | - foreach(var shapeConfig in Shapes) |
75 | | - { |
76 | | - result.AddRange(shapeConfig.Items); |
77 | | - } |
78 | | - return result; |
79 | | - } |
80 | | - public INestStructure<TOut> MapStructure<TOut>(Func<long?, TOut> func) |
81 | | - { |
82 | | - List<Nest<TOut>> lists = new(); |
83 | | - foreach(var shapeConfig in Shapes) |
84 | | - { |
85 | | - lists.Add(new Nest<TOut>(shapeConfig.Items.Select(x => new Nest<TOut>(func(x))))); |
86 | | - } |
87 | | - return new Nest<TOut>(lists); |
| 56 | + return shapes[0].dims[0]; |
88 | 57 | } |
89 | 58 |
|
90 | | - public Nest<long?> AsNest() |
| 59 | + public INestStructure<TensorShapeConfig> ToTensorShapeConfigs() |
91 | 60 | { |
92 | | - Nest<long?> DealWithSingleShape(TensorShapeConfig config) |
93 | | - { |
94 | | - if (config.Items.Length == 0) |
95 | | - { |
96 | | - return Nest<long?>.Empty; |
97 | | - } |
98 | | - else if (config.Items.Length == 1) |
99 | | - { |
100 | | - return new Nest<long?>(config.Items[0]); |
101 | | - } |
102 | | - else |
103 | | - { |
104 | | - return new Nest<long?>(config.Items.Select(x => new Nest<long?>(x))); |
105 | | - } |
106 | | - } |
107 | | - |
108 | | - if(Shapes.Length == 0) |
109 | | - { |
110 | | - return Nest<long?>.Empty; |
111 | | - } |
112 | | - else if(Shapes.Length == 1) |
113 | | - { |
114 | | - return DealWithSingleShape(Shapes[0]); |
115 | | - } |
116 | | - else |
117 | | - { |
118 | | - return new Nest<long?>(Shapes.Select(s => DealWithSingleShape(s))); |
119 | | - } |
120 | | - } |
121 | | - |
122 | | - |
123 | | - |
124 | | - public static implicit operator GeneralizedTensorShape(int dims) |
125 | | - => new GeneralizedTensorShape(dims); |
126 | | - |
127 | | - public IEnumerator<long?[]> GetEnumerator() |
128 | | - { |
129 | | - foreach (var shape in Shapes) |
130 | | - { |
131 | | - yield return shape.Items; |
132 | | - } |
| 61 | + return MapStructure(s => new TensorShapeConfig() { Items = s.dims.Select<long, long?>(x => x == -1 ? null : x).ToArray() }); |
133 | 62 | } |
134 | 63 |
|
135 | | - IEnumerator IEnumerable.GetEnumerator() |
| 64 | + public static implicit operator GeneralizedTensorShape(Shape shape) |
136 | 65 | { |
137 | | - return GetEnumerator(); |
| 66 | + return new GeneralizedTensorShape(shape); |
138 | 67 | } |
139 | 68 | } |
140 | 69 | } |
0 commit comments