This repository was archived by the owner on Jul 24, 2022. It is now read-only.
forked from dotnet/machinelearning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVectorType.cs
More file actions
181 lines (162 loc) · 7.54 KB
/
Copy pathVectorType.cs
File metadata and controls
181 lines (162 loc) · 7.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Immutable;
using System.Linq;
using System.Text;
using Microsoft.ML.Internal.DataView;
using Microsoft.ML.Internal.Utilities;
namespace Microsoft.ML.Data
{
/// <summary>
/// The standard vector type. The representation type of this is <see cref="VBuffer{T}"/>,
/// where the type parameter is in <see cref="ItemType"/>.
/// </summary>
public sealed class VectorDataViewType : StructuredDataViewType
{
/// <summary>
/// The dimensions. This will always have at least one item. All values will be non-negative.
/// As with <see cref="Size"/>, a zero value indicates that the vector type is considered to have
/// unknown length along that dimension.
/// </summary>
/// <remarks>
/// In the case where this is a multi-dimensional type, that is, a situation where <see cref="Dimensions"/>
/// has length greater than one, since <see cref="VBuffer{T}"/> itself is a single dimensional structure,
/// we must clarify what we mean. The indices represent a "flattened" view of the coordinates implicit in the
/// dimensions. We consider that the last dimension is the most "minor" index. In the case where <see cref="Dimensions"/>
/// has length <c>2</c>, this is commonly referred to as row-major order. So, if you hypothetically had
/// dimensions of <c>{ 5, 2 }</c>, then the <see cref="VBuffer{T}"/> values would be all of length <c>10</c>,
/// and the flattened indices <c>0, 1, 2, 3, 4, ...</c> would correspond to "coordinates" of
/// <c>(0, 0), (0, 1), (1, 0), (1, 1), (2, 0), ...</c>, respectively.
/// </remarks>
public ImmutableArray<int> Dimensions { get; }
/// <summary>
/// Constructs a new single-dimensional vector type.
/// </summary>
/// <param name="itemType">The type of the items contained in the vector.</param>
/// <param name="size">The size of the single dimension.</param>
public VectorDataViewType(PrimitiveDataViewType itemType, int size = 0)
: base(GetRawType(itemType))
{
Contracts.CheckParam(size >= 0, nameof(size));
ItemType = itemType;
Size = size;
Dimensions = ImmutableArray.Create(Size);
}
/// <summary>
/// Constructs a potentially multi-dimensional vector type.
/// </summary>
/// <param name="itemType">The type of the items contained in the vector.</param>
/// <param name="dimensions">The dimensions. Note that, like <see cref="Dimensions"/>, must be non-empty, with all
/// non-negative values. Also, because <see cref="Size"/> is the product of <see cref="Dimensions"/>, the result of
/// multiplying all these values together must not overflow <see cref="int"/>.</param>
public VectorDataViewType(PrimitiveDataViewType itemType, params int[] dimensions)
: base(GetRawType(itemType))
{
Contracts.CheckParam(ArrayUtils.Size(dimensions) > 0, nameof(dimensions));
Contracts.CheckParam(dimensions.All(d => d >= 0), nameof(dimensions));
ItemType = itemType;
Dimensions = dimensions.ToImmutableArray();
Size = ComputeSize(Dimensions);
}
/// <summary>
/// Constructs a potentially multi-dimensional vector type.
/// </summary>
/// <param name="itemType">The type of the items contained in the vector.</param>
/// <param name="dimensions">The dimensions. Note that, like <see cref="Dimensions"/>, must be non-empty, with all
/// non-negative values. Also, because <see cref="Size"/> is the product of <see cref="Dimensions"/>, the result of
/// multiplying all these values together must not overflow <see cref="int"/>.</param>
public VectorDataViewType(PrimitiveDataViewType itemType, ImmutableArray<int> dimensions)
: base(GetRawType(itemType))
{
Contracts.CheckParam(dimensions.Length > 0, nameof(dimensions));
Contracts.CheckParam(dimensions.All(d => d >= 0), nameof(dimensions));
ItemType = itemType;
Dimensions = dimensions;
Size = ComputeSize(Dimensions);
}
private static Type GetRawType(PrimitiveDataViewType itemType)
{
Contracts.CheckValue(itemType, nameof(itemType));
return typeof(VBuffer<>).MakeGenericType(itemType.RawType);
}
private static int ComputeSize(ImmutableArray<int> dims)
{
int size = 1;
for (int i = 0; i < dims.Length; ++i)
size = checked(size * dims[i]);
return size;
}
/// <summary>
/// Whether this is a vector type with known size.
/// Equivalent to <c><see cref="Size"/> > 0</c>.
/// </summary>
public bool IsKnownSize => Size > 0;
/// <summary>
/// The type of the items stored as values in vectors of this type.
/// </summary>
public PrimitiveDataViewType ItemType { get; }
/// <summary>
/// The size of the vector. A value of zero means it is a vector whose size is unknown.
/// A vector whose size is known should correspond to values that always have the same <see cref="VBuffer{T}.Length"/>,
/// whereas one whose size is unknown may have values whose <see cref="VBuffer{T}.Length"/> varies from record to record.
/// Note that this is always the product of the elements in <see cref="Dimensions"/>.
/// </summary>
public int Size { get; }
public override bool Equals(DataViewType other)
{
if (other == this)
return true;
if (!(other is VectorDataViewType tmp))
return false;
if (!ItemType.Equals(tmp.ItemType))
return false;
if (Size != tmp.Size)
return false;
if (Dimensions.Length != tmp.Dimensions.Length)
return false;
for (int i = 0; i < Dimensions.Length; i++)
{
if (Dimensions[i] != tmp.Dimensions[i])
return false;
}
return true;
}
public override bool Equals(object other)
{
return other is DataViewType tmp && Equals(tmp);
}
public override int GetHashCode()
{
int hash = Hashing.CombineHash(ItemType.GetHashCode(), Size);
hash = Hashing.CombineHash(hash, Dimensions.Length);
for (int i = 0; i < Dimensions.Length; i++)
hash = Hashing.CombineHash(hash, Dimensions[i].GetHashCode());
return hash;
}
public override string ToString()
{
var sb = new StringBuilder();
sb.Append("Vector<").Append(ItemType);
if (Dimensions.Length == 1)
{
if (Size > 0)
sb.Append(", ").Append(Size);
}
else
{
foreach (var dim in Dimensions)
{
sb.Append(", ");
if (dim > 0)
sb.Append(dim);
else
sb.Append('*');
}
}
sb.Append(">");
return sb.ToString();
}
}
}