|
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) |
16 | | - { |
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 } } }; |
23 | | - } |
| 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) |
| 16 | + //{ |
| 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 } } }; |
| 23 | + //} |
24 | 24 |
|
25 | | - public GeneralizedTensorShape(Shape shape) |
| 25 | + public GeneralizedTensorShape(Shape value, string? name = null) |
26 | 26 | { |
27 | | - Shapes = new TensorShapeConfig[] { shape }; |
| 27 | + NodeValue = value; |
| 28 | + NestType = NestType.Node; |
28 | 29 | } |
29 | 30 |
|
30 | | - public GeneralizedTensorShape(TensorShapeConfig shape) |
| 31 | + public GeneralizedTensorShape(IEnumerable<Shape> values, string? name = null) |
31 | 32 | { |
32 | | - Shapes = new TensorShapeConfig[] { shape }; |
| 33 | + ListValue = values.Select(s => new Nest<Shape>(s) as INestStructure<Shape>).ToList(); |
| 34 | + Name = name; |
| 35 | + NestType = NestType.List; |
33 | 36 | } |
34 | 37 |
|
35 | | - public GeneralizedTensorShape(TensorShapeConfig[] shapes) |
| 38 | + public GeneralizedTensorShape(Dictionary<string, Shape> value, string? name = null) |
36 | 39 | { |
37 | | - Shapes = shapes; |
| 40 | + DictValue = value.ToDictionary(x => x.Key, x => new Nest<Shape>(x.Value) as INestStructure<Shape>); |
| 41 | + Name = name; |
| 42 | + NestType = NestType.Dictionary; |
38 | 43 | } |
39 | 44 |
|
40 | | - public GeneralizedTensorShape(IEnumerable<Shape> shape) |
| 45 | + public GeneralizedTensorShape(Nest<Shape> other) |
41 | 46 | { |
42 | | - Shapes = shape.Select(x => (TensorShapeConfig)x).ToArray(); |
| 47 | + NestType = other.NestType; |
| 48 | + NodeValue = other.NodeValue; |
| 49 | + DictValue = other.DictValue; |
| 50 | + ListValue = other.ListValue; |
| 51 | + Name = other.Name; |
43 | 52 | } |
44 | 53 |
|
45 | 54 | public Shape ToSingleShape() |
46 | 55 | { |
47 | | - if (Shapes.Length != 1) |
| 56 | + var shapes = Flatten().ToList(); |
| 57 | + if (shapes.Count != 1) |
48 | 58 | { |
49 | 59 | throw new ValueError("The generalized shape contains more than 1 dim."); |
50 | 60 | } |
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()); |
| 61 | + return shapes[0]; |
54 | 62 | } |
55 | 63 |
|
56 | 64 | public long ToNumber() |
57 | 65 | { |
58 | | - if(Shapes.Length != 1 || Shapes[0].Items.Length != 1) |
| 66 | + var shapes = Flatten().ToList(); |
| 67 | + if (shapes.Count != 1 || shapes[0].ndim != 1) |
59 | 68 | { |
60 | 69 | throw new ValueError("The generalized shape contains more than 1 dim."); |
61 | 70 | } |
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); |
88 | | - } |
89 | | - |
90 | | - public Nest<long?> AsNest() |
91 | | - { |
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 | | - } |
| 71 | + return shapes[0].dims[0]; |
120 | 72 | } |
121 | | - |
122 | | - |
123 | 73 |
|
124 | | - public static implicit operator GeneralizedTensorShape(int dims) |
125 | | - => new GeneralizedTensorShape(dims); |
126 | | - |
127 | | - public IEnumerator<long?[]> GetEnumerator() |
| 74 | + public INestStructure<TensorShapeConfig> ToTensorShapeConfigs() |
128 | 75 | { |
129 | | - foreach (var shape in Shapes) |
130 | | - { |
131 | | - yield return shape.Items; |
132 | | - } |
| 76 | + return MapStructure(s => new TensorShapeConfig() { Items = s.dims.Select<long, long?>(x => x == -1 ? null : x).ToArray() }); |
133 | 77 | } |
134 | 78 |
|
135 | | - IEnumerator IEnumerable.GetEnumerator() |
| 79 | + public static implicit operator GeneralizedTensorShape(Shape shape) |
136 | 80 | { |
137 | | - return GetEnumerator(); |
| 81 | + return new GeneralizedTensorShape(shape); |
138 | 82 | } |
139 | 83 | } |
140 | 84 | } |
0 commit comments