forked from Tensor-Array/Tensor-Array-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtensor.py
More file actions
350 lines (312 loc) · 13.3 KB
/
tensor.py
File metadata and controls
350 lines (312 loc) · 13.3 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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"""
# src/tensor_array/core/tensor.py
# This module defines the Tensor class, which represents a multi-dimensional array (tensor) and provides various mathematical operations, shape manipulation, and data type conversion.
# The Tensor class is designed to be used in a computational graph for automatic differentiation.
"""
from __future__ import annotations
from ..tensor2 import Tensor as _Tensor
from .datatypes import DataTypes
class Tensor(_Tensor):
"""
A class representing a multi-dimensional array (tensor) with various operations.
This class provides methods for mathematical operations, shape manipulation, and data type conversion.
It is designed to be used in a computational graph for automatic differentiation.
"""
def __init__(self, *args, **kwargs):
"""
Initializes the Tensor instance.
"""
super().__init__(*args, **kwargs)
def transpose(self, dim0: int, dim1: int, isDevive: bool) -> Tensor:
"""
Transposes the tensor along the specified dimensions.
Args:
dim0 (int): The first dimension to transpose.
dim1 (int): The second dimension to transpose.
isDevive (bool): Whether to perform the operation in-place on the original tensor.
Returns:
Tensor: A new tensor that is the transposed version of the original tensor.
"""
return super().transpose(dim0, dim1, isDevive)
def calc_grad(self) -> None:
"""
Calculates the gradient of the tensor with respect to its inputs.
"""
super().calc_grad()
def get_grad(self) -> Tensor:
"""
Returns the gradient of the tensor.
Returns:
Tensor: A tensor representing the gradient of the original tensor.
If the tensor does not have a gradient, it returns None.
"""
return super().get_grad()
def sin(self) -> Tensor:
"""
Computes the sine of the tensor element-wise.
Returns:
Tensor: A tensor containing the sine of each element in the original tensor.
"""
return super().sin()
def cos(self) -> Tensor:
"""
Computes the cosine of the tensor element-wise.
Returns:
Tensor: A tensor containing the cosine of each element in the original tensor.
"""
return super().cos()
def tan(self) -> Tensor:
"""
Computes the tangent of the tensor element-wise.
Returns:
Tensor: A tensor containing the tangent of each element in the original tensor.
"""
return super().tan()
def sinh(self) -> Tensor:
"""
Computes the hyperbolic sine of the tensor element-wise.
Returns:
Tensor: A tensor containing the hyperbolic sine of each element in the original tensor.
"""
return super().sinh()
def cosh(self) -> Tensor:
"""
Computes the hyperbolic cosine of the tensor element-wise.
Returns:
Tensor: A tensor containing the hyperbolic cosine of each element in the original tensor.
"""
return super().cosh()
def tanh(self) -> Tensor:
"""
Computes the hyperbolic tangent of the tensor element-wise.
Returns:
Tensor: A tensor containing the hyperbolic tangent of each element in the original tensor.
"""
return super().tanh()
def log(self) -> Tensor:
"""
Computes the natural logarithm of the tensor element-wise.
Returns:
Tensor: A tensor containing the natural logarithm of each element in the original tensor.
"""
return super().log()
def clone(self) -> Tensor:
"""
Creates a copy of the tensor.
Returns:
Tensor: A new tensor that is a copy of the original tensor.
This method does not perform any operations on the tensor; it simply returns a new instance with the same data.
"""
return super().clone()
def cast(self, dtype: DataTypes) -> Tensor:
"""
Casts the tensor to a different data type.
Args:
dtype (DataTypes): The target data type to cast the tensor to.
Returns:
Tensor: A new tensor that is a copy of the original tensor, but with the specified data type.
This method does not perform any operations on the tensor; it simply returns a new instance with the same data but in the specified data type.
"""
return super().cast(dtype)
def numpy(self):
"""
Converts the tensor to a NumPy array.
Returns:
numpy.ndarray: A NumPy array containing the data of the tensor.
This method allows for easy interoperability with NumPy, enabling the use of NumPy functions and operations on the tensor data.
Note: This method does not perform any operations on the tensor; it simply returns a NumPy array representation of the tensor.
"""
return super().numpy()
def shape(self) -> tuple:
"""
Returns the shape of the tensor.
Returns:
tuple: A tuple representing the dimensions of the tensor.
This method provides the size of each dimension of the tensor, allowing for easy inspection of its structure.
Note: This method does not perform any operations on the tensor; it simply returns the shape as a tuple.
"""
return super().shape()
def dtype(self) -> DataTypes:
"""
Returns the data type of the tensor.
Returns:
DataTypes: The data type of the tensor, represented as a DataTypes enum.
This method provides information about the type of data stored in the tensor, such as whether it is an integer, float, or boolean.
Note: This method does not perform any operations on the tensor; it simply returns the data type.
"""
return super().dtype()
def __getitem__(self, item) -> Tensor:
"""
Gets an item or a slice from the tensor.
Args:
item: The index or slice to retrieve.
Returns:
Tensor: A new tensor that is a view of the original tensor at the specified index or slice.
"""
return super().__getitem__(item)
def __len__(self) -> int:
"""
Returns the number of elements in the first dimension of the tensor.
Returns:
int: The size of the first dimension of the tensor.
"""
return super().__len__()
def __str__(self) -> str:
"""
Returns a string representation of the tensor.
Returns:
str: A string that describes the tensor, including its shape and data type.
"""
return super().__str__()
def __repr__(self) -> str:
"""
Returns a detailed string representation of the tensor.
Returns:
str: A string that includes the class name, shape, data type, and other relevant information about the tensor.
"""
return super().__repr__()
def __add__(self, other: Tensor) -> Tensor:
"""
Adds two tensors element-wise.
Args:
other (Tensor): The tensor to add to the current tensor.
Returns:
Tensor: A new tensor that is the element-wise sum of the current tensor and the other tensor.
This method does not modify the original tensors; it returns a new tensor with the result of the addition.
"""
return super().__add__(other)
def __sub__(self, other: Tensor) -> Tensor:
"""
Subtracts two tensors element-wise.
Args:
other (Tensor): The tensor to subtract from the current tensor.
Returns:
Tensor: A new tensor that is the element-wise difference of the current tensor and the other tensor.
This method does not modify the original tensors; it returns a new tensor with the result of the subtraction.
"""
return super().__sub__(other)
def __mul__(self, other: Tensor) -> Tensor:
"""
Multiplies two tensors element-wise.
Args:
other (Tensor): The tensor to multiply with the current tensor.
Returns:
Tensor: A new tensor that is the element-wise product of the current tensor and the other tensor.
This method does not modify the original tensors; it returns a new tensor with the result of the multiplication.
"""
return super().__mul__(other)
def __truediv__(self, other: Tensor) -> Tensor:
"""
Divides two tensors element-wise.
Args:
other (Tensor): The tensor to divide the current tensor by.
Returns:
Tensor: A new tensor that is the element-wise quotient of the current tensor and the other tensor.
This method does not modify the original tensors; it returns a new tensor with the result of the division.
"""
return super().__truediv__(other)
def __pow__(self, other: Tensor) -> Tensor:
"""
Raises the current tensor to the power of another tensor element-wise.
Args:
other (Tensor): The tensor representing the exponent.
Returns:
Tensor: A new tensor that is the element-wise result of the current tensor raised to the power of the other tensor.
"""
return super().__pow__(other)
def __matmul__(self, other: Tensor) -> Tensor:
"""
Performs matrix multiplication between two tensors.
Args:
other (Tensor): The tensor to multiply with the current tensor.
Returns:
Tensor: A new tensor that is the result of matrix multiplication between the current tensor and the other tensor.
This method does not modify the original tensors; it returns a new tensor with the result of the matrix multiplication.
"""
return super().__matmul__(other)
def __eq__(self, other: Tensor) -> bool:
"""
Checks if two tensors are equal.
Args:
other (Tensor): The tensor to compare with the current tensor.
Returns:
bool: True if the tensors are equal, False otherwise.
This method compares the data, shape, and data type of the tensors to determine equality.
"""
return super().__eq__(other)
def __ne__(self, other: Tensor) -> bool:
"""
Checks if two tensors are not equal.
Args:
other (Tensor): The tensor to compare with the current tensor.
Returns:
bool: True if the tensors are not equal, False otherwise.
This method compares the data, shape, and data type of the tensors to determine inequality.
"""
return super().__ne__(other)
def __lt__(self, other: Tensor) -> bool:
"""
Checks if the current tensor is less than another tensor.
Args:
other (Tensor): The tensor to compare with the current tensor.
Returns:
bool: True if the current tensor is less than the other tensor, False otherwise.
"""
return super().__lt__(other)
def __le__(self, other: Tensor) -> bool:
"""
Checks if the current tensor is less than or equal to another tensor.
Args:
other (Tensor): The tensor to compare with the current tensor.
Returns:
bool: True if the current tensor is less than or equal to the other tensor, False otherwise.
"""
return super().__le__(other)
def __gt__(self, other: Tensor) -> bool:
"""
Checks if the current tensor is greater than another tensor.
Args:
other (Tensor): The tensor to compare with the current tensor.
Returns:
bool: True if the current tensor is greater than the other tensor, False otherwise.
"""
return super().__gt__(other)
def __ge__(self, other: Tensor) -> bool:
"""
Checks if the current tensor is greater than or equal to another tensor.
Args:
other (Tensor): The tensor to compare with the current tensor.
Returns:
bool: True if the current tensor is greater than or equal to the other tensor, False otherwise.
"""
return super().__ge__(other)
def __pos__(self) -> Tensor:
"""
Returns the tensor itself, unchanged.
Returns:
Tensor: The original tensor.
This method is typically used to indicate that the tensor should be treated as a positive value, but it does not modify the tensor in any way.
"""
return super().__pos__()
def __neg__(self) -> Tensor:
"""
Negates the tensor element-wise.
Returns:
Tensor: A new tensor with the negated values.
"""
return super().__neg__()
def __abs__(self) -> Tensor:
"""
Returns the absolute value of the tensor element-wise.
Returns:
Tensor: A new tensor with the absolute values.
"""
return super().__abs__()
def _hash__(self) -> int:
"""
Returns a hash value for the tensor.
Returns:
int: A hash value representing the tensor.
This method is useful for using tensors as keys in dictionaries or sets.
"""
return super()._hash__()