sgnts.base.array_backend
¶
Class for specifying array operation implementations. Specifically, we define a set of generic array operations that can be implemented in various backends (e.g., numpy, pytorch, tensorflow, etc.). This allows us to write generic code that can be run on different backends without modification.
The operations are defined as static methods in the ArrayBackend class, and
must be implemented in subclasses. The current set of operations includes:
arange: Create an array of evenly spaced valuescat: Concatenate arrays along a specified axisfull: Create an array filled with a specified valuematmul: Perform matrix multiplication of two arraysones: Create an array of onespad: Pad an array with zerosstack: Stack arrays along a new axissum: Sum of array elements over a given axiszeros: Create an array of zeros
ArrayBackend
¶
Base class for array operation implementations. Subclasses should implement the array operations as static methods.
Source code in src/sgnts/base/array_backend.py
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 | |
arange(stop, start=0, step=1)
staticmethod
¶
Returns a 1-D array with values from the interval [start, stop), taken
with common difference step begining from start.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stop
|
float
|
float, the stopping value for the set of points |
required |
start
|
float
|
float, default 0, the starting value for the set of points |
0
|
step
|
float
|
float, default 1, the gap between each pair of adjacent points |
1
|
Returns:
| Type | Description |
|---|---|
Array
|
Array, an array of evenly spaced values |
Source code in src/sgnts/base/array_backend.py
cat(data, axis)
staticmethod
¶
Concatenate arrays along a specified axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[Array]
|
Iterable[Array]: Arrays to concatenate, all with the same shape |
required |
axis
|
int
|
int: Axis along which to concatenate the arrays |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Concatenated array |
Source code in src/sgnts/base/array_backend.py
full(shape, fill_value)
staticmethod
¶
Create an array filled with a specified value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Tuple[int, ...]
|
Tuple[int, ...]: Shape of the array |
required |
fill_value
|
Any
|
Any: Value to fill the array with |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Array filled with the specified value |
Source code in src/sgnts/base/array_backend.py
matmul(a, b)
staticmethod
¶
Matrix multiplication of two arrays. out = a x b
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array
|
Array, the first array |
required |
b
|
Array
|
Array, the second array |
required |
Returns:
| Type | Description |
|---|---|
Array
|
Array, the result of the matrix multiplication |
Source code in src/sgnts/base/array_backend.py
ones(shape)
staticmethod
¶
Create an array of ones.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Tuple[int, ...]
|
Tuple[int, ...]: Shape of the array |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Array of ones |
pad(data, pad_samples)
staticmethod
¶
Pad an array with zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Array
|
Array: Array to pad |
required |
pad_samples
|
tuple[int, int]
|
tuple: Number of zeros to pad at each end of the array |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Padded array |
Source code in src/sgnts/base/array_backend.py
stack(data, axis=0)
classmethod
¶
Stack arrays along a new axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[Array]
|
Iterable[Array]: Arrays to stack, all with the same shape |
required |
axis
|
int
|
int: Axis along which to stack the arrays |
0
|
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Stacked array |
Source code in src/sgnts/base/array_backend.py
sum(a, axis=None)
staticmethod
¶
Sum of array elements over a given axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
a
|
Array
|
Array, elements to sum |
required |
axis
|
Optional[Union[int, tuple[int, ...]]]
|
Optional[int, tuple[int, ...]], axis or axes along which a sum is performed |
None
|
Returns:
| Type | Description |
|---|---|
Array
|
Array, an array of summed elements |
Source code in src/sgnts/base/array_backend.py
zeros(shape)
staticmethod
¶
Create an array of zeros.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shape
|
Tuple[int, ...]
|
Tuple[int, ...]: Shape of the array |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Array |
Array
|
Array of zeros |