TensorShape
public struct TensorShape: Equatable, Comparable
The tensor shape description. Specify the shape and data type of a Tensor
data object.
## dataType Indicate the initial set data type.
## shapeArray
The shape
attrribute defines the dimension of a Tensor
object. In serrano
, we follow row-marjor
order to store and access elements in a Tensor
object and each row is represented as an array.
For a given shape array with n
indices [i_0, i_1, ..., i_(n-1)]
, each index from i_0
to i_(n-2)
defines the number of rows in its
previous dimension. The last index define the number of elements in its previous dimention.
For example, a TensorShape
object with shpae
as [2, 1, 3]
.
It’s 1st dimension has 2
rows in which each row has 1
row with 3 elements.
User should be clear and unserstanding what a Tensor
object looks like when they passing in a TensorShape
argument.
For example, a Tensor
object with shape [2, 3]
, it can be visulized as a nested array like below:
// shape [2, 3]
[
[1.0, 0.5, 1.3],
[2.0, 4.2, 6.7],
]
And a typical real-world example, a 3-channel RGB image data could be represented with shape [3, image_hight, image_width]
:
[
// R channel frame
[
[232, ..., 123], // (# of Int elements) = image_width
.
.
.
[113, ..., 225]
], // (# of Array elements) = image_hight
// G channel frame
[
[232, ..., 123],
.
.
.
[113, ..., 225]
],
// B channel frame
[
[232, ..., 123],
.
.
.
[113, ..., 225]
]
]
Equatable
Two TensorShape objects are equal (==
)if they have the same shape
.
Two TensorShape objects are dot equal (.==
) if they have the same shape
and same dataType
.
Rank is 0
If a Tensor
object’s shapeArray has 0
rank, it indicates that it just contains a scalar value.
-
Data type
Declaration
Swift
var dataType:TensorDataType
-
Shape array
Declaration
Swift
var shapeArray: [Int]
-
Rank of dimensions
Declaration
Swift
var rank: Int
-
Element count
Declaration
Swift
var count: Int
-
Description
Declaration
Swift
var description: String
-
Public init for out init
Declaration
Swift
public init(dataType: TensorDataType, shape: [Int])
Parameters
dataType
dataType
shape
shape
-
Check if
shape
array is validDeclaration
Swift
public func shapeVerify() -> Bool
-
Get
shape
array in reversedDeclaration
Swift
public func reversedShapArray() -> [Int]
-
Get transposed shape of current shape.
Note
only works for shapes with rank values as2
. Otherwise,fatalError
will be throws.Declaration
Swift
public func transposed() -> TensorShape
-
Tow shapes dot equals when have same
dataType
and==
.Declaration
Swift
public static func .==(shapeA: TensorShape, shapeB: TensorShape) -> Bool
Parameters
shapeA
shape A
shapeB
shape B
-
Two shap equals when has same rank and same dimensions.
Declaration
Swift
public static func ==(shapeA: TensorShape, shapeB: TensorShape) -> Bool
Parameters
shapeA
shapeA description
shapeB
shapeB description
Return Value
return value description
-
shapeA
larger thanshapeB
if:Declaration
Swift
public static func >(shapeA: TensorShape, shapeB: TensorShape) -> Bool
Parameters
shapeA
shapeA description
shapeB
shapeB description
Return Value
return value description
-
shapeA
larger than or equal toshapeB
if: -shapeA
has largerrank
or -shapeA
andshapeB
has same rank whileshapeA
has a larger or samecount
Declaration
Swift
public static func >=(shapeA: TensorShape, shapeB: TensorShape) -> Bool
Parameters
shapeA
shapeA description
shapeB
shapeB description
Return Value
return value description
-
shapeA
less thanshapeB
if: -shapeA
has smallerrank
or -shapeA
andshapeB
has same rank whileshapeA
has a smallercount
Declaration
Swift
public static func <(shapeA: TensorShape, shapeB: TensorShape) -> Bool
Parameters
shapeA
shapeA description
shapeB
shapeB description
Return Value
return value description
-
shapeA
less than or equal toshapeB
if: -shapeA
has smallerrank
or -shapeA
andshapeB
has same rank whileshapeA
has a smaller or samecount
Declaration
Swift
public static func <=(shapeA: TensorShape, shapeB: TensorShape) -> Bool
Parameters
shapeA
shapeA description
shapeB
shapeB description
Return Value
return value description